Jump to content

[SOLVED] mysql_fetch_array(): supplied argument is not a valid MySQL ...


inactive

Recommended Posts

Hey guys.

 

A family member's business site went bananas after someone fiddled with something somewhere, and they've asked me to have a look to see if I can fix it.

 

It basically a pretty hacked job at a php/mysql site, but none of the mysql queries seem to be working.

 

Now I have not had any real experience (or knowlege, to be honest lol) with MySQL, but I do have a general understanding of how queries work (bit of experience with Salesforce API / data loader). That being said I have no idea whats wrong here, so I'm hoping someone can spot the fatal flaw.

 

Ok so the part in question of index.php:

<?php
include_once "connect.php";
$dbh = dbConnect();
$query = "SELECT * FROM categories ORDER BY catName";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
echo '<a href="index.php?page=products&cat=' . $row['catID'] . '" class="';
if($cat < 0) {
	$cat = $row['catID'];
}
if($cat == $row['catID']) {
	echo 'active';
	$catName = $row['catName'];
}
echo '">' . $row['catName'] . '</a>';
}
?>

 

And then connect.php:

<?php
$dbHost = "localhost";
$dbUser = "furnitur_furnitu";
$dbPass = "******";
$dbName = "furnitur_furniture";
function dbConnect() {
global $dbHost;
global $dbUser;
global $dbPass;
global $dbName;
$dbh=mysql_connect($dbHost, $dbUser, $dbPass) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db($dbName, $dbh);
return $dbh;
}
?>

 

And when it runs I get:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/furnitur/public_html/index.php on line 79

 

I've done a bit of googling, and found that this could mean that the query is not valid (bad table name of something), but when I run the same query in phpMyAdmin, it seems to work (please see screen dump at bottom of post).

 

And I've double checked the username and pw, and both are correct (I tried deliberately using the wrong password, and got a could not connect/bad credentials error).

 

Any ideas what the problem is?

 

Thanks heaps guys.

 

~Mitch.

 

 

screendump.png

try this.......

<?php
include_once "connect.php";
$dbh = dbConnect();
$query = "SELECT * FROM categories WHERE ORDER BY catName";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
echo '<a href="index.php?page=products&cat=' . $row['catID'] . '" class="';
if($cat < 0) {
	$cat = $row['catID'];
}
if($cat == $row['catID']) {
	echo 'active';
	$catName = $row['catName'];
}
echo '">' . $row['catName'] . '</a>';
}
?>

Change

<?php
$result = mysql_query($query);
?>

to

<?php
$result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
?>

This should tell you why the query is not working.

 

Ken

Ha! Thanks Ken.

 

Ok so I get:

Problem with the query: SELECT * FROM categories WHERE ORDER BY catName
No database selected

 

Which I think is strange, as in connect.php there is:

$dbName = "furnitur_furniture";

and a bit later:

mysql_select_db($dbName, $dbh);

Where $dbh is the mysql handle (or whatever its called). And from what I've checked in phpMyAdmin, that database name is correct.

 

So is there some bad logic in that part of the code? I'm a bit of a noob here lol, can't spot it myself.

(See my first post on what is in connect.php).

 

Thanks guys.

Change

<?php
mysql_select_db($dbName, $dbh);
?>

to

<?php
mysql_select_db($dbName, $dbh) or die("Problem selecting database: $dbName<br>" . mysql_error());
?>

 

Whenever you're trying to debug mysql problems, put "or die" clauses on the relevant mysql calls. These will help you debug the problems.

 

Ken

You have saved by bacon Ken, thanks.

 

Ok did your suggestion, and it turned out there was a problem with the credentials (username was wrong  :-[ how embarrassing lol).

 

Corrected, tried again, and it said there was a problem with the query syntax. I reverted the query code back to how I had it (i.e. before redarrow's suggestions), and we have lift-off!

 

Thanks for all your help guys!

 

[CLOSED]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.