【问题标题】:Error When Trying To Add A Variable In A Php Prepared Statement尝试在 PHP 准备语句中添加变量时出错
【发布时间】:2019-02-09 17:21:20
【问题描述】:

有人可以告诉我如何更正这个不断给我 sql 语法错误的查询吗?

致命错误:未捕获的 mysqli_sql_exception:您的文件中有错误 SQL 语法;检查与您的 MariaDB 服务器相对应的手册 在“?”附近使用正确语法的版本在第 1 行 C:\xampp\htdocs\test\browsing_histories.php:38 堆栈跟踪:#0 C:\xampp\htdocs\test\browsing_histories.php(38): mysqli->query('SELECT * FROM b...') #1 {main} 在第 38 行的 C:\xampp\htdocs\test\browsing_histories.php 中抛出

我需要这样做,这样我就不必编写数百个查询而是一个。数百个查询,每个查询都针对每个列查询。例如。

$total_pages = $conn->query("SELECT * FROM browsing_histories WHERE USERNAME = ? ")->num_rows; 

$total_pages = $conn->query("SELECT * FROM browsing_histories WHERE FIRST_NAME = ? ")->num_rows; 

$total_pages = $conn->query("SELECT * FROM browsing_histories WHERE GENDER = ? ")->num_rows; 

$total_pages = $conn->query("SELECT * FROM browsing_histories WHERE AGE-RANGE = ? ")->num_rows; 

等等……

一个查询应该像我们对未准备好的语句一样进行。因此,它是这样的吗:

