php - 一起使用 mysql_fetch_assoc 和 mysql_result 吗?

标签 php mysql loops

我在尝试在同一个 PHP 脚本中同时使用 mysql_fetch_assocmysql_result 时遇到了很大麻烦。

最初(当不使用mysql_result函数时),我使用mysql_querymysql_fetch_assoc的组合来获取所需的数据库值,一切都是美好的。然后我在代码中添加了 2 行,以使用 mysql_result 获取某些“标题”字段值。

现在,如果我运行下面的脚本,即使有 2 个结果,我也只会收到 1 个结果。然后,如果我将 do/while 循环向上移动,使其位于其他 2 个代码块(mysql_fetch_assocmysql_result 行)之间,我将收到所需的 2 个结果。

我需要将循环放在 mysql_result 部分之后,因此不能将循环放在它之前。

// connect to DB and get values
mysql_select_db($database, $mywebsite);
$query_not_related_before = "SELECT  * FROM table  limit 2";
$not_related_before = mysql_query($query_not_related_before, $ mywebsite);
$row_not_related_before = mysql_fetch_assoc($not_related_before);


// Extract just the results from the title field (the problem area!)
$before_essayid4 = mysql_result($not_related_before,0, 'title');
$before_essayid5 = mysql_result($not_related_before,1, 'title');


// Display results etc
do { 
echo "<br />".$row_not_related_before['title']."<br />";} 
while ($row_not_related_before = mysql_fetch_assoc($not_related_before));

请帮忙,

非常感谢,

大卫

最佳答案

我不确定这是否能解决您的问题,但我认为您应该“寻求”返回结果。

mysql_data_seek ($not_related_before, 0)

此外,请查看 mysql_result 上的警告页面:

Calls to mysql_result() should not be mixed with calls to other functions that deal with the result set.

希望这有帮助;)

关于php - 一起使用 mysql_fetch_assoc 和 mysql_result 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5685805/

相关文章:

loops - 控制并行循环中的线程数并减少开销

Java 乐透模拟

php - 如何使用 preg_replace_callback?

php - 为什么 Symfony2 建议避免使用 'legacy' php session ?

PHP mySQL xampp fatal error

mysql - 我不知道/分组依据?有?子查询?我可以用什么?

mysql - 使用 mysql Like 查询从表中检索十进制值

javascript - 在 Wordpress 的一些管理页面上自定义 css 和 JS

php - 如何从数据库中获取可点击链接的列表

python-3.x - Python - 迭代列表 -Lambda