gpt4 book ai didi

php - 使用mysqli从数据库获取数据

转载 作者:行者123 更新时间:2023-11-29 23:27:57 25 4
gpt4 key购买 nike

我有一个问题要从数据库中获取数据。

$_POST['search']Pizza

这是PHP的输出:

Debug :Pizza <br>
SELECT * FROM products WHERE name LIKE '%Pizza%';<br>
some test<BR>
0<BR>


这是PHP代码:

    <?php
error_reporting(E_ALL); ini_set('display_errors', 1);
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

sec_session_start();
if (login_check($mysqli) == true)
{
$out = "";
$num = "";
if(isset($_POST['search'])){
$search = "%".$_POST['search']."%";
echo "Debug :".$search."<br>";
$sql = "SELECT * FROM products WHERE name LIKE '%".$search."%';";
echo $sql."<br>";

if ($stmt->prepare("SELECT * FROM products WHERE name LIKE ?"))
{
$stmt->bind_param("s", $search);
$stmt->execute();
echo "some test<br>";
$reslut = $stmt->get_result();
if (($num = $stmt->num_rows)>0)
{
$out = "<table border=\"3\">";
$out .= "<tr><td>ID </td><td>name </td><td>kacl for 100g </td><td>one portion </td><td>kcal for one portion </td></tr>\n\r";
while ($row = $reslut->fetch_assoc())
{
$out .= sprintf ("<tr><td>%s </td><td>%s </td><td>%s </td><td>%s </td><td>%s </td></tr>\n\r", $row["id"], $row["name"], $row["p100g_kcal"], $row["port_name"], $row["port_kcal"]);
//You could echo a div in here with the data you want to be displayed
}
$out .= "</table>";
}
else
{
$out ="keine Lebensmittel gefunden!";
}
echo $num;

}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Search products</title>
<link rel="stylesheet" href="styles/main.css" />
</head>
<body>
<?php if (login_check($mysqli) == true) : ?>
<p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p>

<h3>Search Produckt Details</h3>
<p>You may search only part for example "Pizza" instead of "Salami Pizza"</p>
<form method="post" action="search.php" id="searchform">
<input type="text" name="search">
<input type="submit" name="submit" value="Search">
</form>
<?php
if(isset($_POST['search']))
{
echo "found ". $num ." data rows:<br>";
echo $out;
}
?>
<?php else : ?>
<p>
<span class="error">You are not authorized to access this page.</span> Please <a href="login.php">login</a>.
</p>
<?php endif; ?>
</body>
</html>


运行它之后, $out$num变量为空,但是随后我在 SELECT中运行 phpmyAdmin命令,我得到了22行。

最佳答案

在这里检查

$search = "%".$_POST['search']."%";
echo "Debug :".$search."<br>";
$sql = "SELECT * FROM products WHERE name LIKE '%".$search."%';";
echo $sql."<br>";


它将输出为

调试:%pizza%
选择*从名称类似'%% pizza %%'的产品中;

因此,请更改您的代码并使其完全使用。

关于php - 使用mysqli从数据库获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807056/

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