Forum Moderators: coopster

Message Too Old, No Replies

ecommerce num_rows thingy

elo all !

         

diegomh7

3:34 pm on Jul 4, 2006 (gmt 0)

10+ Year Member



So, Im doing an ecommerce site and im querying the db using php. when i try and retrieve products from the database and count the rows im using mysql_num_rows.

One of the features is to display how many i.e dell Desktops are in the db, so im connecting to the db and counting the results in the SELECT query, problem is :

//DESKTOPS
$sqldesk = "SELECT * FROM goldline_products where brand = '$brand' AND prod_type = 'Desktop'";
$rsdesk = mysql_query($sqldesk);
$deskcount = mysql_num_rows($rsdesk);
echo "$deskcount";

that counts the results in the db no problem and then later in the page im echoing

Desktops ($deskcount) Laptops ($lapcount)

etc

so people can see how many are in the db.

Whats happening is when the result returns 0, the warning msg for num_rows is displayed all over the top of the page, all i want it to do is say ok there arent any so the variable = 0.

Help

Can i use a different function to count the array or something?

regards,

Diego

diegomh7

3:43 pm on Jul 4, 2006 (gmt 0)

10+ Year Member



not working properly yet but ....

//DESKTOPS
$sqldesk = "SELECT * FROM goldline_products where brand = '$brand' AND prod_type = 'Desktop'";
$rsdesk = mysql_query($sqldesk);
if (mysql_num_rows == 0 ){
$deskcount ="0";
}else{
$deskcount = mysql_num_rows(rsdesk);
}

Grrrrrrr

[edited by: diegomh7 at 3:47 pm (utc) on July 4, 2006]

Sekka

3:46 pm on Jul 4, 2006 (gmt 0)

10+ Year Member



mysql_num_rows () should return 0 if no records are found.

I often find it errors when the table is empty though, or if the SQL query fails.

diegomh7

3:48 pm on Jul 4, 2006 (gmt 0)

10+ Year Member



so what do can do about it?

i need the result of 0 if its empty but not the warning ..

hair, pull, now, pain, will, make, feel, better...

diegomh7

4:32 pm on Jul 4, 2006 (gmt 0)

10+ Year Member



ok think i fudged it ..

//DESKTOPS
$sqldesk = "SELECT * FROM goldline_products where brand = '$brand' AND prod_type = 'Desktop'";
$rsdesk = mysql_query($sqldesk);
$deskcount = mysql_num_rows($rsdesk) or ('');

seems to work, kind of do this or do nothing kind of approach.

hope this helps others with the same problem

regards,

Diego

coopster

4:40 pm on Jul 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It shouldn't be erroring when the table is empty, but if the query is incorrect it certainly will.


so what do can do about it?

Well, you should monitor whether or not your query ran successfully! If you have an issue with your statement you should indeed be monitoring for these types of issues.

barns101

4:40 pm on Jul 4, 2006 (gmt 0)

10+ Year Member



The "@" should suppress any errors from the mysql_num_rows() function:


//DESKTOPS
$sqldesk = "SELECT * FROM goldline_products where brand = '$brand' AND prod_type = 'Desktop'";
$rsdesk = mysql_query($sqldesk);
$deskcount = @mysql_num_rows($rsdesk);
echo "$deskcount";

diegomh7

6:54 pm on Jul 4, 2006 (gmt 0)

10+ Year Member



Thanx for the feedback people!

One more thing,

"Well, you should monitor whether or not your query ran successfully! If you have an issue with your statement you should indeed be monitoring for these types of issues."

erm, great, any examples? I mean i do query stuff and echo results to see if things are working,

thanx in advance

Diego

Sekka

11:21 pm on Jul 4, 2006 (gmt 0)

10+ Year Member



//DESKTOPS
$sqldesk = "SELECT * FROM goldline_products where brand = '$brand' AND prod_type = 'Desktop'";
$rsdesk = mysql_query ($sqldesk);
if (!$rsdesk) {
// Code if MySQL query failed
}

$deskcount = mysql_num_rows($rsdesk);

Sekka

11:22 pm on Jul 4, 2006 (gmt 0)

10+ Year Member



Also, when using SQL, try and stick to conventions, e.g. upper case keywords like "WHERE" instead of "where".

diegomh7

8:59 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



oki dokey thanx guys, ii do normally stick to uppercase so its easier to read later,

thanx for the help guys,

Diego