April 23, 2012

Warning: mysql_fetch_assoc() expects parameter 1 to be resourc

Question by Mahdi_Nine

Possible Duplicate:
mysql_fetch_assoc error, can't seem to figure out what the problem is
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

<?php
$Dblink = mysql_connect("localhost", "root", "","Amoozeshgah");
$res = mysql_query("select * from Tbl_About");
$rec = mysql_fetch_assoc($res);


?>

the query is working but has following error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean

why?

Answer by Starx

You are mixing API’s of mysql and mysqli. To be more specific, you use the mysqli_connect() as mysql_connect(). Here are the two different ways to solve the problem.

Using MySQLi

$Dblink = mysqli_connect("localhost", "root", "","Amoozeshgah");
$res = mysqli_query($Dblink, "select * from Tbl_About");
$rec = mysqli_fetch_assoc($res);

But, If you want to keep on using MySQL API, then, you have to use mysql_select_db() to select the DB

$Dblink = mysql_connect("localhost", "root", "");
mysql_select_db("Amoozesh");

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!