Forum Moderators: coopster

Message Too Old, No Replies

mysql num rows error?

         

shams

12:00 am on Dec 2, 2006 (gmt 0)

10+ Year Member



hi,
i am learning pagiantion from:

this is what they wrote:
Now we will run a query to find out how many items (rows) match the category being viewed. This will be the total amount of items that can be showed.This query can look something like this:

$q = mysql_query("SELECT * FROM `code` WHERE `type` = '".addslashes($_GET['cat'])."'");
if(!$q) die(mysql_error());
$total_items = mysql_num_rows($q);

I change the query for my mysql table:

<?php
// Make a MySQL Connection
// Connects to your Database
include 'library/config.php';
include 'library/opendb.php';

$q = mysql_query("SELECT * FROM pharmacy WHERE id = '".addslashes($_GET['cat'])."'");
if(!$q) die(mysql_error());
$total_items = mysql_num_rows($q);
echo " total = ".$total_items;
include 'library/closedb.php';
?>
now when i run the query the oupt is:
total = 0
but there are many rows in the table pharmacy, where is the problem and what .addslashes means here?

[edited by: jatar_k at 12:54 am (utc) on Dec. 2, 2006]
[edit reason] no urls thanks [/edit]

justgowithit

12:11 am on Dec 2, 2006 (gmt 0)

10+ Year Member



It's tough to say without seeing the data. I would replace your WHERE clause with a value you know exists. Change
".addslashes($_GET['cat'])."
to a definite value and see if the rows returned are correct.

If they are the problem is not with the query but instead with the $_GET['cat'] value.

shams

12:34 am on Dec 2, 2006 (gmt 0)

10+ Year Member



thanks for reply i change the ".addslashes($_GET['cat'])." to a definit value and the query work fine, any suggestion to change the above query to work with pagination?

eelixduppy

6:59 pm on Dec 2, 2006 (gmt 0)



Take a look at the recent thread on Pagination with MySQL [webmasterworld.com] for a guide on how to set up your query to work correctly.

Also, do not use addslashes [us2.php.net] to escape the variable within the query, use mysql_real_escape_string [us2.php.net].