Jump to content

[SOLVED] Warning: mysql_fetch_array()..


kraen123

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/150141-solved-warning-mysql_fetch_array/
Share on other sites

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

 

 

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.
}

  • 2 weeks later...

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?

 

 

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.