No connection to joke database

i have tryed to connect to the database but it is saying Unable to locate the joke database.

bit the database ijdb is there as is joke any one know why this is happening
the joke as jokes in it ect but ive tryed everythin i can think of i am new to php my sql but it wa working then it as stoped i have the right details in the connect index.php page which is below i do not use password because im just learning, anyone can help please send me email or you can contact me on skype or msn or yahoo all help will be greatful thanks paul
here is my code

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

As a debug test:


$link = mysqli_connect('localhost', 'ijdb', '') or die(mysql_error());

see what it says.

thankyou for your repley but im still getting the same error
Unable to locate the joke database.

After the if block for connecting to the MySQL server, add this line

printf("Server version: %s\
", mysqli_get_server_info($link));

If PHP has connected to MySQL ok then the server version will be displayed, if the server version isn’t displayed or has an error then the problem is with establishing a connection between PHP and MySQL

thanks for your reply but it still same error unless i have put it in the wrong place here is the code ive more than likely put it in the wrong place thanks again here is the code?

<?php
$link = mysqli_connect(‘localhost’, ‘ijdb’, ‘’) or die(mysql_error());

if (!$link)

{printf("Server version: %s
", mysqli_get_server_info($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’;
?>

hello i have changed my database name to somthing else and the joke table what i am getting now it is trying to download a file instead of showing the jokes woulda anyone have any ideas why (started getting to me now doin everything in book to the letter and it still isnt working)
thankyou for reading this and i hope you can help ?

Have you checked your password to the database…? There might be one set and you will need to set it to no password if you dont want one

Are you using a localserver with phpmyadmin…?

Also try removing the $link variable in your ‘select _db’ line, it might make a difference

This is the code I use, it might help.

<?php

$connect = mysql_connect(“localhost”,“root”,“”)
or die (“Connection failed”);

mysql_select_db(“phppractice”) or die (“Nope didn’t connect”);
echo “Connected”;

?>

From your first post you said

i have tryed to connect to the database but it is saying Unable to locate the joke database.

bit the database ijdb is there as is joke any one know why this is happening

from this I read that your database table is called “joke” not ‘ijdb’

So you need to change line 17


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

to


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

thankypu for your help with my problem but i am still getting the same error it is trying to download the file instead of showing joes in the data base
i am not using a password and i am using localhost on my computer
here is the code i have got:

<?php
$link = mysqli_connect('localhost', 'bailuk', '');
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, 'bailsjokes'))
{
$output = 'Unable to locate the bailsjokes database.';
include 'output.html.php';
exit();
}
$output = 'Database connection established.';
include 'output.html.php';
?>

i have attached a screen shot of the erro to download
thank you again for all your help

attachments take too long to be approved, please copy and paste what error you are now getting

the error i am sorry i can not get it to paste into box i have trye all extentios gif jpeg ect

I just need to know which error you are seeing on your screen now, you have changed the name of your database and the name of your table, I cant just assume that youre still getting the error in the same part of your code. :wink:

You can send me the attachment via PM if you want

Add this between the block where the character set is set and the block where a database is selected:

$db_list = mysql_list_dbs($link);

while ($row = mysql_fetch_object($db_list)) {
     echo $row->Database . "<br />";
}

That should display a list of databases on the server which the current MySQL user is allowed to see. You should see amongst the list of databases the ‘ijdb’ (or whatever you named the jokes database) database listed there.

If it is not listed then it may be that the MySQL user your using to log into MySQL from PHP might not have the rights to see the jokes database.

thank you all for trying to help me i still can not connect to the database joke i have reinstalled everything and set new passwor but im still getting this error:
UNABLE TO CONNECT TO THE DATABASE SERVER.

the password is 100% correct i have followed every step of the book a million times and it is all correct to the book and still will not connect here is the code im using i have added the @ at the top line to stop load of text coming before the error thanks again for all your help

<?php
$link = @mysqli_connect(‘localhost’, ‘ijdb’, ‘bailukcom’);
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’;
?>

wish i could get this sorted stoping me movin to next chapter!!!

$link = @mysqli_connect('localhost', 'ijdb', 'bailukcom');

So ‘ijbd’ is your sql database name (not to be confused with table name) and ‘bailukcom’ is your sql password, yes ?

replace this line

$output = 'UNABLE TO CONNECT TO THE DATABASE SERVER.';

with

 $output = 'UNABLE TO CONNECT TO THE DATABASE SERVER. - ' . mysqli_error();

just a thought have you tried not using the mysqli extension, perhaps it not enabled.

Try this


<?php
$link = @mysql_connect('localhost', 'ijdb', 'bailukcom');
if (!$link)
 
{
$output = 'UNABLE TO CONNECT TO THE DATABASE SERVER. - ' . mysql_error();  
include 'output.html.php';
exit();
}
 
if (!mysql_set_charset($link, 'utf8'))
 
{
$output = 'UNABLE TO SET DATABASE CONNECTION ENCODING. - ' . mysql_error();  
include 'output.html.php';
exit();
}
 
if (!mysql_select_db($link, 'ijdb'))
 
{
$output = 'UNABLE TO LOCATE THE JOKE DATABASE. - ' . mysql_error();  
include 'output.html.php';
exit();
}
 
$output = 'DATABASE CONNECTION EStABLISHED.;
include 'output.html.php';
?>
 
 

thanks for your reply i have changed what you said and now i get diffrent error
Fatal error: Call to undefined function mysql_error() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\connect\index.php on line 6
now i get that and here is the line you told me to change

<?php
$link = @mysqli_connect('localhost', 'ijdb', 'bailukcom');
if (!$link)

{
$output = 'UNABLE TO CONNECT TO THE DATABASE SERVER.' . mysql_error();

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';
?>

thanks again?

It sounds like a configuration problem with your locahost software.

Are you using WAMP…? This would really help if you tell us what software you are using for this.

If so make sure rewrite_module is turned on, this caused me problems before.

You do this by going to WAMP, right clicking, going to Apache, going to Apache Modules, then scrolling down the list and getting a tick placed next to the mentioned above.

I know this is small point but the name of your db is exactly the same as the one in your script right…?

no i am using the windows essentails (x86) and apache 2.2.15 (win32) and php 2.2.13 as it says in the book i installed the idivdule package ones, i am using windows 7 ulitmate as my o/s, all datbase everything is workin it is allowed through windows fire wall just that it will not connect for some silly reason
thankyou for your reply

thankyou all for yur help i have sorted it it as connected to the db now thank you all