有人可以告诉我如何纠正这个不断给我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();
答案 0 :(得分:0)
两个参数需要两个值
$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;
似乎也没有参数