corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » Php help? (mysql_num_rows)


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author Php help? (mysql_num_rows)
Whittie
Member

Registered: 11th Aug 06
Location: North Wales Drives: BMW, Corsa & Fiat
User status: Offline
15th Jan 10 at 01:25   View User's Profile U2U Member Reply With Quote

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
Location: Northampton Drives: Fiat 500/Porsche Boxster
User status: Offline
15th Jan 10 at 07:46   View User's Profile U2U Member Reply With Quote

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
15th Jan 10 at 09:30   View User's Profile U2U Member Reply With Quote

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
15th Jan 10 at 09:41   View User's Profile U2U Member Reply With Quote

Thank you
Whittie
Member

Registered: 11th Aug 06
Location: North Wales Drives: BMW, Corsa & Fiat
User status: Offline
15th Jan 10 at 10:05   View User's Profile U2U Member Reply With Quote

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
15th Jan 10 at 10:22   View User's Profile U2U Member Reply With Quote

http://www.tizag.com/mysqlTutorial/mysqlcount.php
Whittie
Member

Registered: 11th Aug 06
Location: North Wales Drives: BMW, Corsa & Fiat
User status: Offline
15th Jan 10 at 10:26   View User's Profile U2U Member Reply With Quote

Perfect! Thanks Sam
Sam
Moderator
Premium Member


Registered: 24th Dec 99
Location: West Midlands
User status: Offline
15th Jan 10 at 10:42   View User's Profile U2U Member Reply With Quote



That Tizag site I find very useful/helpful, as well as the W3Schools website.
ed
Member

Registered: 10th Sep 03
Location: Northampton Drives: Fiat 500/Porsche Boxster
User status: Offline
15th Jan 10 at 12:02   View User's Profile U2U Member Reply With Quote

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
Location: Northampton Drives: Fiat 500/Porsche Boxster
User status: Offline
15th Jan 10 at 12:04   View User's Profile U2U Member Reply With Quote

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
Location: Northampton Drives: Fiat 500/Porsche Boxster
User status: Offline
15th Jan 10 at 12:05   View User's Profile U2U Member Reply With Quote

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
15th Jan 10 at 12:10   View User's Profile U2U Member Reply With Quote

Got a lot to learn
Sam
Moderator
Premium Member


Registered: 24th Dec 99
Location: West Midlands
User status: Offline
15th Jan 10 at 12:18   View User's Profile U2U Member Reply With Quote

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
Location: Northampton Drives: Fiat 500/Porsche Boxster
User status: Offline
15th Jan 10 at 12:27   View User's Profile U2U Member Reply With Quote

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
15th Jan 10 at 12:37   View User's Profile U2U Member Reply With Quote

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
Location: Northampton Drives: Fiat 500/Porsche Boxster
User status: Offline
15th Jan 10 at 13:17   View User's Profile U2U Member Reply With Quote

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
15th Jan 10 at 13:48   View User's Profile U2U Member Reply With Quote

Dom
Member

Registered: 13th Sep 03
User status: Offline
15th Jan 10 at 14:38   View User's Profile U2U Member Reply With Quote

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....

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
Few Pics from the Pod (56K Warning) PaulW General Chat 12 711
21st Feb 05 at 16:40
by Jambo
 
Dodgy HTML/PHP/Table problem liamC Geek Day 9 421
24th Jan 07 at 19:37
by liamC
 
PHP Template Help Laney Geek Day 14 525
17th Jul 07 at 19:26
by Ian
 

Corsa Sport » Message Board » Off Day » Geek Day » Php help? (mysql_num_rows) 28 database queries in 0.0191948 seconds