警告:mysql_result()[function.mysql-result]:无法跳转到MySQL结果索引6的第0行

时间:2014-08-22 17:57:17

标签: php function

我有一些代码,

if(logged_in() == true){ 
$website_id = (INT)$_GET['id'];
$addedById  = mysql_result(mysql_query("SELECT `user_id` FROM `websites` WHERE `id` = '$website_id'"), 0);
$q = mysql_fetch_assoc(mysql_query("SELECT `phone` FROM `users` WHERE `user_id` = '$addedById'"));
$result       = mysql_query("SELECT * FROM `websites` WHERE `id` = '$website_id'");
echo $q['phone'];
 }
else{ 
echo' please logged_in'; 
 }

并返回错误“警告:mysql_result()[function.mysql-result]:无法跳转到MySQL结果的第0行”

在行中

$addedById  = mysql_result(mysql_query("SELECT `user_id` FROM `websites` WHERE `id` = '$website_id'"), 0);

我尝试了几个选项来解决这个问题,什么都没有,我应该如何修复它?

1 个答案:

答案 0 :(得分:0)

尝试分离代码并检查查询结果..

$result = mysql_query('SELECT user_id FROM websites WHERE id = '.$website_id);
if (!$result || empty($result)) {
    echo 'Could not query:' . mysql_error();
}
echo mysql_result($result, 0);

这是你的代码有一些变化(不是一个好的代码,但试试看):

if(logged_in() == true){ 
    $website_id = (INT)$_GET['id'];

    $result = mysql_query("SELECT user_id FROM websites WHERE id = " . $website_id);
    if (!empty($result)) {
        $addedById = mysql_result($result, 0);

        $query_user = mysql_query("SELECT phone FROM users WHERE user_id = " . $addedById);
        $result_user = mysql_fetch_assoc($query_user);
        if (!empty($result_user) {
            echo $result_user[0]['phone'];
        }
    }
 } else { 
    echo' please logged_in'; 
 }

但是你不应该再使用这个mysql函数了,不推荐使用它们。 使用PDO或MySQLi。 http://php.net/manual/en/book.mysqli.php http://php.net/manual/en/ref.pdo-mysql.php