Whittie
Member
Registered: 11th Aug 06
Location: North Wales Drives: BMW, Corsa & Fiat
User status: Offline
|
http://is.gd/
Where it says underneath how many current urls have been shortened, how would I do this??
Need something to count how many urls are currently in the db and display it, but how could I do it?
Any help is appreciated, thanks!
[Edited on 15-01-2010 by Whittie]
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
In PHP MySQL you'd use MYSQL_NUM_ROWS to retrieve the number of records in a table.
|
Laney
Member
Registered: 6th May 03
Location: Leeds
User status: Offline
|
quote: Originally posted by ed
In PHP MySQL you'd use MYSQL_NUM_ROWS to retrieve the number of records in a table.
Or possibly a SELECT COUNT(*) on the table
|
Whittie
Member
Registered: 11th Aug 06
Location: North Wales Drives: BMW, Corsa & Fiat
User status: Offline
|
Thank you
|
Whittie
Member
Registered: 11th Aug 06
Location: North Wales Drives: BMW, Corsa & Fiat
User status: Offline
|
Been looking online and in my book. They both say this should work, but it doesn't (Obviously, yes i did replace the user / password / db name).
code:
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
?>
|
Sam
Moderator Premium Member
Registered: 24th Dec 99
Location: West Midlands
User status: Offline
|
http://www.tizag.com/mysqlTutorial/mysqlcount.php
|
Whittie
Member
Registered: 11th Aug 06
Location: North Wales Drives: BMW, Corsa & Fiat
User status: Offline
|
Perfect! Thanks Sam
|
Sam
Moderator Premium Member
Registered: 24th Dec 99
Location: West Midlands
User status: Offline
|
That Tizag site I find very useful/helpful, as well as the W3Schools website.
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
For what you want you should either write it so that it echos the data as JSON, or you should write it as a function which returns the value. It makes your script more modular and you can re-use it else where in the site. If you have it return it as JSON then you would then need some Javascript to get it onto the page, but then with a bit of AJAX you could have it update live. No real need to do it what way, but it's just the difference between a website an a web app
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
Also, store your MySQL connection in a file and include it in every page you require a connection. Modularity again...
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
I also tend to write a function for mysql queries. So that:
$sql="SQL QUERY";
$query=mysql_query($sql);
becomes:
$query=dbQuery("SQL QUERY);
Which you can re-use all over the site.
[Edited on 15-01-2010 by ed]
|
Whittie
Member
Registered: 11th Aug 06
Location: North Wales Drives: BMW, Corsa & Fiat
User status: Offline
|
Got a lot to learn
|
Sam
Moderator Premium Member
Registered: 24th Dec 99
Location: West Midlands
User status: Offline
|
I think Whittie needs to first understand how he has to do things, before learning how to do them differently?
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
Learning how to write things into modular functions the first time round isn't exactly hard though. It promotes you to properly plan a structure also which saves a lot of time.
|
Sam
Moderator Premium Member
Registered: 24th Dec 99
Location: West Midlands
User status: Offline
|
Yes I know it's not hard, but he needs to learn what the functions do first. It's all very well telling him to do this that and the other but if he doesn't understand what the hell things do he won't understand how/why he needs to do them different as he progresses!
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
Alright alright, he seems a clever lad I'm sure he'll see the suggestions and be able to do a bit of reading around.
|
Sam
Moderator Premium Member
Registered: 24th Dec 99
Location: West Midlands
User status: Offline
|
|
Dom
Member
Registered: 13th Sep 03
User status: Offline
|
php.net, for explainations and examples
A good understanding of functions and classes, and the differences is also worth the reading, especially when and when not to use either....
|