MySQL获取数组错误

时间:2011-06-16 21:01:31

标签: php mysql

  

可能重复:
  Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
  mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

我收到了以下PHP错误:

警告:mysql_fetch_array():提供的参数不是第32行/hermes/web03/b556/pow.kkarimu/htdocs/F12_MEDIA/Site/develop/v1/twitter/twitter_oauth.php中的有效MySQL结果资源

警告:mysql_fetch_array():提供的参数不是第38行/hermes/web03/b556/pow.kkarimu/htdocs/F12_MEDIA/Site/develop/v1/twitter/twitter_oauth.php中的有效MySQL结果资源

从这个php文件:

 $query = mysql_query("SELECT * FROM users_entity WHERE oauth_provider = 'twitter' AND oauth_id = ".$user_info->id);
    $result = mysql_fetch_array($query);

    // If not, let's add it to the database
    if(empty($result)){
        $query = mysql_query("INSERT INTO users_entity (oauth_provider, oauth_id, oauth_username, oauth_token, oauth_secret) VALUES ('twitter', {$user_info->id}, '{$user_info->screen_name}', '{$access_token['oauth_token']}', '{$access_token['oauth_token_secret']}')");
        $query = mysql_query("SELECT * FROM users_entity WHERE id = ".mysql_insert_id());
        $result = mysql_fetch_array($query);
    } else {
        // Update the tokens
        $query = mysql_query("UPDATE users_entity SET oauth_token = '{$access_token['oauth_token']}', oauth_secret = '{$access_token['oauth_token_secret']}' WHERE oauth_provider = 'twitter' AND oauth_id = {$user_info->id}");

*更新完整代码:

<?
require_once('../Connections/f12_database_connect.php');
?>
<?

$root = realpath($_SERVER["DOCUMENT_ROOT"]);

require("twitteroauth/twitteroauth.php");
session_start();

if(!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])){

// We've got everything we need
// TwitterOAuth instance, with two new parameters we got in twitter_login.php
$twitteroauth = new TwitterOAuth('Tt7HVYyZJC25GWMybpemg', '11nazzxqFrJEqVCxpdLfMBxq4fsh1iPsaaEIma5sI', $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
// Let's request the access token
$access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
// Save it in a session var
$_SESSION['access_token'] = $access_token;
// Let's get the user's info
$user_info = $twitteroauth->get('account/verify_credentials');
/*// Print user's info
print_r($user_info);*/

//Check and update database
if(isset($user_info->error)){
    // Something's wrong, go back to square 1
    header('Location: /twitter/twitter_login.php');
} else {
    // Let's find the user by its ID
    $query = mysql_query("SELECT * FROM users_entity WHERE oauth_provider = 'twitter' AND oauth_id = ".$user_info->id);
    $result = mysql_fetch_array($query);

    // If not, let's add it to the database
    if(empty($result)){
        $query = mysql_query("INSERT INTO users_entity (oauth_provider, oauth_id, oauth_username, oauth_token, oauth_secret) VALUES ('twitter', {$user_info->id}, '{$user_info->screen_name}', '{$access_token['oauth_token']}', '{$access_token['oauth_token_secret']}')");
        $query = mysql_query("SELECT * FROM users_entity WHERE id = ".mysql_insert_id());
        $result = mysql_fetch_array($query);
    } else {
        // Update the tokens
        $query = mysql_query("UPDATE users_entity SET oauth_token = '{$access_token['oauth_token']}', oauth_secret = '{$access_token['oauth_token_secret']}' WHERE oauth_provider = 'twitter' AND oauth_id = {$user_info->id}");
    }

    $_SESSION['twitter_id'] = $result['user_primary_id'];
    $_SESSION['twitter_username'] = $result['oauth_username'];
    $_SESSION['twitter_oauth_uid'] = $result['oauth_id'];
    $_SESSION['twitter_oauth_provider'] = $result['oauth_provider'];
    $_SESSION['twitter_oauth_token'] = $result['oauth_token'];
    $_SESSION['twitter_oauth_secret'] = $result['oauth_secret'];

    if(!empty($_SESSION['twitter_username'])){
    // User is logged in, redirect
    header('Location: /catalog/edition/standard/authenticate/login_success_twitter.php');
    }

}


} else {
    // Something's missing, go back to square 1
    header('Location: /twitter/twitter_login.php');
}

?>

2 个答案:

答案 0 :(得分:2)

这意味着您的查询失败。我敢打赌,如果你查看$query的值,就会看到它显示false,这是发生错误时从mysql_query()返回的内容。

此外,您的代码对SQL注入攻击是开放的。您应该至少使用mysql_real_escape_string()转义数据,但我强烈建议您使用PDO参数化方法,以完全避免此问题。

答案 1 :(得分:1)

试试这段代码:

$query  =   "SELECT * FROM users_entity WHERE oauth_provider = 'twitter' AND oauth_id = ".$user_info->id.' LIMIT 0,1';
$result = mysql_query($query);

// If exists, let's update it, else add it
if(is_resource($result) && mysql_num_rows($result) > 0){ // a record exists
    $row = mysql_fetch_array($query); // fetch record details

    // Update the tokens
    $query = mysql_query("UPDATE users_entity SET oauth_token = '{$access_token['oauth_token']}', oauth_secret = '{$access_token['oauth_token_secret']}' WHERE oauth_provider = 'twitter' AND oauth_id = {$user_info->id}");
} else{
    $query = mysql_query("INSERT INTO users_entity (oauth_provider, oauth_id, oauth_username, oauth_token, oauth_secret) VALUES ('twitter', {$user_info->id}, '{$user_info->screen_name}', '{$access_token['oauth_token']}', '{$access_token['oauth_token_secret']}')");
    $query = mysql_query("SELECT * FROM users_entity WHERE id = ".mysql_insert_id());
    $result = mysql_fetch_array($query);
}

您的第一个查询出现问题,mysql_fetch_array()将获取一行。您的查询不是将其限制为单行。我添加了LIMIT 0,1来做到这一点。

在执行mysql_fetch_array()之前,您应确保mysql_query()返回的结果是数据库资源,而不是其他类型,然后您可以获取其数据。这正是我添加if(is_resource($result) && mysql_num_rows($result) > 0)的原因,它将检查结果是否为资源,并且我们至少有一个mysql_query()结果的记录。

建议#1

如果我是你,我会制作oauth_provideroauth_id一个UNIQUE索引,并在一个查询中完成这项工作,即INSERT ... ON DUPLICATE ...

建议#2

如果我是你,我不会使用mysql_insert_id(),这对你来说并不完全准确,因为可能在中间插入了另一条记录,你最终得到了另一个用户的详细信息。

这将为您提供新插入用户的准确信息:

$query = mysql_query("SELECT * FROM users_entity WHERE oauth_provider = 'twitter' AND oauth_id = {$user_info->id} AND oauth_token = '{$access_token['oauth_token']}' ORDER BY id DESC LIMIT 0,1");