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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序