gpt4 book ai didi

php - 在我的 php 脚本中,我与服务器的连接有效,但我的 sql 查询没有

转载 作者:行者123 更新时间:2023-11-29 01:20:16 24 4
gpt4 key购买 nike

连接有效,我没有收到连接错误。但是当我运行脚本时,我得到一个 undefined index 错误并且它输出“0 个结果”,尽管我的表肯定被填充并且我正在搜索我知道在表中的东西。

我使用 MySQL Workbench 管理数据库,使用 Apache (xampp) 托管本地服务器并运行 PHP 脚本。这可能是问题所在吗?有没有办法让我在与 apache 网站相同的地方托管数据库?

$sql="SELECT * FROM book_table WHERE Title LIKE $input OR Author LIKE $input OR Barcode LIKE $input";
$result = $conn->query($sql);

if ($result) {

while($row = $result->fetch_all()) {
echo "<br>Title: " . $row["Title"]. " - Author: " . $row["Author"];
}
} else {
echo " <br> 0 results";
}

最佳答案

我给你的建议是PDO :

$dsn = 'mysql:host=localhost;dbname='.$dbname;//$dbName is the name of your database
$user = 'root';
$pass = '123';//use your login information here
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM book_table WHERE Title LIKE :info OR Author LIKE :info OR Barcode LIKE :info";
$ps = $db->prepare($query);
$ps->bindValue(':info', $input)
$ps->execute();
$result = $ps->fetchAll(PDO::FETCH_ASSOC);

//iterate over result
if (!empty($results)){
foreach ($result as $row) {
echo "<br>Title: " . $row["Title"]. " - Author: " . $row["Author"];
}
} else {
echo " <br> 0 results";
}

此外,请记住以正确的方式使用 MySQL LIKE。当你想匹配字符串的一部分时,你需要使用%符号。

例如:

SELECT * FROM book_table WHERE Title LIKE "%goodbook%"

它将返回标题中包含“goodbook”的所有行。

关于php - 在我的 php 脚本中,我与服务器的连接有效,但我的 sql 查询没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40307649/

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