Home Executing SELECT query return false
Reply: 2

Executing SELECT query return false

Ayan Banerjee
Ayan Banerjee Published in 2018-01-13 07:12:01Z

I have a PHP site that I uploaded in 000webhost. It needs a database to store data. But when I try to sign in it didn't sign in. When I check the connection it turns out the connection was fine. Still not able to access the database. So I wrote a small script to check database access and it failed. I have a table named songs which contains some songs.

Here is the script :


    $host = 'localhost';
    $user = 'xxxxxxx';
    $pass = 'xxxxxxx';
    $db = 'xxxxxxx';

    $timezone = date_default_timezone_set("Asia/Kolkata");

    $con = new PDO("mysql:host=$host;dbname=$db", $user, $pass); 

        echo "Connection success";
        $query  = mysqli_query($con, "SELECT * FROM songs");
            $tb = mysqli_fetch_array($query);
        else {
            echo " failed db access";
    else {
        echo " connection failed";

The details have been kept hidden for security reasons. The above script gives the following output:

Connection success failed db access
Borut Reply to 2018-01-13 09:59:48Z

It doesn't work, because you're using PDO for connection and mysqli to query. Change your connection to mysqli

$con = new mysqli($servername, $username, $password, $dbname);

or change your query to PDO

$query  = $con->query("SELECT * FROM songs");
Ravi Reply to 2018-01-13 09:38:39Z

The output you are showing is misleading, failed db access. As per below code, database was connected successfully and you were executing query, which didn't executed it seems and flow going to else block.

// your code 
else {
   echo " failed db access";

You need to check,

  1. If you are using same user credentials and has access to the table.
  2. If same table exists
  3. If you have provided correct database details where you have created tables.


As I mentioned earlier, user might not have sufficient privilege to access songs table. To GRANT access, you need to execute following SQL

GRANT ALL PRIVILEGES ON *.* TO '<username>'@'localhost; WITH GRANT OPTION;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.339328 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO