Page 1 of 1

My Script Dosent Want To And Will Not Connect Lol

Posted: Sat Feb 10, 2007 10:04 am
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.

Posted: Sat Feb 10, 2007 10:19 am
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 :?

Posted: Sat Feb 10, 2007 10:22 am
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>');
}; 

Posted: Sat Feb 10, 2007 10:28 am
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 ;)

Posted: Sat Feb 10, 2007 10:49 am
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