gpt4 book ai didi

php - SQLSTATE[HY093] : Invalid parameter number: parameter was not defined : PHP PDO Object

转载 作者:行者123 更新时间:2023-11-30 00:52:38 30 4
gpt4 key购买 nike

我知道这个错误之前已得到解答,但我无法解决我的代码中的问题。

这是我的代码:

//Basic SELECT query
$select = 'SELECT id, joketext';
$from = ' FROM joke';
$where = ' WHERE TRUE';

$placeholders = array();

if ($_GET['author'] != ' ') //author is selected
{
$where .= 'AND authorid = :authorid';
$placeholders[':authorid'] = $_GET['author'];
}

if ($_GET['category'] != ' ') //category is selected
{
$from .= 'INNET SELECT jokecategory ON id = jokeid';
$where .= 'AND categoryid = :categoryid';
$placeholders[':categoryid'] = $_GET['category'];
}

if ($_GET['text'] != ' ') //text was specified
{
$where .= 'AND joketext LIKE :joketext';
$placeholders[':joketext'] = '%' . $_GET['text'] . '%';
}

try
{
$sql = $select . $from . $where;
$s = $pdo->prepare($sql);
$s->execute($placeholders);
}
catch (PDOException $e)
{
$error = 'Error fetching jokes.' . $e->getMessage();
include 'error.php';
exit();
}

如果有人向我解释为什么会出现此错误,我将不胜感激?谢谢!

最佳答案

您需要在 $where$from 的每次添加之前添加空格。

if ($_GET['author'] != ' ') //author is selected
{
$where .= ' AND authorid = :authorid';
$placeholders[':authorid'] = $_GET['author'];
}

if ($_GET['category'] != ' ') //category is selected
{
$from .= ' INNER JOIN jokecategory ON id = jokeid';
$where .= ' AND categoryid = :categoryid';
$placeholders[':categoryid'] = $_GET['category'];
}

if ($_GET['text'] != ' ') //text was specified
{
$where .= ' AND joketext LIKE :joketext';
$placeholders[':joketext'] = '%' . $_GET['text'] . '%';
}

如果您不这样做,您会得到如下所示的查询:

SELECT id, joketext FROM joke WHERE TRUEAND authorid = :authoridANDjoketext = :joketext

关于php - SQLSTATE[HY093] : Invalid parameter number: parameter was not defined : PHP PDO Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20829796/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com