mysql_fetch_assoc():6不是有效的MySQL结果资源

时间:2014-04-08 03:21:44

标签: php mysql

尝试将某些信息查询到数据库但似乎无法找到错误。我在PHP MySQL函数方面遇到了一些麻烦,但我会说大约90%的修复都是简单的语法错误。

        $TableName = "opportunities";
        $Opportunities = array();
        $SQLString = "SELECT opportunity_ID, company, city, " . 
         "start_date, end_date, position, description" . " FROM $TableName;";       
        $QueryResult = mysql_query($SQLString);

        if (mysql_num_rows($QueryResult) > 0) 
        {
            while (($Row = mysql_fetch_assoc($QueryResult)) !== FALSE) 
            {
                $Opportunities[] = $Row;
                mysql_free_result($QueryResult);
            }
        }

1 个答案:

答案 0 :(得分:0)

确保在建立连接时以及在查询之后添加检查

    $server = '127.0.0.1';
    $username = 'root';
    $password = '';
    $database = 'test';


mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

$TableName = "opportunities";
$Opportunities = array();
$SQLString = "SELECT opportunity_ID, company, city, " . 
 "start_date, end_date, position, description" . " FROM $TableName;";       
$QueryResult = mysql_query($SQLString);

if(mysql_error()) {
    die(mysql_error();
}

if($QueryResult) {

        while($Row = mysql_fetch_assoc($QueryResult)) 
        {
            $Opportunities[] = $Row;
        }
            mysql_free_result($QueryResult);
}

mysql_close();

此外,您在循环中释放结果$ QueryResult,因此下一次迭代将没有资源从中获取数据。