# about connecting php and mysql



## hacklinux (Jun 18, 2011)

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.


----------



## Ricky (Jun 18, 2011)

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 
	
	



```
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!


----------



## hacklinux (Jun 20, 2011)

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 (Jun 20, 2011)

I see few errors there...

Try this...


```
<?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.*


```
# 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...


```
$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...


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

Better use...


```
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";

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


Another one is...

*2. *


```
$res = mysql_query($sql);
```


```
mysql_query($sql);
```
 should actually be 
	
	



```
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


----------



## speedyguy (Jun 21, 2011)

is it possible for u to use mysql_fetchrow() instead of mysql_fetch_array() ...

Enjoy~!


----------



## arpanmukherjee1 (Jun 25, 2011)

@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 (Jun 25, 2011)

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

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


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


----------



## krishnandu.sarkar (Jun 26, 2011)

arpanmukherjee1 said:


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



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


----------

