Question:
Why can't my php connect to mysql server?
Olivier N
2007-05-05 04:25:59 UTC
I am programming in php. php and mysql are installed on SUSE linux platform. Any other thing in php is successful. But when I try to connect to mysql server, it fails. I am very sure that there is no syntax error in my program. and I have turned off all the firewalls on my linux computer.If you want to see how my codes are:
$db = mysql_connect("localhost");
mysql_select_db("umuryango",$db);
$query = "SELECT * FROM abavandimwe";
$result = mysql_query($query);
$table = mysql_field_table($table,0);
echo $table
?>

here my intertion was to echo the name of the table on the browser using localhost/db.php db.php being the name of the file
umuryango is the database name and abavandimwe is the table name that I created on mysql server. With nothing displayed on the browser I concluded that there is no connection between php and mysql. So I need your help.
Four answers:
Omega F
2007-05-05 04:28:16 UTC
Syntax Error #1:

Line 2

You must specify a username and password in your sql_connect function.



Logical Error #1:

Line 6

mysql_field_table($table, 0);

should be:

mysql_field_table($result, 0);



WORKING VERSION : COPY AND PASTE CODE:

$DB_HOST = 'localhost'; // mysql database relitive path

$DB_USER = 'username'; // mysql username

$DB_PASS = 'password'; // mysql password

$DB_CONN = mysql_connect(

$DB_HOST,

$DB_USER,

$DB_PASS)

OR die ('Error connecting to mysql DB');

// connect or print error



$dbname = 'umuryango';// database name

$dbtable = 'abavandimwe'; // database table

mysql_select_db ($dbname);

$query = 'SELECT * FROM ' . $dbtable;

$result = mysql_query($query);

// DEBUG: echo $query; // print SQL

$table = mysql_field_table($result,0);

echo $table; // print table name
?
2007-05-07 10:05:18 UTC
Few mistakes:

1. Connecting: needs username and password.

$link = mysql_connect("localhost", "username", "password") or die(" Could not connect : " . mysql_error());

This will show you WHY you do not connect (mysql_error returns a message.)

Keep the value $link to close the DB later.



2. Selecting the DB

mysql_select_db("dbname") or die("Could not select database");



3. The job:

"SELECT * from" returns an array. i.e.

$query = "SELECT * FROM `users` WHERE `user`=' " . $uname." ' ";

(Your query does not have any details such as "WHERE". If you have nothin, just write "where 1".)



(Now, send the query:)

$list = mysql_query($query) or die("Query failed : " . mysql_error());

(and report the error if there is one)



(Now fetch the array:)

$lst = mysql_fetch_array($list);

(now, use the array $lst)



(Now, free the results:)

mysql_free_result($list);



(Finally, close the link:)

mysql_close($link);

(don't forget to CLOSE your link: that's why you KEEP $link all the way through.)
Mitch C
2007-05-05 12:05:21 UTC
Hello,



You're getting a little crazy with things there and you need to simplify it all. Here's a simple connection script you can use and place in an external file to be called into pages with database connectivity:



//connect to the database

$db = mysql_connect("localhost", "db_username", "db_user_password");

mysql_select_db("database_name");



And there you have it. Let me know if you need more help.



I hope it helps,

Mitch Cannon
Barrucadu
2007-05-05 11:30:36 UTC
Not only are there syntax errors, but you are using a variable to store the connection link ($db), and thats not needed. You also haven't put your username and password in mysql_connect()


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...