English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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.

2007-05-05 00:25:59 · 4 answers · asked by Olivier N 1 in Computers & Internet Programming & Design

4 answers

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-05 00:28:16 · answer #1 · answered by Omega F 2 · 0 0

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.)

2007-05-07 06:05:18 · answer #2 · answered by just "JR" 7 · 0 0

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

2007-05-05 08:05:21 · answer #3 · answered by Mitch C 2 · 0 0

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()

2007-05-05 07:30:36 · answer #4 · answered by Barrucadu 2 · 0 1

fedest.com, questions and answers