My Script Dosent Want To And Will Not Connect Lol

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

My Script Dosent Want To And Will Not Connect Lol

Post by .Stealth »

Hello, ive just recently created a script, im reading from a book and learning and im trying to put to use what i have just learnt so that i will have a better understanding of whats going on, i have looked at the book now and then just to confirm im doing it all right, but it just wont work, it wont connect to the database to create a table, ive tried it on wamp server on my pc and also my web server, here is my code (dont mind my comments, im just trying to get into the habbit of making them lol):

Code: Select all

<?php

//include database details
include('include/db-inc.php');

//include the header of the page
include('include/header.php');


//database connections
$mysql_con = "@mysql_connect('$host','$user','$password')";
$select_db = "@mysql_select_db('$database')";

//check if connection was successfull
if(!$mysql_con){
    exit('<p>Error: Unable to connect to the host.</p>');
};


//check if database exists
if(!$select_db) {
    exit('<p>Error: Unable to connect to the database.</p>');
};

//all is good, lets proceed to create the jokes table
$sql =             'CREATE TABLE joke(
                id INT NOT NULL AUTO-INCREMENT PRIMARY KEY,
                joketext TEXT,
                jokedate DATE NOT NULL
                )';

//check if adding table was sucessfull
if (@mysql_query($sql))    {
                        echo 'Joke table sucessfully created';
}
else {
    exit('<p>error creating table: ' .
    mysql_error() . '</p>');
}

//include the footer of the page
include('include/footer.php');

?>

here is the error that i get when doing it on my pc:
error creating table: Access denied for user 'ODBC'@'localhost' (using password: NO)
i dont know what ODBC is, the user im trying to connect with is called jack, all the paswords `etc are correct.

here is the error on my web server:

error creating table: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

this is whats in "db-inc.php":

Code: Select all

<?php
//mysql database details

$host = 	"********";
$database = "*******";
$user = 	"******";
$password = "*****";
?>
passwords blocked out.

can anybody see what ive done wrong? thanks for any help, im really stuck.
Last edited by .Stealth on Sat Feb 10, 2007 11:59 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Have you tried echoing the usernames and passwords to make sure they are what you think they are? Sounds lke null or false is being passed so it's using defaults. Also, have you added those quotes around the functions to connect? I don't see how it would even give you that error with those quotes there :?
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post by .Stealth »

yeah i put the quotes there, you mean here:

Code: Select all

$mysql_con = "@mysql_connect('$host','$user','$password')";
$select_db = "@mysql_select_db('$database')"; 

they are variables, so i can call them here:

Code: Select all

if(!$mysql_con){
    exit('<p>Error: Unable to connect to the host.</p>');
};


//check if database exists
if(!$select_db) {
    exit('<p>Error: Unable to connect to the database.</p>');
}; 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

No no, that's not right. If they're supposed to run functions, remove the quotes. You get the return value back as #resource_id or (bool)false.

Code: Select all

$mysql_con = @mysql_connect($host,$user,$password); 
$select_db = @mysql_select_db($database);
It's not usually a good idea to surpress errors so much neither... wrap the functions in your own functions and do the error checking there ;)
.Stealth
Forum Commoner
Posts: 57
Joined: Wed Jan 10, 2007 12:15 pm
Location: Manchester, England

Post by .Stealth »

ohh i see, thanks alot, i finally got a freindly error:

Error: Unable to connect to the host.

lol, thank god ofr that, ive been staring at the code for 2 hours lol, ive just looked in the book now and saw that it dosent have quotes lol, just an autopilot thing aye.

thanks alot mate, really helped alot.

ive not learnt about functions yet, i know a little but not how to execute etc, im still very new.

thanks again :D
Post Reply