mysqli_query没有显示任何结果

时间:2014-12-25 05:51:38

标签: php mysql

我想从MySQL表中检索与特定电子邮件匹配的数据。此电子邮件中有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
}

?>

5 个答案:

答案 0 :(得分:0)

下面:

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

这实际上并没有输出值,你可以使用echo来输出变量的值,或者像这样使用PHP短标记,

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

或者使用echo

的漫长道路
<p><?php echo $fullname; ?></p>

答案 1 :(得分:0)

要检查您是否已连接到数据库,请尝试以下代码并进行测试,同时检查您是否使用mysqli_fetch_array($sql)

print_r中获取数据?
    <?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
         }
    }
 ?>

答案 2 :(得分:0)

我不知道问题出在哪里,但我想帮助你一点:

<?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
   }
?>

答案 3 :(得分:0)

选择database请尝试

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

答案 4 :(得分:0)

成功完成数据库连接后请检查一下吗?

mysqli_connect(DATABASE_HOST,DATABASE_USERNAME,DATABASE_PASSWORD,DATABASE_NAME)或死亡('无法与服务器连接'。);

因为你写的查询是对的。