从数据库获取mysqli的数据

时间:2014-11-07 17:48:34

标签: php mysql

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

$_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行。

an image of phpmyAdmin

1 个答案:

答案 0 :(得分:0)

点击这里

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

它将输出为

调试:%pizza% SELECT * FROM products WHERE name LIKE&#39; %% pizza %%&#39;;

所以请更改您的代码并使其完整使用。