July 25, 2013

mysql_query() keeps failing

Dylan’s Question:

So I have a MySQL database that I’m trying to query, and the query keeps failing. I know this because immediately after my query I call mysql_fetch_array() to parse the result of the query into something usable. At this point, however, my script pukes, saying that mysql_fetch_array() expects a resource, but a boolean was given. The only way this could have happened is that mysql_query() failed and returned false instead of a resource. My relevant files are here.

The first file contains my MySQL configuration.
The second file contains the include file that contains the function with the problem.
The third file contains the actual webpage that calls the function with the problem.

The fourth file is completely unrelated, but since I couldn’t post each of these links individually (I don’t have enough rep points yet), I had to give you all of them at once.

Yes, mysql_fetch_array() does require a valid resource to fetch something. Before I tell you your soluction, I would like to point that MYSQL_* API is now deprecated So I will tell you using mysqli API

$query = "YOUR QUERY STATEMENT";
$result = mysqli_query($query); // Now this statement should return a mysql resource
$row = mysqli_fetch_assoc($result);

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!