Unable to locate database

Hi there,

I’m trying to run the chapter4/connect/index.php code from Chapter 4 of “Building your own database driven website using PHP and MySql” and the error that I’m getting in the browser is “Unable to locate the joke database.” Do I have to put the sql file in a certain directory?
I appreciate your help.

Thanks,
M

You probably have to import the SQL file into the database called ‘jokes’???

Have you created the ijdb yet? A

SHOW  DATABASES

query will show you if you have

No I haven’t created the DB on my computer. I uploaded the code to my webserver and expected it to work. I have mysql installed on my comp. Would this be how to begin:

>CREATE TABLE ijdb;

Then how do I add the sql file to the ijdb?

And how do I set this up to work on the webserver?

I tried setting up the tables for the ijdb database on my hostgator webserver and i’m getting the following error that the ‘author’ already exists. Any idea why this is happening?

Error
SQL query:

CREATE TABLE mrusinek_ijdb.author (

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
name VARCHAR( 255 ) ,
email VARCHAR( 255 )
) CHARACTER SET utf8 ENGINE = MYISAM
MySQL said:

#1050 - Table ‘author’ already exists

USE mrusinek_ijdb;
DROP TABLE author;

OK I did that. I went back and re-entered the table, and edited it but am still getting the same error.

I hope you are trying to run this code(taken from here):

<?php
$link = mysqli_connect('localhost', 'root', 'password');
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();
}

$output = 'Database connection established.';
include 'output.html.php';
?>

If yes obviously there is no db called ijdb
Did you try to run the SQL?

SHOW  DATABASES

I created the tables and went to the chapter4/connect/index.php and still getting ‘Unable to locate the joke database.’

Here’s my code:
<code>
<?php
$link = mysqli_connect(‘localhost’,‘mrusinek’,‘mypasswd’);

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, ‘mrusinek-ijdb’))
{
$output = ‘Unable to locate the joke database.’;
include ‘output.html.php’;
exit();
}

$output = ‘Database connection established.’;
include ‘output.html.php’;
?></code>

ok got it. database connected successfully.

What did Multiv123 do?

I get the same error - unable to connect to database.

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

My database is named contact or woli_contact. I’ve used phpmyadmin to create the database and I know it’s there but I’ve tried every variation of database name that I can think of and it won’t connect to the database.

Suggestions?