Fatal error, can't connect to db

Hi,

I’m having problems with an example that I have been following in the book PHP Solutions. I know it can’t be the code because I eventually copied the downloaded code and copied it into my IDE just to make sure, but still ended up with the same problem when I refreshed the browser.

It’s two files, the first is called connection.inc.php and is located in the includes folder of the project. The second file is mysqli.php and located in the mysql folder.

When I type the mysqli.php file into the browser I end up with this ‘Fatal error: Call to a member function query() on a non-object in C:\wamp\www\mysql\mysqli.php on line 8’

(mysqli.php)

<?php
require_once('../includes/connection.inc.php');
// connect to MySQL
$conn = dbConnect('read');
// prepare the SQL query
$sql = 'SELECT * FROM images ORDER BY caption';
// submit the query and capture the result
$result = $conn->query($sql) or die(mysqli_error());
// find out how many records were retrieved
$numRows = $result->num_rows;
?>

(connection.inc.php)

<?php
function dbConnect($usertype, $connectionType = 'mysqli') {
  $host = 'localhost';
  $db = 'phpsols';
  if ($usertype  == 'read') {
	$user = 'psread';
	$pwd = 'michael';
  } elseif ($usertype == 'write') {
	$user = 'pswrite';
	$pwd = 'michael';
  } else {
	exit('Unrecognized connection type');
  }
  if ($connectionType == 'mysqli') {
	return new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');
  } else {
    try {
      return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
    } catch (PDOException $e) {
      echo 'Cannot connect to database';
      exit;
    }
  }
}

I’m not gonna lie, I’m absolutely at a loss with this problem. Had a quick look through the forums but couldn’t find anything. If anyone could suggest any ideas to me as to what is happening, that would be great as I’m getting really peed off with this…

Thanks in advance.

Is the MySQLi extension enabled in php.ini? In your php.ini file you’ll find three lines (though not necessarily right after each other):

extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_pdo_mysql.dll

If any of them three lines have a ; at the start, remove the ; save the file and restart the server.

That’s not the problem. If the extension wasnt installed, his object-creation would have caused a Class-Not-Exist fatal error prior to the query line.

I’m… not sure that this line works.
return new mysqli($host, $user, $pwd, $db) or die (‘Cannot open database’);
Because the return will always work successfully and never call the or?

Try seperating them out


$conn = new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');
return $conn;

I suspect that your mysql server is rejecting the connection but the above code line wont output the die.

Hi,

The mysqli extension is enabled and all the text is right in the php.ini file.

I also tried separating them out like StarLion suggested, but ended up with the same result as before.

I have noticed that my phpinfo.php looks slightly different from the image presented in the book. Where mine reads mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $ under my mysql Client API version, and mysqli Client API library version, the example image simply reads 5.1.41. Also under Active links mine is 0 and his is 58. You think that this could be the problem?

thanks

Okay… next thing to try.


$conn = new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');
echo $conn->connect_error;
var_dump($conn);
return $conn;  

Because mySQLi Construct should always return an object, connected or not, we need to check if there’s a value in connect_error.

(Far as i know, the mysqli constructor never returns FALSE, which would mean the ‘or die’ never triggers)

Refresh the page.

That code didn’t seem to work either. Just keep coming up with the same “Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\phpsols\mysql\mysqli.php on line 8”

This is where I put it just to make sure that I did put it in the right place. In the connection.inc.php file.

<?php
function dbConnect($usertype, $connectionType = 'mysqli') {
  $host = 'localhost';
  $db = 'phpsols';
  if ($usertype  == 'read') {
	$user = 'psread';
	$pwd = 'michael';
  } elseif ($usertype == 'write') {
	$user = 'pswrite';
	$pwd = 'michael';
  } else {
	exit('Unrecognized connection type');
  }
  if ($connectionType == 'mysqli') {
	 $conn = new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');
	 echo $conn->connect_error;
	 var_dump($conn);
	 return $conn; 
	 // return new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');
  } else {
    try {
      return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
    } catch (PDOException $e) {
      echo 'Cannot connect to database';
      exit;
    }
  }
}
echo 'hello';

Anyway I’m shooting off to Oxford until Monday so I’ll not be able to respond to any replies for a short while, but I’ll post back Monday.