inactive Posted March 31, 2008 Share Posted March 31, 2008 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. Link to comment https://forums.phpfreaks.com/topic/98749-solved-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql/ Share on other sites More sharing options...
redarrow Posted March 31, 2008 Share Posted March 31, 2008 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>'; } ?> Link to comment https://forums.phpfreaks.com/topic/98749-solved-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql/#findComment-505319 Share on other sites More sharing options...
inactive Posted March 31, 2008 Author Share Posted March 31, 2008 Thanks for the tip redarrow, but didn't change the result unfortunately. Anything else? Link to comment https://forums.phpfreaks.com/topic/98749-solved-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql/#findComment-505326 Share on other sites More sharing options...
kenrbnsn Posted March 31, 2008 Share Posted March 31, 2008 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 Link to comment https://forums.phpfreaks.com/topic/98749-solved-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql/#findComment-505328 Share on other sites More sharing options...
inactive Posted March 31, 2008 Author Share Posted March 31, 2008 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. Link to comment https://forums.phpfreaks.com/topic/98749-solved-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql/#findComment-505335 Share on other sites More sharing options...
kenrbnsn Posted March 31, 2008 Share Posted March 31, 2008 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 Link to comment https://forums.phpfreaks.com/topic/98749-solved-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql/#findComment-505338 Share on other sites More sharing options...
inactive Posted March 31, 2008 Author Share Posted March 31, 2008 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] Link to comment https://forums.phpfreaks.com/topic/98749-solved-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql/#findComment-505345 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.