Nick Fusaro
Nick Fusaro

Reputation: 42

Can't query MySQL Database

I'm trying to query a database using php5.6, I can't get this query to work, the error seems to be with

$response = @mysqli_query($dbc, $query) OR die('nope'. mysql_error());

This is my query php file:

<?php

require 'db.php';

$query = "SELECT * FROM USERS";

$response = @mysqli_query($dbc, $query) OR die('nope'. mysql_error());

    if ($response){
        echo 'Query successful!';
    } else {
        echo 'Error - query unsuccessful';
    }

?>

This is my db connection file:

<?php

error_reporting(E_ALL);

$DB_User = 'user';
$DB_Passwd = 'pass';
$DB_Host = 'localhost';
$DB_Name = 'myDB';

$dbc = mysqli_connect($DB_Host,$DB_User,$DB_Passwd,$DB_Name);
if (!$dbc) {
    die('Could not connect: ' . mysqli_error());
}

I've updated the files to all use mysqli and removed the @, but it's still not connecting or showing errors, still just throwing a 500. I'm not sure where to go from here...

Upvotes: 0

Views: 976

Answers (1)

samo0ha
samo0ha

Reputation: 3788

in this line you should use mysqli_connect() instead of mysql_connect()

$dbc = @mysql_connect($DB_Host,$DB_User,$DB_Passwd,$DB_Name)

and in case of @mysql_connect() your syntax will be like this

$dbc = @mysql_connect($DB_Host,$DB_User,$DB_Passwd);
if (!$dbc) {
    die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('foo', $dbc);
if (!$db_selected) {
    die ('Can\'t use database : ' . mysql_error());
}

// NOTE THAT

mysql is deprecated since 5.5 and deleted in php 7 so it is not recommended to use it

Upvotes: 1

Related Questions