(警告:mysql_fetch_assoc():提供的参数不是有效的MySQL结果资源),但查询返回数据

时间:2015-01-07 00:37:20

标签: php mysqli

确定。所以我正在运行的代码根本不起作用。当我直接在服务器上运行查询时,它工作,但这不适用于我的网页。我已浏览了所有帮助页面,无法查明错误。请帮忙。

function getUserData($id) {
        $servername = "******************";
        $dbname = "***********";
        $sql_username = "************";
        $sql_password = "****************";
        $conn = mysql_connect($servername, $sql_username, $sql_password, $dbname);
        $userData = array();
        $sql = mysql_query("SELECT * FROM users WHERE user_id ='".$id."'");
        while ($row = mysql_fetch_assoc($sql)) {

        $userData['id'] = $row['id'];   
        $userData['user_name'] = $row['username'];
        $userData['password'] = $row['password'];       
        $userData['aboutme'] = $row['aboutme'];     
        $userData['first_name'] = $row['fname'];
        $userData['last_name'] = $row['lname'];     
        $userData['gender'] = $row['gender'];
        $userData['user_email'] = $row['email'];    
        $userData['country'] = $row['country']; 
        $userData['language'] = $row['language'];   
        $userData['send_emails'] = $row['sendemails'];
        $userData['date'] = $row['date'];           
        }
        return $userData;
    }
    function getUserId($username) {
        $servername = "*********************";
        $dbname = "*********";
        $sql_username = "***********";
        $sql_password = "***********";
        $conn = mysql_connect($servername, $sql_username, $sql_password, $dbname);
        $sql = mysql_query("SELECT user_id FROM users WHERE user_name = '".$username."'");
        while ($row2 = mysql_fetch_assoc($sql)) {
            return $row2['user_id'];            
        }
    }

这就是所谓的功能。

    $userData = getUserData(getUserId($_SESSION['valid_user']))

1 个答案:

答案 0 :(得分:1)

mysql_connect($servername, $sql_username, $sql_password, $dbname)

  

bool mysql_select_db(string $ database_name [,resource $ link_identifier = NULL])

根据手册:

<强> http://php.net/manual/en/function.mysql-select-db.php

  • 第4个参数不符合您的想法。这是mysqli_()语法。

您需要使用mysql_select_db() http://php.net/manual/en/function.mysql-select-db.php

从手册中也可以:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

来自mysql_connect()

  

resource mysql_connect([string $ server = ini_get(&#34; mysql.default_host&#34;)[,string $ username = ini_get(&#34; mysql.default_user&#34;)[,string $ password = ini_get (&#34; mysql.default_password&#34;)[,bool $ new_link = false [,int $ client_flags = 0]]]]])

打开或重用与MySQL服务器的连接。