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/

AldenB
  • 133
  • 7

3 Answers3

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.

Martin
  • 19,815
  • 6
  • 53
  • 104
  • 1
    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 Jun 23 '17 at 18:14
  • @AldenB glad to help, always check your error logs, they're very useful! – Martin Jun 23 '17 at 18:15
  • Honestly I didn't know that my PHP/MySQL errors would show up there; and yes, they are very useful! – AldenB Jun 23 '17 at 18:19
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.
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);
Abax
  • 54
  • 6
  • 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. – Martin Jun 23 '17 at 18:04
  • 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. – Abax Jun 23 '17 at 18:12