php - 查询给我一个 bool 错误

标签 php mysql

拜托,我需要帮助!我已经遇到同样的问题 3 天了。我知道查询没有给我一个 bool 值 true 结果。我什至不知道它给了我什么。它给我的问题是:

Warning: mysqli_prepare() expects parameter 2 to be string, object given in C:\xampp\htdocs\caf\pages\log.php on line 48

我已经尝试将结果转换为字符串,但我发现问题出在这一行给我的输出中:

$query_pages_result = mysqli_query($connection, $get_pages_query)  or die($myQuery."<br/><br/>".mysql_error());

所以请有人帮我看看这一行是否能给我想要的结果,也就是看看我在表格记录中有多少页。提前致谢!

<?php

session_start();

// GET PAGES RECORD FROM LOG TABLE: *********| Only the first time though:
if (isset($_SESSION['log']) != 'logging')
{
    //  Here, just creating a string:
    $pages_record = '';
    $insert_query = '';
    //      Get saved pages from the database:
    $connection = mysqli_connect($mach,$userna,$paso,$db) or die ("Error in log-page script: AB-1 - query: $insert_query." . mysqli_error());
    mysqli_select_db($connection,'cafeteria');

    //      Query string to pull all pages from table record:
    $get_pages_query = "select *  from `log-page` order by `log-id`";

    //      Query the database, and save result:
    $query_pages_result = mysqli_query($connection, $get_pages_query)  or die($myQuery."<br/><br/>".mysql_error());

    //      Check number of results returned:
    $num_of_results = '';

    if ($stmt = mysqli_prepare($connection, $query_pages_result)) {
    $num_of_results = mysqli_num_rows($stmt);

    if ($num_of_results > 0)
    {
        // Loop through the result array: Each time, one row, and then the next one ...
        for ($row = 0; $row < $num_of_results; $row++ )
        {
            // Getting one row:
            $get_row = mysqli_fetch_array($query_pages_result);
            // Extracting just the page name from the row:
            $one_page = substr($get_row["page"],strripos($get_row["page"],"/") + 1);
            // Adding this page name to the string created previously:
            if ($row == 0)
            {
                $pages_record .= $one_page;
            }
            else 
            {
                $pages_record .= ",".$one_page;
            }           
        }

        // Once all pages have been read and saved to the string
        // now we save it to the session:
        $_SESSION['logpages'] = $pages_record;
        $_SESSION['log'] = 'logging'; // This just tells us, we are logging pages to the database.
    }
    else 
    {
        // There are no pages in the table:
        $_SESSION['logpages'] = "";
        $_SESSION['log'] = 'logging'; // This just tells us, we are logging pages to the database.
    }
}

// Check if page is already in session list.
$pages_array = array();
if (strlen(isset($_SESSION['logpages'])) > 0 )
{
    // string variable that holds all pages separated by commas:
    $pages_string =  $_SESSION['logpages'];

    // creating an Array to hold all pages already logged in server:
    if (strstr($pages_string, ","))
    {
        $pages_array = explode(",", $pages_string);
    }
    else // just means there's only one page in the record
    {
        // so, we push it inside the array.
        array_push($pages_array, $pages_string);
    }

    // current page: [ We are extracting only the page, not the entire url, Exmp: login.php ]
    $current_page = substr($_SERVER['PHP_SELF'],strripos($_SERVER['PHP_SELF'],"/") + 1);

    // Check if current_page is in the array already:
    if (!in_array($current_page, $pages_array))
    {
        // IF is NOT in the array, then add it:
        array_push($pages_array, $current_page);

        // Add it to the Session variable too:
        $pages_string = implode(",", $pages_array);

        // Re-save it to SESSION:
        $_SESSION['logpages'] = $pages_string;

        // Now, add it to the database table "log-page""
        $connection = mysqli_connect($mach,$userna,$paso,$db) or die ("Unable to connect!");
        mysqli_select_db($connection,'cafeteria');

        // Query to insert page description into the table:
        // [ date - time - page - user ]
        $insert_query = "INSERT INTO `log-page` 
            (`date`, `time`, `page`, `user`) VALUES 
            ('".date("Y-m-d")."', '".date("H:i:s")."', '".$_SERVER['PHP_SELF']."', '".(isset($_SESSION['SESSION_UNAME']))."')"; 
            mysqli_select_db($connection,'cafeteria');

        // INSERTING INTO DATABASE TABLE:
        mysqli_query($connection, $insert_query) or die ("Error in log-page script: AB-2 - query: $insert_query." . mysqli_error($connection));
        // Done!
    }
    else 
    {
        // IF it IS in the list, just SKIP.
    }
}
else 
{
    // means, that there are absolutely no pages saved in the database, basically this is the first log:
    $_SESSION['logpages'] = substr($_SERVER['PHP_SELF'],strripos($_SERVER['PHP_SELF'],"/") + 1);    

    // Now, add it to the database table "log-page""
    $connection = mysqli_connect($mach,$userna,$paso,$db) or die ("Unable to connect!");
    mysqli_select_db($connection,'cafeteria');

    // Query to insert page description into the table:
    // [ date - time - page - user ]
    $insert_query = "INSERT INTO `log-page` 
        (`date`, `time`, `page`, `user`) VALUES 
        ('".date("Y-m-d")."', '".date("H:i:s")."', '".$_SERVER['PHP_SELF']."', '".(isset($_SESSION['SESSION_UNAME']))."')"; 
    mysqli_select_db($connection,'cafeteria');
    // INSERTING INTO DATABASE TABLE:
    mysqli_query($connection,$insert_query) or die ("Error in log-page script: AB-2 - query: $insert_query." . mysqli_error($connection));
    // Done!    
}

}?>

最佳答案

问题出在这一行:

if ($stmt = mysqli_prepare($connection, $query_pages_result)) {

第二个变量为mysqli_prepare需要是实际的 SQL 查询,而不是使用 mysqli_query 执行查询的结果对象。你的逻辑可能与此相反。您应该准备语句,然后执行它。看起来您正在执行它,然后尝试准备它。

尝试删除 $query_pages_result = mysqli_query(... 行并将其余部分更改为:

if ($stmt = mysqli_prepare($connection, $get_pages_query)) {
    $stmt->execute();

但是,您的查询没有任何绑定(bind)参数,因此您实际上可以只使用 mysqli_query 并在这种情况下删除 mysqli_prepare - 您的偏好确实如此.

关于php - 查询给我一个 bool 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12936719/

相关文章:

PHP PDO 安全

Php不循环,在另一页仅提供一项,编辑

php - Symfony Doctrine 2 PostRemove 删除文件奇怪行为

php - MySQL 联结表查询(带或不带联结)

php - PHP 中的 Gettext 需要调用 setlocale(LC_MESSAGES, NULL)

PHP 5.5 (MS) SQL 服务器功能未按预期工作

mysql - 为什么我在这个 if 语句上遇到错误?

php - PHP中的SQL解析器?

php - MySQL - 按字符数排序

mysql - 选择一条固定线路和多条任意线路