Mysql_query错误

时间:2015-07-28 08:16:24

标签: php mysql mysqli

<?php
    $db = new mysqli("XXXXXX","XXXXXXX","XXXXXX","XXXXXXX");

    $strSQL = $db->query('SELECT * FROM VotEndListView ');
    $objQuery = mysqli_query($strSQL, $db);
    $intNumField = mysqli_field_count($objQuery);
    $resultArray = array();
    while($obResult = mysqli_fetch_array($objQuery))
    {
        $arrCol = array();
        for($i=0;$i<$intNumField;$i++)
        {
            $arrCol[mysqli_fetch_field_direct($objQuery,$i)] = $obResult[$i];
        }
        array_push($resultArray,$arrCol);
    }

    mysqli_close($db);

    echo json_encode($resultArray);
?>

这是错误信息:

  

警告:mysqli_query()要求参数1为mysqli,第5行/home/u528666799/public_html/vote/getGallery1.php中给出的对象

     

警告:mysqli_field_count()期望参数1为mysqli,在第6行的/home/u528666799/public_html/vote/getGallery1.php中给出为null

     

警告:mysqli_fetch_array()要求参数1为mysqli_result,在第8行/home/u528666799/public_html/vote/getGallery1.php中给出null   []

4 个答案:

答案 0 :(得分:0)

替换你的

$strSQL = $db->query('SELECT * FROM VotEndListView ');

$strSQL = 'SELECT * FROM VotEndListView';

答案 1 :(得分:0)

<?php
    $db = new mysqli("XXXXXX","XXXXXXX","XXXXXX","XXXXXXX");

    $strSQL = 'SELECT * FROM VotEndListView'; // NEED TO BE A STRING
    $objQuery = mysqli_query($db,$strSQL); // It works like ($db,String)
    $intNumField = mysqli_field_count($objQuery);
    $resultArray = array();
    while($obResult = mysqli_fetch_array($objQuery))
    {
        $arrCol = array();
        for($i=0;$i<$intNumField;$i++)
        {
            $arrCol[mysqli_fetch_field_direct($objQuery,$i)] = $obResult[$i];
        }
        array_push($resultArray,$arrCol);
    }

    mysqli_close($db);

    echo json_encode($resultArray);
?>

您的问题是mysqli_query。它的作用类似于mysqli_query($database, the query AS A STRING)

答案 2 :(得分:0)

使用mysqli_connect()

<?php
    $db = mysqli_connect("localhost","u528666799_park","class2558","u528666799_park");

    $strSQL = "SELECT * FROM VotEndListView ";
    $objQuery = mysqli_query($strSQL, $db);
    $intNumField = mysqli_field_count($objQuery);
    $resultArray = array();
    while($obResult = mysqli_fetch_array($objQuery))
    {
        $arrCol = array();
        for($i=0;$i<$intNumField;$i++)
        {
            $arrCol[mysqli_fetch_field_direct($objQuery,$i)] = $obResult[$i];
        }
        array_push($resultArray,$arrCol);
    }

    mysqli_close($db);

    echo json_encode($resultArray);
?>

mysqli_query()

答案 3 :(得分:0)

您的代码中有一些混淆,首先使用mysqli OO机制进行连接,然后使用过程代码进行与mysqli的所有其他交互。坚持一个或另一个。

由于某种原因,您还会执行两次执行查询。

您的代码可以归结为这个简单的替代方案,包括将一些有用的状态信息传递给您的json数据结构,这样您的javascript就会知道如果此代码出现问题会发生什么。

<?php
    $db = new mysqli("XXXXXX","XXXXXXX","XXXXXX","XXXXXXX");

    $result = $db->query('SELECT * FROM VotEndListView');

    $to_json = array();

    if ( ! $result ) {
        $to_json['status'] = 'FAILED';
        $to_json['error'] = $db->error;

    } else {
        $to_json = $result->fetch_all(MYSQLI_ASSOC))
        $to_json['status'] = 'OK';
        $to_json['error'] = '';
    }

    echo json_encode($to_json);

    mysqli_close($db);

    exit;
?>