2

Really, it drive me crazy when I can't detect what error that happens. I can't handle it.

I managed to make a connection to MySQL, and check it out with:

$connection = mysqli_connect(HOST, USER, PASS, DB) or die('Could not connect!');
if($connection){
    echo 'It's connected!';
}

Yeah, that say connected. Then, when I try a query, it fails without error reporting. I've tried do this to check if it fails:

$query = "SELECT $field FROM users WHERE id = ".$_SESSION['user_id'];
$result = mysqli_query($dbc, $query);
if($result){
    echo 'Query OK';
}else{
    echo 'Query failed';
}

The browser said: Query failed. So, there's an error in my query. Then I echoed the query out with this:

echo $query;
// Printed in the browser: SELECT firstname FROM users WHERE id = 1

Copy that value and use it in phpMyAdmin. It works. So, i guess an error occured in mysqli_query function. But i can't get the error message and so i don't know what's going on. I've tried this:

$result = mysqli_query($dbc, $query) or die(mysqli_error($dbc));

and this:

if(!$result){
     echo mysqli_error($dbc);
}

Nothing happens. The browser just blank. Then, I tried to change this:

$result = mysqli_query($dbc, $query);

to this:

$result = mysqli_query($query);

Still nothing happens. What's going on? How can I know what error occured?

I run the server in Debian with phpinfo(): PHP Version 5.4.36-0+deb7u3

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mas Bagol
  • 4,377
  • 10
  • 44
  • 72
  • Have you checked your `error_reporting` and `display_errors` settings? Also, are you not debugging on your machine, but on a remote server? :/ – Andrea Feb 03 '15 at 01:01
  • @Andrea Sorry, how to check those settings? I'm editing on a remote server. Actually, I remote my server that installed in VirtualBox. – Mas Bagol Feb 03 '15 at 15:23
  • Have you seen: [http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php](http://stackoverflow.com/q/845021/367456)? Did you enable error logging in both your webserver and PHP? Have you checked your webservers error log and PHP's error log? – hakre Mar 20 '15 at 11:41

1 Answers1

-2

Guys read what they have posted, they are mixing the connections up.

$connection = mysqli_connect(HOST, USER, PASS, DB) or die('Could not connect!');

So this line should read:

$result = mysqli_query($connection, $query) or die(mysqli_error($connection));

With mysqli_ you need to pass the connection to a number of things that was not required in mysql_

Or you can switch your original db connect to $dbc

 $dbc = mysqli_connect(HOST, USER, PASS, DB) or die('Could not connect!');

I hope this helps!

AJF
  • 11,767
  • 2
  • 37
  • 64
Peter
  • 9