$stmt = $conn->prepare('SELECT * FROM browsing_histories WHERE $query_type = ? 
ORDER BY id LIMIT ?,?'); 

完整上下文:

$query_type = $_GET['query_type']; 

//If $_GET['query_type']) is empty then show error as it must be set. 
if(!isset($_GET['query_type']) && empty($_GET['query_type'])) 
{ 
    echo "Invalid Query!"; 
} 
//If $_GET['query_type']) is full with value other than wild-card or "All" 
or "all" then make speccific query. 
elseif($query_type != 'all' OR $query_type != 'All' OR $query_type != '*') 
{ 
    ${$query_type} = $_GET[$_GET['query_type']]; 
    $followed_word = ${$query_type}; //Same as: $_GET[$_GET['query_type']]; 

    $total_pages = $conn->query("SELECT * FROM browsing_histories WHERE 
$query_type = ?")->num_rows; 
    //Make the query. 
    $stmt = $conn->prepare('SELECT * FROM browsing_histories WHERE 
$query_type = ? ORDER BY id LIMIT ?,?'); 
    $stmt->bind_param('sii', $query_type, $calc_page, $num_results_on_page); 
} 
else //Make general query or query for all records. 
{ 
    //Grab "all records" from the table. 
    //Get the total number of records from the table: 
"browsing_histories". 
    $total_pages = $conn->query("SELECT * FROM browsing_histories")- 
>num_rows; 
    //Make the query. 
    $stmt = $conn->prepare('SELECT * FROM browsing_histories ORDER BY id 
LIMIT ?,?'); 
    $stmt->bind_param('ii', $num_results_on_page); 
} 

$stmt->execute(); 

注意:即使我将准备好的语句查询中的 $query_type 更改为实际的列名,例如“用户名”,我仍然会收到相同的错误。

$stmt = $conn->prepare('SELECT * FROM browsing_histories WHERE username = 
? 
ORDER BY id LIMIT ?,?'); 

编辑 1: 修复了我的代码,但没有运气!

$query_type = $_GET['query_type']; 

//If $_GET['query_type']) is empty then show error as it must be set. 
if(!isset($_GET['query_type']) && empty($_GET['query_type'])) 
{ 
    echo "Invalid Query!"; 
} 
//If $_GET['query_type']) is full with value other than wild-card or 
"All" or "all" then make speccific query. 
elseif($query_type != 'all' OR $query_type != 'All' OR $query_type != 
'*') 
{ 
    ${$query_type} = $_GET[$_GET['query_type']]; 
    $followed_word = ${$query_type}; //Same as: 
$_GET[$_GET['query_type']]; 

    $total_pages = $conn->query("SELECT * FROM browsing_histories WHERE 
username = ?")->num_rows; 
    //Make the query. 
    $stmt = $conn->prepare('SELECT * FROM browsing_histories WHERE 
username = ? ORDER BY id LIMIT ?,?'); 
    $stmt->bind_param('sii', $query_type, $calc_page, 
$num_results_on_page); 
} 
else //Make general query or query for all records. 
{ 
    //Grab "all records" from the table. 
    //Get the total number of records from the table: 
"browsing_histories". 
    $total_pages = $conn->query("SELECT * FROM browsing_histories")- 
>num_rows; 
    //Make the query. 
    $stmt = $conn->prepare('SELECT * FROM browsing_histories ORDER BY id 
LIMIT ?,?'); 
    $stmt->bind_param('ii', $calc_page, $num_results_on_page); 
} 

$stmt->execute(); 

编辑 2: 这是我的最新更新。问题依然存在。注释掉的查询是我尝试过的。它们都显示相同的错误。 未注释掉的也显示错误。

    $query_type = $_GET['query_type']; 

    //If $_GET['query_type']) is empty then show error as it must be set. 
    if(!isset($_GET['query_type']) && empty($_GET['query_type'])) 
    { 
        echo "Invalid Query!"; 
    } 
    //If $_GET['query_type']) is full with value other than wild-card or 
    "All" or "all" then make speccific query. 
    elseif($query_type != 'all' OR $query_type != 'All' OR $query_type != 
    '*') 
    { 
    ${$query_type} = $_GET[$_GET['query_type']]; 
    $followed_word = ${$query_type}; //Same as: 
    $_GET[$_GET['query_type']]; 

    //$total_pages = $conn->query("SELECT * FROM browsing_histories WHERE 
    ? = ?")->num_rows; 
    //$total_pages = $conn->query("SELECT * FROM browsing_histories WHERE 
    \"$query_type\" = ?")->num_rows; 
    $total_pages = $conn->query("SELECT * FROM browsing_histories WHERE 
    $query_type = ?")->num_rows; 
    $stmt->bind_param('i', ${$query_type}); 
    $stmt->execute(); 
    //Make the query. 
    //$stmt_2 = $conn->prepare("SELECT * FROM browsing_histories WHERE ? 
    = ? ORDER BY id LIMIT ?,?"); 
    //$stmt_2 = $conn->prepare("SELECT * FROM browsing_histories WHERE 
    \"$query_type\" = ? ORDER BY id LIMIT ?,?"); 
    //$stmt_2 = $conn->prepare("SELECT * FROM browsing_histories WHERE 
    $query_type = ? ORDER BY id LIMIT ?,?"); 
    $stmt_2 = $conn->prepare("SELECT * FROM browsing_histories WHERE 
    username = ? ORDER BY id LIMIT ?,?"); 

    //$stmt_2->bind_param('sii', $query_type, ${$query_type}, $calc_page, 
    $num_results_on_page); 
    $stmt_2->bind_param('sii', ${$query_type}, $calc_page, 
    $num_results_on_page); 
    } 
    else //Make general query or query for all records. 
    { 
        //Grab "all records" from the table. 
        //Get the total number of records from the table: 
        "browsing_histories". 
        $total_pages = $conn->query("SELECT * FROM browsing_histories")- 
        >num_rows; 
        //Make the query. 
        $stmt = $conn->prepare('SELECT * FROM browsing_histories ORDER BY 
        id LIMIT ?,?'); 
        $stmt->bind_param('ii', $calc_page, $num_results_on_page); 
        } 

        $stmt->execute(); 

【问题讨论】:

    标签: php mysql sql mariadb


    【解决方案1】:

    两个参数需要两个值

    $stmt->bind_param('ii', $num_results_on_page, $second_value); 
    

    对于这 3 个参数 3 个值

    $stmt = $conn->prepare('SELECT * FROM browsing_histories WHERE username = ? 
    ORDER BY id LIMIT ?,?'); 
    
    $stmt->bind_param('sii',$your_username_value, $num_results_on_page, $second_value); 
    

    并检查代码

     $total_pages = $conn->query("SELECT * FROM browsing_histories WHERE username = ?")->num_rows;
    

    似乎也没有参数

    【讨论】:

    • ScaisEdge,我在原始帖子中添加了“编辑 1”,将脚本更新为您的建议。但没有运气。
    • 检查代码`$total_pages = $conn->query("SELECT * FROM browse_histories WHERE username = ?")->num_rows; ` 似乎也没有参数
    • 我已经检查过了。错误说问题出在这一行: $stmt = $conn->prepare("SELECT * FROM browse_histories WHERE username = ? ORDER BY id LIMIT ?,?");似乎它不喜欢:“WHERE username = ?”。
    • 我更新的代码在 EDIT 2 中。您在 EDIT 2 上看到的所有这些实验也都失败了。关于查询语法错误的相同错误问题仍然存在。我修改的原始教程代码在这里:codeshack.io/how-to-create-pagination-php-mysql 这个教程有问题吗?我是 php 初学者和编程初学者。
    • 这是显示错误的第二个查询。第一个查询查询记录数。第二个查询查询数据库的结果。无论我编码第二个查询有多少不同的方式,错误总是指向第二个查询。我尝试了以下所有方法,但我得到了原始帖子中提到的相同错误。检查我尝试过的所有各种 stmt_2(第二次查询)。在我的原始帖子中检查“编辑 2”。非常感谢您尝试解决我的问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2020-02-03
    • 1970-01-01
    • 2020-05-03
    • 2017-09-15
    • 2014-03-08
    • 1970-01-01
    相关资源
    最近更新 更多