【发布时间】: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();
【问题讨论】: