![]() |
MySQL Count and PHP
Hey
1. Will $categorycount = mysql_query("select count(*) from games where catid = '" . $row['id'] ."'"); count the total rows in the games table but only if catid is $row['id']; 2. Do I need to do a mysql_fetch_array on the query or is there any other way I can show the count result? |
Hey
1. Yeh that looks fine. 2. You can use mysql_result to return the first column of the first row in the result set. Example: PHP Code:
|
Thanks for that Karl.
|
Thus using the result just gave you, you can also make your query a lot more safe. Even if you are a beginner. Though this does looks a lot better than when I first started out. Props on that one. First I should advise you NOT to use double quotes ("") but single quotes (''). Why? Since defining what ever in a query, it's wise to open a identifier with "'.$username.'". Thus the query will look like this:
PHP Code:
PHP Code:
Although it is a bit off topic, it still might help you. Good luck crazyryan! |
Thanks for the advice, I have one more problem.
My site uses mod_rewrite on the category pages and I want to apply pagination, will that be a problem? My .htaccess code is RewriteRule "^category/([0-9]+).html$" category.php?id=$1 [Last] And my PHP code is PHP Code:
Any help? |
I'm not all too fond of .htaccess files, so I've chosen to stick to the server-side programming PHP and mySQL. So maybe you should wait for a confirming reply from a little bit more experienced scripter than me. For example, Karl, Salathe of Wildhoney.
On to the posted PHP field. I've checked it our and it should not pose any problem. I reckon it's for some kind game browser or progress page? I'm very fond of those scripts, so if you could share a little bit more, that would be very graceful. Anyways, returning to the pagination part. There, since you said you were kind of a beginner, are a few ways to do pagination. Through classes or simple methods like $_GET[page] and LIMIT in the query. Simply use the TOTAL of results to generate a page. For exmaple, you've got a limit of 20 results a page. LIMIT 0 20 LIMIT 20 40 LIMIT 40 60 LIMIT 60 80 And so on. Now how do you get it dynamic? If NO $_GET is defined, LIMIT is simply 0 to $max ($max = 20;) Then, if get is set, you can retrieve it (like $_GET[page] is 20), then you simply add it to $new = $_GET[page] + $max. LIMIT '.$_GET[page].' '.$new.' :) It should NOT pose any conflicts since the while loop simply echo'd what you retrieve, thus you choose WHAT to retrieve with the LIMITS. Make sure to build in simply checks like if (!is_numeric($_GET[page])) { exit('trying to be a h4xxOR!?'); } DONE. Good luck! Hopefully you kinda understand my jibberish up there. |
Quote:
|
I agree with Karl, with the exception of using quotes around the `catid` field. If it's an INT type column you should forgo using quotes as if it were a string, e.g.
PHP Code:
|
Always single quote your variables, otherwise injection is easy (even if cleaned). For instance
PHP Code:
PHP Code:
PHP Code:
|
Quote:
String values should be wrapped in quotes; integer, double, etc. columns should not be wrapped in quotes. It's just good SQL. |
Quote:
r stands for? |
Quote:
|
Quote:
|
Quote:
p instead which stands for pointer otherwise face the wrath of Adam. :-Phttp://www.talkphp.com/showpost.php?p=3956&postcount=23 |
Quote:
1. Check all numeric/floating/ect. before running the query with is_numeric, die with error if not numeric. Problem: Future developers may not be so wise to catch on to what you are doing. You may also forget once. 2. Have another cleaning function for expected numeric variables Problem: Same as 1 3. Quote everyting Problem: Same as 1 and 2, but much simpler. It is the difference of 2 characters opposed to a different or extra function. Its not bad SQL, as taken from http://dev.mysql.com/doc/refman/5.0/...uidelines.html Quote:
|
Quote:
We could argue about it all night. If you want to wrap your INT type data in quotes, I won't report you. ;-) |
According to the mysql docs, its the recommended way, its not just allowed.
Perhaps is not good otherSQL, I don't have any real experience in anything besides mysql. |
I think it would be in everyone's best interest to at least try PDO
Why? Because it's very secure, you don't need to manually protect queries, it does it for you :-) Also.. it's easier, I think ^^ |
Quote:
You could try this: RewriteRule ^category/([0-9]+)/page\-([0-9]+)\.html$ category.php?id=$1&page=$2 [L,QSA] and have the following URLs: /category/1/page-1.htmlOf course you will need to work on the query to make the pagination work. Talk a look at this tutorial or this one (more advanced). |
Quote:
|
| All times are GMT. The time now is 07:18 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0