Problems connecting to MySQL through PHP

software development


I have problems connecting to DB.
This is the error I get (which is from my error_handler.php file):
ERRNO: 8 TEXT Undefined variable: mysqli LOCATION: /home/.mohave/tetest/[]/index.php, line 8

Line 8 in index.php says:
$mysqli = new $mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_DATABASE);

These are defined in config.php:

<?php define('DB_HOST', '[]'); define('DB_USER', 'test'); define('DB_PASSWORD', ''); define('DB_DATABASE', 'ajax') ?>

I’m using stock setup nothing funny. This is my first attempt at connecting.

What is wrong? Is it something with access privileges? I use the latest DH version of PHP.


The error message tells you what is wrong - you are using an undefined variable. So you might want to start reviewing the code.

And of the code you posted so far, it looks like the programmer mixed up constants and variables.

I don’t program in PHP much, but you might also want to check that the PHP install supports the mysqli extension - AFAIK DreamHost hasn’t rolled out a PHP install that supports it yet.

:cool: -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7


when you define a constant like
define(“DB_HOST”, “”);

you should invoke it like

$mysqli = new mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

The mistakes are:

  1. remove $ sign when you invoke a constant
  2. the function to connect to mysql is new mysqli_connect

$50 off and 3 free domains with code: [color=#CC0000]DH3[/color] Sign Up NOW or More Codes Here


Thanks for answering.

I’ve removed ‘$’ which was a typo.
I am getting a little closer. I prefer to use the new mysqli statement, since that’s what the author refers to (“Building responsive Web Applications - AJAX and PHP”). I also find tutorials using this. I’ve now changed that line to:
$mysqli = new mysqli(’<>’, ‘tetest’, ‘’, ‘ajax’ );

And I get:
ERRNO: 2 TEXT mysqli::mysqli() [function.mysqli-mysqli]: (28000/1045): Access denied for user ‘tetest’@‘’ (using password: YES) LOCATION: /home/.mohave/tetest/<>/index.php, line 9

Which is all very strange since I host everything on DH.
I understand that there’s some restrictions when connecting from outside, but the site (index.php) and the db are one the same domain (if possibly not on the same server?).

They are like this:

Nevermind! Got it working now. Had used the wrong user. And now that I am explicitly entering info in mysqli it works. I’ll leave this for others reference.

Thanks for the help.


Hey, that is great!

Obviously the error message is complaining a wrong username or password.

I’m glad you got it worked!

$50 off and 3 free domains with code: [color=#CC0000]DH3[/color] Sign Up NOW or More Codes Here