gpt4 book ai didi

php - MySQL 获取数组错误

转载 作者:行者123 更新时间:2023-11-29 06:16:09 24 4
gpt4 key购买 nike

Possible Duplicate:
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():提供的参数不是/hermes/web03/b556/pow.kkarimu/htdocs/F12_MEDIA/Site/develop/v1/twitter/twitter_oauth.php 第 32 行中的有效 MySQL 结果资源

警告:mysql_fetch_array():提供的参数不是/hermes/web03/b556/pow.kkarimu/htdocs/F12_MEDIA/Site/develop/v1/twitter/twitter_oauth.php 第 38 行中的有效 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');
}

?>

最佳答案

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

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

关于php - MySQL 获取数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6378548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com