mysqli_fetch_query返回布尔值

时间:2016-06-30 09:16:03

标签: php mysql

美好的一天,我正在尝试阅读我桌上的所有记录,但我一直收到错误

  

message:'警告:mysql_fetch_array()需要参数1   mysqli_result,在

中给出的布尔值

这是我的代码:            

                 $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 个答案:

答案 0 :(得分:0)

这是因为你混合了mysql和mysqli。

这是数据库连接的问题

你必须写这样的连接

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

而不是:

$conn =  mysqli_connect("localhost", "root", ""); 
 mysqli_select_db($conn,"first_db") or die("Cannot connect to the database");