Page 1 of 1

What is the problem with mysql_num_row?

Posted: Fri Aug 16, 2002 2:39 pm
by rushenas
I wrote a script, shown below:
<?
if ($province=="") $prov="";
else $prov="Place=".$province."and";
if ($ground=="") $grnd="";
else $grnd="Ground=".$ground."and";
if ($border=="") $brdr="";
else $grnd="Border="."$border"."and";
if ($pattern=="") $pat="";
else $pat="Pattern=".$pattern."and";
if ($material=="") $raw="";
else $raw="Raw=".$material."and";
if ($medallion !="Y") $med="";
else $med="Medallion=".$medallion."and";
if ($round !="Y") $circle="";
else $circle="Circle=".$round;
// End of setting up query

$query="SELECT Image, Description, FROM carpet WHERE ".$prov.$grnd.$brdr.$pat.$raw.$med.$circle;
if (substr($query, -3)=="and")
for($i=1; $i<4; $i++)
$query=substr_replace($query, "", -1);

echo $query;
//require("include/db.php"); // Connect to databse
$db = mysql_pconnect("localhost");

if(!$db)
{
echo'<p><b><font face="Verdana"><font color="#FF0000">Error: </font><font color="#000080">Sorry, but can\'t connect to Database. Please try again later.</font></font></b></p>';
exit;
}

mysql_select_db ("carpets");

$result = mysql_query ($query); // Running the query

$amount = mysql_num_rows ($result); // Counting the number of raws returned by query
echo $amount;
?>
But PHP show me an error message:
SELECT Image, Description, FROM carpet WHERE Place=Ardebil
Warning: Supplied argument is not a valid MySQL result resource in C:\apache\htdocs\website\persian carpets inc\1.php on line 37
What is the problem? How can I count number of results?

Posted: Fri Aug 16, 2002 3:04 pm
by hob_goblin
try changing

Code: Select all

$result = mysql_query ($query); // Running the query
to

Code: Select all

$result = mysql_query("$query"); // Running the query

Posted: Fri Aug 16, 2002 7:19 pm
by volka
line 37 is

Code: Select all

$amount = mysql_num_rows ($result); // Counting the number of raws returned by query
?
replace

Code: Select all

$result = mysql_query ($query); // Running the query
by

Code: Select all

print('debug-query:'.$query.'<br/>');
$result = mysql_query ($query) or die(mysql_error());
you forgot some spaces and strings have to be quoted in where clauses.
what will the code do if none of the parameters are set?
i.e.

Code: Select all

if ($province!="")
	$where = "Place='$province'";
else
	$where='';
	
if ($ground!='')
	$where .= ((strlen($where)>0)? ' AND ' : '')."Ground='$ground'";
if ($border!='')
	$where .= ((strlen($where)>0)? ' AND ' : '')."Border='border'";
if ($pattern!='')
	$where .= ((strlen($where)>0)? ' AND ' : '')."Pattern='$pattern'";
if ($material!='')
	$where .= ((strlen($where)>0)? ' AND ' : ''). "Raw='$material'";
if ($medallion =="Y")
	$where .= ((strlen($where)>0)? ' AND ' : ''). "Medallion='$medallion'";
// ^^ always  Medallion='Y' or not set ?
if ($round =="Y")
	$where .= ((strlen($where)>0)? ' AND ' : ''). "Circle='$round'";

// End of setting up query 

$query="SELECT Image, Description, FROM carpet";
if (strlen($where) > 0)
	$query.=' WHERE '.$where;
^ be careful - code not even tested by compiler ;)