Problems connecting to MySQL through PHP

software development

#1

Hi
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/[test2.mydomain.com]/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', '[mysql.mydomain.com]'); 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.


#2

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: openvein.org -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7


#3

when you define a constant like
define(“DB_HOST”, “mysql.mydomain.com”);

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


#4

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(’<test2.mydomain.com>’, ‘tetest’, ‘’, ‘ajax’ );

And I get:
ERRNO: 2 TEXT mysqli::mysqli() [function.mysqli-mysqli]: (28000/1045): Access denied for user ‘tetest’@‘monster.dreamhost.com’ (using password: YES) LOCATION: /home/.mohave/tetest/<mysql.mydomain.com>/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:
test2.mydomain.com
mysql.mydomain.com

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.


#5

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