0

Actually I am trying to use MySqli but its is not working for me even it is showing that database is connected but query is not working even not giving any error. I tried using the code bellow:

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();   
    }
  • 2
    What do you mean it's not working. You just said query works and database is connected – Rotimi Oct 04 '17 at 09:44
  • Sorry I mean database connected but query is not working.. That was typo I fixed in the question too. – SimulationCode Oct 04 '17 at 09:47
  • Try mysqli_connect( "127.0.0.1", "dbu", "password", "dbname"); and remove mysqli_select_db() – shashi Oct 04 '17 at 09:51
  • If you do a `die(var_dump($num))` right after `$num = mysqli_num_rows($query);` what do you get? – apokryfos Oct 04 '17 at 10:04
  • Possible duplicate of [How to get MySQLi error information in different environments? / mysqli\_fetch\_assoc() expects parameter 1 to be mysqli\_result](https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments-mysqli-fetch-as) – Dharman Jul 05 '19 at 21:12

6 Answers6

0

You have an issue with how you select the database. First parameter is the link while second is the name

mysqli_select_db($connect, 'db_name');

Alternatively you could pass the database as the fourth parameter as

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

Also the same issue is with the query

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

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

Where $query is the query itself. Also do not forget to use prepared statements when you have where clauses for where statements and also when you have insert values as well as delete

Rotimi
  • 4,783
  • 4
  • 18
  • 27
0

please check mysqli_query syntax before start to write code https://www.w3schools.com/PhP/func_mysqli_query.asp

Your select query having syntax problem, Please change like below,

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

May this will help

KMS
  • 566
  • 4
  • 16
0

Turn error on you have errors in mysqli_* functions. change your code to

$connect = mysqli_connect("localhost", 'dbu', 'password');
if ($connect) {
    echo 'connected';
} else {
    echo 'Failed:'.mysqli_connect_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($connect);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
B. Desai
  • 16,414
  • 5
  • 26
  • 47
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();

It's Work for me please check

Abhijit
  • 931
  • 1
  • 9
  • 21
0

Try using this.

$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();
  }
Himanshu Ray
  • 38
  • 1
  • 12
0

Hi Order of your method pparameters are wrong Try this

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);
        }
User123456
  • 2,492
  • 3
  • 30
  • 44