dtqjbbr5283 2014-08-20 20:00
浏览 19
已采纳

显示在php中获取mysql数据的结果

I'm not sure if I doing the connection and query 100%, but since the error I'm getting is only with the results I'm assuming I am. I don't, however, know how to process the data correctly to display it.

php:

$query = SELECT * FROM `user` WHERE `email` = 'mflem@yahoo.com' AND 'password' = 'password'

$db = new mysqli('localhost','root','','xxxxxxx') or die('unable to connect!');
if($db->connect_errno){
   $message = $db->connect_error;
} else{
   if($result = $db->query($query)){
       $result->close();
       while($row = mysqli_fetch_assoc($result)){
          echo "result: " . $row['user_Id'];
       }
       return $result;
   } else{
       $message = $db->error;
       return $message;
   }
}
$db->close();

The error in the browser console it says.


Warning: mysqli_fetch_assoc(): Couldn't fetch mysqli_result in C:\xampp\htdocs\apps\MyVyn\Utils\utils\servConn.php on line 14

Notice: Undefined variable: return in C:\xampp\htdocs\apps\MyVyn\Utils\utils\servConn.php on line 36

Could someone point out what I'm missing? Also are there any better ways of fetching data from you DB and displaying the results?

  • 写回答

1条回答 默认 最新

  • doutou7549 2014-08-20 20:04
    关注

    Edit:

    Me: "You're not selecting anything."

    OP: "These is, I didn't notice i didn't include it."

    Always post full code when posting. When something is omitted, it leaves room for confusioin.


    You're not selecting anything.

    Meaning, you need to select a column/columns or everything * from a database table.

    Example:

    $query = mysqli_query($db,"SELECT column FROM table_name");
    

    or

    $query = mysqli_query($db,"SELECT column_1, column_2 FROM table_name");
    

    or

    $query = mysqli_query($db,"SELECT * FROM table_name");
    

    Yet, I suggest you look into use prepared statements, or PDO with prepared statements, there are many examples you can use there to base yourself on.


    Edit: #2

    You're closing your DB connection prematurely with $db->close(); in:

    else{
       if($result = $db->query($query)){
           $result->close();
           while($row = mysqli_fetch_assoc($result)){
              echo "result: " . $row['user_Id'];
           }
           $db->close(); // <= there
           return $result;
       } else{
           $message = $db->error;
           $db->close(); // <= there
           return $message;
       }
    }
    

    Remove the $result->close(); from inside all your loops and place it outside of a successful query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP