简体   繁体   English

mysqli_query没有显示任何结果

[英]mysqli_query is not displaying any results

I want to retrieve data matching a specific email from a MySQL table. 我想从MySQL表中检索与特定电子邮件匹配的数据。 There are 2 records associated with this email, but nothing is printed on my screen. 该电子邮件有2条记录,但我的屏幕上没有打印任何内容。

<?php

$conn = mysqli_connect('localhost', 'root', '', 'testdrive');


$sql = mysqli_query($conn, 
        'SELECT * FROM products_bought
        where email = "'.mysqli_real_escape_string($conn, "a@gmail.com").'"'
    );
while($row = mysqli_fetch_array($sql)){
    $fullname = $row['fullname'];
    ?>
    <p><?php echo $fullname; ?></p>
    <?php
}

?>

Here: 这里:

<p><?php $fullname; ?></p>

This is not actually outputting a value, you can either use echo to output the value of the variable, or use PHP short tags like so, 这实际上不是输出值,您可以使用echo输出变量的值,也可以使用PHP短标签,例如

<p><?= $fullname; ?></p>

Or the long way using echo 或者使用回声很长的路要走

<p><?php echo $fullname; ?></p>

To Check that you are connected to DB or not please try below code and test, also check that are you getting data in mysqli_fetch_array($sql) using print_r 要检查您是否已连接到数据库,请尝试以下代码并进行测试,还要检查是否正在使用print_rmysqli_fetch_array($sql)获取数据。

    <?php

    $conn = mysqli_connect('localhost', 'root', '', 'testdrive');

    if(mysqli_errno($conn))
    {

        echo"sum error";
    }
    else
    {
      $email = mysqli_real_escape_string($conn, "a@gmail.com");
      $sql = mysqli_query($conn, 'SELECT * FROM products_bought where email = "'.$email.'"'
        );
         while($row = mysqli_fetch_array($sql)){
          $fullname = $row['fullname'];
        ?>
        <p><?php echo $fullname; ?></p>
        <?php
         }
    }
 ?>

I don't know where the problems came from, but i want to help you a bit : 我不知道问题来自何处,但我想为您提供一些帮助:

<?php
   $conn = mysqli_connect('localhost', 'root', '', 'testdrive');
   $sql = mysqli_query($conn,'SELECT * FROM products_bought
    where email = "'.mysqli_real_escape_string($conn, "a@gmail.com").'"');

   if($sql->num_rows > 0){ // Check existence of the data that received
      while($row = mysqli_fetch_array($sql)){
        $fullname = $row['fullname'];
        echo "<p>".$fullname."</p>"; // Why just echoing it in one <?PHP ?>
      }
   } else {
      // If no data sent. then check your db, cause the problems was in your db
   }
?>

the select database please try 选择database请尝试

   $conn = mysqli_connect('localhost', 'root', '');
    $db = mysqli_select_db('testdrive');

Please check once your database connection done successfully ? 一旦数据库连接成功完成,请检查?

mysqli_connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD,DATABASE_NAME) or die('Could not connect with server.'); mysqli_connect(DATABASE_HOST,DATABASE_USERNAME,DATABASE_PASSWORD,DATABASE_NAME)或die('无法与服务器连接。');

because query you have written is right. 因为您编写的查询是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM