about connecting php and mysql

hacklinux

learn more share more....
can you please help me with connecting mysql with php?the code i used is

<?php

# Define MySQL Settings
define("MYSQL_HOST", "localhost");
define("MYSQL_USER", "root");
define("MYSQL_PASS", "hello");
define("MYSQL_DB", "test");

$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());
mysql_select_db("".MYSQL_DB."",$conn) or die(mysql_error());

$sql = "SELECT * FROM test";
$res = mysql_query($sql);

while ($field = mysql_fetch_array($res))
{
$id = $field['id'];
$name = $field['name'];

echo 'ID: ' . $field['id'] . '<br />';
echo 'Name: ' . $field['name'] . '<br /><br />';
}

?>



but it displays an error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\server\htdocs\mysql_test.php on line 15



please help me at the earliest.
 

Saahib

Cyborg Agent
Assuming rest is without any error, you have to give one more parameter to mysql_fetch_array function. Since you are suing associative array in result, it should be
Code:
while ($field = mysql_fetch_array($res,MYSQL_ASSOC))


Let me know if it works for you or else I will test this at my place!
 
OP
H

hacklinux

learn more share more....
no it didnt work.it says d same:



Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\server\htdocs\bic.php on line 15
 

krishnandu.sarkar

Simply a DIGITian
Staff member
I see few errors there...

Try this...

PHP:
<?php

$link = mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("testdb", $link) or die(mysql_error());

$sql = "SELECT * FROM tbltestdb";
$result = mysql_query($sql, $link);

while ($row = mysql_fetch_array($result))
{
$id = $row['id'];
$name = $row['name'];

echo "ID: " . $id . "<br />";
echo "Name: " . $name . "<br />";
}

mysql_close($link);
?>

Now the errors I'm suspecting are...

1.

PHP:
# Define MySQL Settings
define("MYSQL_HOST", "localhost");
define("MYSQL_USER", "root");
define("MYSQL_PASS", "hello");
define("MYSQL_DB", "test");

$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());

So the string that's getting genrated is...

PHP:
$conn = mysql_connect("""localhost""", """root""", """hello""") or die(mysql_error());

Because...you wrote

mysql_connect("".MYSQL_HOST.""

so the variable MYSQL_HOST = "localhost" that you defined avobe

so "localhost" gets concatinated as ""."localhost"."" which becomes """localhost"""

Right..??

So you should have write it as...

PHP:
$conn = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die(mysql_error());

Better use...

PHP:
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";

$link = mysql_connect($dbhost, $dbuser, $dbpass);


Another one is...

2.

PHP:
$res = mysql_query($sql);

PHP:
mysql_query($sql);
should actually be
PHP:
mysql_query($sql, $link);

Though I've seen many people to write it as like you did. I don't know whether it's right or wrong, but $link should be provided as the function prototype itself says that...

Correct me if I'm wrong anywhere..!!

And lastly one suggestion...always perform mysql_close(); :)

Happy Coding :D
 
@krishnandu.sarkar
i have been learning PHP and ur code worked out fine.

what is the best option if speed of connectivity and execution is to be considered - mysql mysqli PEAR db or PDO ??? (are the there any more extentions ?)

also how can i find the PHP version other than phpinfo() ??

as PDO is not available for versions <5.0 i want to write a function that takes the DB driver name as parameter and find out which version of PHP is available and will return the connection variable either of PEAR db or PDO type
 

mitraark

Decrepit
Code:
$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());

Why use the "". ... ."" ? Simply write

Code:
$conn = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS)
if(!isset($connection))
die(mysql_error());
 

krishnandu.sarkar

Simply a DIGITian
Staff member
@krishnandu.sarkar
i have been learning PHP and ur code worked out fine.

what is the best option if speed of connectivity and execution is to be considered - mysql mysqli PEAR db or PDO ??? (are the there any more extentions ?)

also how can i find the PHP version other than phpinfo() ??

as PDO is not available for versions <5.0 i want to write a function that takes the DB driver name as parameter and find out which version of PHP is available and will return the connection variable either of PEAR db or PDO type

Sorry no idea. I don't know that much PHP, just more than the basics. :(
 
Top Bottom