Can Not Connect To DB

Hey guys, here is the piece of code that I am using that is located w/in a folder… I type http://localhost/additem and the 3rd “IF” statement is thrown, I am tried moving a copy of the DB Folder itself to the same master folder that the additem folder is located w/in…

<?php
$link = mysqli_connect(‘localhost’, ‘root’, ‘’);
if (!$link)
{
$output = ‘Unable to connect to the database server.’;
include ‘output.html.php’;
exit();
}

if (!mysqli_set_charset($link, ‘utf8’))
{
$output = ‘Unable to set database connection encoding.’;
include ‘output.html.php’;
exit();
}

if (!mysqli_select_db($link, ‘ijdb’))
{
$output = ‘Unable to locate the joke database.’;
include ‘output.html.php’;
exit();
}

$sql = ‘CREATE TABLE joke (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
joketext TEXT,
jokedate DATE NOT NULL
) DEFAULT CHARACTER SET utf8’;
if (!mysqli_query($link, $sql))
{
$output = 'Error creating joke table: ’ . mysqli_error($link);
include ‘output.html.php’;
exit();
}

$output = ‘Joke table successfully created.’;
include ‘output.html.php’;
?>


if (!mysqli_select_db($link, 'ijdb'))

should be


if (!mysqli_select_db('ijdb', $link))

You had the arguments reversed.

You have it reversed on several other functions as well. The $link would be second on set_charset and query as well.

From I am told, you have to pass the $link, before you can pass the file (db) you are looking for.

Just to recap this…
I am typing in:

localhost/chapter4/connect

and the file/folder itself is @:
HD/Library/WebServer/Documents/Chapter4/connect

I checked w/in my “myphpadmin” and there is a table named “ijdb”, yet it will not connect to it.

As I told you, you are passing the values in the wrong order:

mysql_select_db ( string $database_name [, resource $link_identifier ] )

PHP: mysql_select_db - Manual

Same with mysql_query:

mysql_query ( string $query [, resource $link_identifier ] )

PHP: mysql_query - Manual

Trust me.

You can write it like this if you want to know why one of your functions is failing:


mysqli_select_db($link, 'ijdb') or die(mysql_error());

Well I tried that already and it didnt work… and according my “Site Point” book. it says mysqli_query(link, query) which means you pass the link before you pass the item you want. BUT, either way, I am still getting the same error message. Also, I tried switching the link, utf8 and when i do… it throws the 2nd error

mysqli takes link first. base mysql takes dbname first. (You should actually be passing the DBName as parameter 4 on your connect command, btw)

This database… does exist, right? You’ve already created it?

Instead of displaying a generic error message, display mysqli_error().

My apologies. I was looking only at mysql_select_db. I didn’t notice you were using mysqli_select_db.

At any rate, you should run this snippet:


mysqli_select_db($link, 'ijdb') or die(mysqli_error());

Is this the way it should look? B/C if so, I am not seeing a error msg. btw, I tried mysqli_select_db($link, ‘ijdb’) or die(mysqli_error()); and I still do not get any sort of error msg.

if (!mysqli_select_db($link, ‘ijdb’));
{
$output = die(mysqli_error());
include ‘output.html.php’;
exit();
}

die() is like exit(). It will stop processing of the file. If you want the script to continue, then do this:


if (!mysqli_select_db($link, 'ijdb'));
{
$output = mysqli_error();
include 'output.html.php';
exit();
} 

Yea, I think I tried that also… still no error msg… I even went into my MAMP folder and made sure that error msg’ing was turned on…

Out of curiosity, since you are using mysqli, what happens if you do this:


$link = mysqli_connect('localhost', 'root', '', 'ijdb');

Mysqli allows you to include the database name in the connect function.

if I do that, the first

if (!$link)
{
$output = ‘Unable to connect to the database server.’;
include ‘output.html.php’;
exit();
}

is thrown.

Can you connect to any other table?

I must admit, this one is throwing me for a loop. I’ve never not gotten an error message from a failed database connection, so I’m a bit at a loss. The only other thing I can think of is that the user is not permitted to access the table, but if you are using the root account, that shouldn’t be an issue and it should still throw an error message.

Actually I have never done this before… I only have 2 tables in my DB… one is icdb and the other is ijdb. I have tried to change the text for the other db and it still wont connect. BTW, this is all stored locally on my computer…

Do you have the same user/password combo in your scripts as you use for myphpadmin? You said you can get to the table via myphpadmin, so if the user/pass is different for that interface, try switching it.

From what I can tell, my user/passwords are root/root, although I am not sure all the places to check for that. I do know that
mysqladmin is set for root/root,
/Applications/MAMP/bin/phpMyAdmin is also set for root/root,
and
/Applications/MAMP/bin/mamp/index.php is set to root/root

Wait. Go back to this:


if (!mysqli_select_db($link, 'ijdb'));

{

$output = mysqli_error();

include 'output.html.php';

exit();

} 

And use this:

if (!mysqli_select_db($link, 'ijdb'));

{

$output = mysqli_error($link);

include 'output.html.php';

exit();

} 

I never used procedural style mysqli, so I didn’t put the $link into the error function, and from the php manual, it says it should be there.

Ok that got me a different response…

Unknown database ‘ijdb’