kraen123 Posted March 19, 2009 Share Posted March 19, 2009 I have been getting this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /... I'm new to PHP, and am using the MySQL/Flash/PHP panel extension which generates the PHP for the application. Here is my code. Any help is GREATLY appreciated! I'm going away for a few days so if I don't reply right away, I'm not forgetting/ignoring. thanks. application/x-httpd-php selectDB.php PHP script text <?php include("db_connect.php"); //---------------Set user vars-------------------- $forCount = $_POST['selectRowsLength']; $whereCount = $_POST['whereLength']; $ASCCount = $_POST['ASCLength']; $tableName = $_POST['table1']; //$ASC = $_POST['ASC']; //$orderBy = $_POST['orderBy']; $ASCTr = ""; $orderByTr = ""; $tableOrderByASC = ""; $ASC = array(); $orderBy = array(); if($_POST['ASC0'] != "" && $_POST['orderBy0'] != ""){ for ($iii=0; $iii<$ASCCount; $iii++){ if ($iii != 0){ $tableOrderByASC .= ", "; }else{ $tableOrderByASC .= "ORDER BY "; } $ASCTr = 'ASC'.$iii; $ASC[$iii] = $_POST[$ASCTr]; $orderByTr = 'orderBy'.$iii; $orderBy[$iii] = $_POST[$orderByTr]; $tableASC = "$ASC[$iii]"; $tableOrderBy = "$orderBy[$iii]"; $tableOrderByASC .= "$tableOrderBy"; if($ASC[$iii] == "ASC"){ $tableOrderByASC .= " ASC"; }else{ $tableOrderByASC .= " DESC"; } } } //------------ //echo "cantId=23"; $whereTr = ""; $likeTr = ""; $whatTr = ""; $tableWhere_What = ""; $where1 = array(); $like1 = array(); $what1 = array(); if($_POST['whereA0'] != "" && $_POST['whatA0'] != ""){ for ($ii=0; $ii<$whereCount; $ii++){ if ($ii != 0){ $tableWhere_What .= " AND "; }else{ $tableWhere_What .= "WHERE "; } $whereTr = 'whereA'.$ii; $where1[$ii] = $_POST[$whereTr]; $likeTr = 'likeA'.$ii; $like1[$ii] = $_POST[$likeTr]; $whatTr = 'whatA'.$ii; $what1[$ii] = $_POST[$whatTr]; $tableWhere = "$where1[$ii]"; $tableWhere1 = "$what1[$ii]"; $tableWhere_What .= "$tableWhere"; if($like1[$ii] == "LIKE"){ $tableWhere_What .= " LIKE '%"; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= "%'"; }else if($like1[$ii] == "="){ $tableWhere_What .= " = '"; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= "'"; }else if($like1[$ii] == "<>"){ $tableWhere_What .= " <> '"; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= "'"; }else if($like1[$ii] == ">"){ $tableWhere_What .= " > '"; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= "'"; }else if($like1[$ii] == "<"){ $tableWhere_What .= " < '"; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= "'"; }else if($like1[$ii] == ">="){ $tableWhere_What .= " >= '"; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= "'"; }else if($like1[$ii] == "<="){ $tableWhere_What .= " <= '"; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= "'"; }else if($like1[$ii] == "BETWEEN"){ $tableWhere_What .= " BETWEEN "; $tableWhere_What .= "$tableWhere1"; }else if($like1[$ii] == "IN"){ $tableWhere_What .= " IN ("; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= ")"; }else if($like1[$ii] == "=TABLE"){ $tableWhere_What .= " = "; $tableWhere_What .= "$tableWhere1"; }else if($like1[$ii] == "TABLE"){ $tableWhere_What .= " = "; $tableWhere_What .= "$tableWhere1"; }else if($like1[$ii] == "<>TABLE"){ $tableWhere_What .= " <> "; $tableWhere_What .= "$tableWhere1"; }else if($like1[$ii] == "LIKETABLE"){ $tableWhere_What .= " LIKE "; $tableWhere_What .= "$tableWhere1"; }else if($like1[$ii] == ">TABLE"){ $tableWhere_What .= " > "; $tableWhere_What .= "$tableWhere1"; }else if($like1[$ii] == "<TABLE"){ $tableWhere_What .= " < "; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= ""; }else if($like1[$ii] == ">=TABLE"){ $tableWhere_What .= " >= "; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= ""; }else if($like1[$ii] == "<=TABLE"){ $tableWhere_What .= " <= "; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= ""; }else if($like1[$ii] == "BETWEENTABLE"){ $tableWhere_What .= " BETWEEN "; $tableWhere_What .= "$tableWhere1"; }else if($like1[$ii] == "INTABLE"){ $tableWhere_What .= " IN ("; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= ")"; }else{ $tableWhere_What .= " = '"; $tableWhere_What .= "$tableWhere1"; $tableWhere_What .= "'"; } } } //---------------- //To run this function flash needs to pass in "fc and tn then tr[i] and trv[i]" $tableRows = ""; $nextTr = ""; $tra = array(); for ($i=0; $i<$forCount; $i++){ if ($i != 0){ $tableRows .= ", "; } $nextTr = 'selectRows'.$i; $tra[$i] = $_POST[$nextTr]; $tableRows .= "$tra[$i]"; } //---------------End set user vars---------------- //------------------------------------------------ //-------------add user function------------------ $result = mysql_query("SELECT $tableRows FROM $tableName $tableWhere_What $tableOrderByASC"); // --start output string with number of entries-- $cant = 0; while($row=mysql_fetch_array($result)) { for ($i=0; $i<$forCount; $i++){ //$traSplit = split(".",""); $myTra = $tra[$i]; if (preg_match("/[.]/", $myTra)) { list($M21, $M22) = split('[.]', $myTra); $myTra = $M22; } echo "$myTra"; echo "$cant"; echo "="; echo "$row[$myTra]"; echo "&"; } $cant++; } echo "cantId=$cant"; ?> This line "while($row=mysql_fetch_array($result))" (#159) is the line indicated in the error. Thanks! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 19, 2009 Share Posted March 19, 2009 Whenever you have problem with queries, you should add some debugging. Try modifying the query to this: $sql = "SELECT $tableRows FROM $tableName $tableWhere_What $tableOrderByASC"; $result = mysql_query($sql) or trigger_error(mysql_error().'<br />Query was'.$sql); That should give you some output that will indicate the problem. P.s. Welcome to the forums Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted March 19, 2009 Share Posted March 19, 2009 kraen123, for someone who is new to PHP, don't you think this is kind of ambiguous? Quote Link to comment Share on other sites More sharing options...
trq Posted March 19, 2009 Share Posted March 19, 2009 You need to always check your queries succeed and return results before trying to use them. eg; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { // now we can use $result } else { // no results found } } else { // query failed. handle error. } Quote Link to comment Share on other sites More sharing options...
kraen123 Posted March 29, 2009 Author Share Posted March 29, 2009 kraen123, for someone who is new to PHP, don't you think this is kind of ambiguous? I don't understand what you mean. Quote Link to comment Share on other sites More sharing options...
kraen123 Posted March 29, 2009 Author Share Posted March 29, 2009 Thanks for the replies. I'll give it a shot tomorrow and see what error is being thrown other than what I put above. Quote Link to comment Share on other sites More sharing options...
kraen123 Posted March 29, 2009 Author Share Posted March 29, 2009 I tried the first suggestion (Ginger Robot's), and I got the following error: Notice: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM' at line 1 Query wasSELECT FROM in ...selectDB.php on line 157 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in ...selectDB.php on line 160 cantId=0 It seems that the problem is with the section: //-------------add user function------------------ $sql = "SELECT $tableRows FROM $tableName $tableWhere_What $tableOrderByASC"; $result = mysql_query($sql) or trigger_error(mysql_error().'<br />Query was'.$sql); // --start output string with number of entries-- $cant = 0; while($row=mysql_fetch_array($result)){ for ($i=0; $i<$forCount; $i++){ //$traSplit = split(".",""); $myTra = $tra[$i]; if (preg_match("/[.]/", $myTra)) { list($M21, $M22) = split('[.]', $myTra); $myTra = $M22; } echo "$myTra"; echo "$cant"; echo "="; echo "$row[$myTra]"; echo "&"; } $cant++; } echo "cantId=$cant"; Any ideas how to fix this? Quote Link to comment Share on other sites More sharing options...
kraen123 Posted March 30, 2009 Author Share Posted March 30, 2009 (bump--sorry...really want to tackle this last hurdle) Can anyone help please? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 30, 2009 Share Posted March 30, 2009 Based on what little of the error message you have posted, I would guess that $tableRows is empty at the time the query is formed. Where is $tableRows set? Quote Link to comment Share on other sites More sharing options...
kraen123 Posted April 2, 2009 Author Share Posted April 2, 2009 Well, I bought a new site on a new server with a slightly newer version of PHP and it works! Thanks everyone for the help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.