This topic is locked
[SOLVED]

 Error: Mysql_Num_Rows(): Supplied Argument Is Not A Valid Mysql Result Resource

5/22/2013 11:18:41 AM
PHPRunner General questions
J
jetacera author

I'm getting the following error for a php code insert: "mysql_num_rows(): supplied argument is not a valid MySQL result resource"
Here is my code:

global $dal;

$tblsitepages = $dal->Table("coupons");

$sql = "SELECT * FROM coupons WHERE coupon_page='".$_SESSION["pageid"]."' ORDER BY rank, couponid";

$rs = CustomQuery($sql);
if(mysql_num_rows($rs) > 0){

echo "<table width=\"1016\" border=\"0\" cellpadding=\"0\" cellspacing=\"2\">";

echo "<tr>";

for($i=0;$i<=mysql_num_rows($rs);$i=$i+1){

while($data = db_fetch_array($rs)){

$i++;

if($i%3==0){

// use the operator to find the remainder of $i / 3 (%)

// if the remainder is 0; it is a multiple of 3, so make a new row.

echo "<td width=\"336\" align=\"center\"><img src=\"http://mydomain.com/siteadmin/banners/".$data["coupon"]."\"'>http://mydomain.com/siteadmin/banners/".$data["coupon"]."\"; width=\"336\" height=\"280\" border=\"0\" alt=\"Click to Print Coupon\" title=\"Click to Print Coupon\" id=\"".$data["couponid"]."\" onClick=\"printme(event)\" style=\"max-width:336px\" /></td></tr><tr>";

} else {

echo "<td width=\"336\" align=\"center\"><img src=\"http://mydomain.com/siteadmin/banners/".$data["coupon"]."\"'>http://mydomain.com/siteadmin/banners/".$data["coupon"]."\"; width=\"336\" height=\"280\" border=\"0\" alt=\"Click to Print Coupon\" title=\"Click to Print Coupon\" id=\"".$data["couponid"]."\" onClick=\"printme(event)\" style=\"max-width:336px\" /></td>";

}
}

}

echo "</tr></table>";

}


This snippet calls up images from a table and display them in rows of 3.
The following original code works on a php page without issue, but I'm having difficulty converting it for use in phprunner:

$sql = mysql_query("SELECT * FROM coupons WHERE coupon_page='".$_SESSION["pageid"]."' ORDER BY rank, couponid");

if(mysql_num_rows($sql) > 0){

echo "<table width=\"1016\" border=\"0\" cellpadding=\"0\" cellspacing=\"2\">";

echo "<tr>";

for($i=0;$i<=mysql_num_rows($sql);$i=$i+1){

while($row = mysql_fetch_array($sql)){

$i++;

if($i%3==0){

// use the operator to find the remainder of $i / 3 (%)

// if the remainder is 0; it is a multiple of 3, so make a new row.

echo "<td width=\"336\" align=\"center\"><img src=\"http://mydomain.com/siteadmin/banners/".$row["coupon"]."\"'>http://mydomain.com/siteadmin/banners/".$row["coupon"]."\"; width=\"336\" height=\"280\" border=\"0\" alt=\"Click to Print Coupon\" title=\"Click to Print Coupon\" id=\"".$row["couponid"]."\" onClick=\"printme(event)\" style=\"max-width:336px\" /></td></tr><tr>";

} else {

echo "<td width=\"336\" align=\"center\"><img src=\"http://mydomain.com/siteadmin/banners/".$row["coupon"]."\"'>http://mydomain.com/siteadmin/banners/".$row["coupon"]."\"; width=\"336\" height=\"280\" border=\"0\" alt=\"Click to Print Coupon\" title=\"Click to Print Coupon\" id=\"".$row["couponid"]."\" onClick=\"printme(event)\" style=\"max-width:336px\" /></td>";

}
}

}

echo "</tr></table>";

}


Any assistance would be appreciated. Thank you.

C
cgphp 5/22/2013

Use the db_numrows wrapper function:

global $dal;

$tblsitepages = $dal->Table("coupons");

$sql = "SELECT * FROM coupons WHERE coupon_page='".$_SESSION["pageid"]."' ORDER BY rank, couponid";

$rs = CustomQuery($sql);
if(db_numrows($rs) > 0){

echo "<table width=\"1016\" border=\"0\" cellpadding=\"0\" cellspacing=\"2\">";

...

...

...
J
jetacera author 5/22/2013

Thank you! That did the trick!

Admin 5/22/2013

Just to clarify - mysql functions are getting obsolete in PHP and PHPRunner switching to mysqli functions. Using PHPRunner wrapper functions like db_numrows solves this issues and prevents future compatibility issues.