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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