MySqLi由于某种原因无法正常工作

时间:2017-10-04 09:42:22

标签: php mysql mysqli

实际上我正在尝试使用MySqli,但它不适用于我,即使它显示数据库已连接但查询无法正常工作,甚至没有给出任何错误。我尝试使用下面的代码:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


    $connect = mysqli_connect("localhost",'dbu', 'password');
    if($connect){
    echo 'connected';   
    }
    else {
    echo 'Failed:'.mysqli_error();  
    }
    mysqli_select_db('dbname',$connect);

    if($query = mysqli_query("SELECT * FROM `users`")){
    $num = mysqli_num_rows($query);
    while($fetch = mysqli_fetch_array($query)){
    echo $id = $fetch['id'];
    }
    }
    else {
    echo 'error:'.mysqli_error();   
    }

6 个答案:

答案 0 :(得分:0)

您在选择数据库时遇到问题。第一个参数是链接,第二个是名称

mysqli_select_db($connect, 'db_name');

或者,您可以将数据库作为第四个参数传递为

$connect = mysqli_connect("localhost",'dbu', 'password','db_name');

同样的问题是查询

mysqli_query($connect, $query);//the first parameter is the link while second is the query itself 

$query = mysqli_query($connect, "SELECT * FROM `users`");

其中$ query是查询本身。当你有where语句的where子句时,以及当你有插入值以及删除时,也不要忘记使用预准备语句

答案 1 :(得分:0)

请在开始编写代码之前检查mysqli_query语法 https://www.w3schools.com/PhP/func_mysqli_query.asp

您的选择查询有语法问题,请更改如下,

$query = mysqli_query($connect, "SELECT * FROM `users`") //connection paramter missing 

愿这有用

答案 2 :(得分:0)

如果您在mysqli_*功能中出错,请转错。将您的代码更改为

$connect = mysqli_connect("localhost",'dbu', 'password');
    if($connect){
    echo 'connected';   
    }
    else {
    echo 'Failed:'.mysqli_error();  
    }
    mysqli_select_db($connect,'dbname'); // <-------change this

    if($query = mysqli_query($connect,"SELECT * FROM `users`")){ //<---------------change this
    $num = mysqli_num_rows($query);
    while($fetch = mysqli_fetch_array($query)){
    echo $id = $fetch['id'];
    }
    }
    else {
    echo 'error:'.mysqli_error();   
    }

答案 3 :(得分:0)

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    //Open a new connection to the MySQL server

    $mysqli = new mysqli('localhost','dbu','password','dbname');

    //Output any connection error
    if ($mysqli->connect_error) {
        die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    //MySqli Select Query
    $results = $mysqli->query("SELECT * FROM users");

    while($row = $results->fetch_assoc()) {
        echo $row["id"];
    }  


    // Frees the memory associated with a result
    $results->free();

    // close connection 
    $mysqli->close();

这对我有用,请检查

答案 4 :(得分:0)

尝试使用此功能。

$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

答案 5 :(得分:0)

你好方法的参数pparameters是错误的 试试这个

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

        $connect = mysqli_connect("localhost",'dbu', 'password');
        if($connect){
            echo 'connected';
        }
        else {
            echo 'Failed:'.mysqli_error($connect);
        }
        mysqli_select_db($connect,'dbname');

        if($query = mysqli_query($connect,"SELECT * FROM `users`")){
            $num = mysqli_num_rows($query);
            while($fetch = mysqli_fetch_array($query)){
                echo $id = $fetch['id'];
            }
        }
        else {
            echo 'error:'.mysqli_error($connect);
        }