donglu3087 2016-06-30 09:16
浏览 57
已采纳

mysqli_fetch_query返回boolean [duplicate]

Good day, Im trying read all records from my table but I keep getting this error

message: 'Warning: mysql_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

here is my code :

                 $conn =  mysqli_connect("localhost", "root", "") or die                (mysql_error()); //Connect to server
$username = mysqli_real_escape_string($conn,$_POST['username']);
$password = mysqli_real_escape_string($conn,$_POST['password']);
$bool = true;
             //mysql_connect("localhost", "root", "") or die(mysql_error());// connects to the server
            mysqli_select_db($conn,"first_db") or die("Cannot connect to the database");//connects to the db
            $query = ("select * from user");//
            $result = mysqli_query($conn,$query);//query the user table

            while($row = mysqli_fetch_array($result))//display all rows from the query
            {
            $table_users = $row['username'];//the first username row is passed on to $table_users,and so on until the query is finished
             if($username == $table_users)//checks if there are any  mixing fields
                {
                 $bool = false;//sets bool to false
                 Print '<script>alert("Username has been taken!")                                </script>';//prompts the user
                Print '<script>window.location.assign("register.php")</script>';//redirects user to register.php

                }

               }
                 if($bool)//sets bool to true
                   {
                    mysqli_query($conn,"INSERT INTO user (username,password) VALUES ('$username','$password')");//inserts the value to table users
                    Print '<script>alert("Successfully Registered!")    </script>';//prompts the user
                    Print '<script>window.location.assign("register.php")</script>';//redirects user to register.php
                   }

                    echo "Username entered is: ". $username. "<br/>";
                    echo "Password entered is: ". $password;
                   }
                  ?>


  • 写回答

1条回答 默认 最新

  • dswfyq6201 2016-06-30 09:20
    关注

    This is because you mixing mysql and mysqli.

    This is problem with database connection

    You have to write connection like this

    $conn =  mysqli_connect("localhost", "root", "","first_db"); 
    

    Instead of this:

    $conn =  mysqli_connect("localhost", "root", ""); 
     mysqli_select_db($conn,"first_db") or die("Cannot connect to the database");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法