0

I've got a rather simple script here that I can't get to work:

<?php
    $host='localhost';
    $user='root';
    $passwd='password';
    $db='cherry_pie';

    echo 'Accessing database...<br/>';
    $mysql_conn = new mysqli($host, $user, $passwd, $db);
    if($mysql_conn->connect_error) die($mysql_conn->connect_error);
?>

The result in my browser is simply:

Accessing database...

Furthermore, no change in the web page occured after transposing

if($mysql_conn->connect_error) die($mysql_conn->connect_error);

with

print_r($mysql_conn);

which leaves me to believe that $mysql_conn is never being created.


I'm currently running ubuntu 16.04 and I've already run

sudo apt install php-mysql
sudo apt install mysql-server
sudo apt install mysql-client

Furthermore, I've run mysql through the shell and can verify that the database 'cherry_pie' does indeed exist.

Obviously my apache2 server is up and running and I already have other html documents and php scripts that will run properly.

I've already spent hours searching the php and mysql documentation, and I've referred to several other posts on this site to no avail, so any help will be greatly appreciated.


EDIT

I've also tried using mysqli_connect() which did not work.

Here's what my error log has to say:

[Fri Jun 23 13:10:38.214070 2017] [:error] [pid 12584] [client 127.0.0.1:56418] PHP Fatal error: Uncaught Error: Class 'mysqli' not found in /var/www/html/php_test.php:8\nStack trace:\n#0 {main}\n thrown in /var/www/html/php_test.php on line 8, referer: http://localhost/

6
  • 1
    Have you checked your PHP error logs? You are missing a semicolon on this line $mysql_conn = new mysqli($host, $user, $passwd, $db). Commented Jun 23, 2017 at 17:49
  • $mysql_conn = new mysqli($host, $user, $passwd, $db), does this code contain semicolon at the end? Commented Jun 23, 2017 at 17:49
  • It does have a semi-colon, that was just a mis-copy. Commented Jun 23, 2017 at 17:50
  • Read And Then Respond To The Suggestions In Your PHP Error Log Commented Jun 23, 2017 at 17:54
  • Try adding some curly brackets...if ($mysqli->connect_error) { die('Connect Error: ' . $mysqli->connect_error); } Commented Jun 23, 2017 at 17:57

3 Answers 3

3

The symptoms you have indicates that your MySQLi library is not loaded by your current PHP build.

EDIT

While MySQL IS loaded, your mysqli_ module is NOT loaded. So please see the link above.

Sign up to request clarification or add additional context in comments.

3 Comments

You sir are my hero. I've been grappling with this since 7am this morning. I've now got MySQLi loaded and proper PHP error reporting. Thank you so much!
@AldenB glad to help, always check your error logs, they're very useful!
Honestly I didn't know that my PHP/MySQL errors would show up there; and yes, they are very useful!
0

You have misspelled $mysql_conn

    //Replace
        if($mysql_conn->connect_error) die(%mysql_conn->connect_error);
    //With
        if($mysql_conn->connect_error) die($mysql_conn->connect_error);

//He edited the post and corrected his spelling mistakes disregard this suggestion.

2 Comments

That was a mis-copy, my actual code has the '$'. I apologize for that.
Did you try echo (($mysql_conn->connect_error) ? "" : die($mysql_conn->connect_error));
0

EDIT

If you are creating a new mysqli connection object, you must have to follow its rules:

if($mysqli_conn->error) die($mysqli_conn->error);

2 Comments

The error log on the edited question clearly states that the mysqli class isn't there, so it's nothing to do with following the coding convention.
That's the bit to change. But reminding as he has it it's right. So i suggest you @AldenB to connect & verify using this way: $mysql_conn = new mysqli($host, $user, $passwd, $db) or die('Error connecting'); ... It it simple anyway it always return an only error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.