colinjack Posted March 1, 2009 Share Posted March 1, 2009 When I run this <?phpinclude 'config.php';include 'opendb.php';mysql_select_db ('kenfest', $conn); $result = mysql_query("SELECT * FROM register"); $total_tickets = mysql_query("SELECT sum(tickets) FROM register"); $numrows = mysql_num_rows($result); echo "There are $numrows people registered:<BR />"; echo "Total tickets requested: $total_tickets <br />";?> I get:There are 10 people registered:Total tickets requested: Resource id #6 but if I run the same query from the mysql CLI I get the correct result.What am I missing?I've just started playing with PHP so PLEASE be patient with me! Link to comment Share on other sites More sharing options...
jeffman Posted March 1, 2009 Share Posted March 1, 2009 This might help: http://www.tizag.com/mysqlTutorial/mysqlsum.php Link to comment Share on other sites More sharing options...
colinjack Posted March 2, 2009 Author Share Posted March 2, 2009 This might help: http://www.tizag.com/mysqlTutorial/mysqlsum.php I found that and other similar one's on the web but that isn't quite what I want.I want a total sum of all the values of the 'tickets' field. The database allows people to register to request tickets (up to 10 per person). I just want to return a running total of tickets.This works from the CLI:mysql> select sum(tickets) from register;+--------------+| sum(tickets) |+--------------+| 97 |+--------------+1 row in set (0.00 sec)mysql>but not from within the script.Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 2, 2009 Share Posted March 2, 2009 $total_tickets = mysql_query("SELECT sum(tickets) FROM register");$total_tickets is not going to be the number, it's going to be a recordset like any other call to mysql_query. You still need to get the row (there should only be one), and then you can get the value from the row. Link to comment Share on other sites More sharing options...
colinjack Posted March 2, 2009 Author Share Posted March 2, 2009 $total_tickets = mysql_query("SELECT sum(tickets) FROM register");$total_tickets is not going to be the number, it's going to be a recordset like any other call to mysql_query. You still need to get the row (there should only be one), and then you can get the value from the row.Doh - just realised what I've written. Back to the manual. Thanks for the pointer. Link to comment Share on other sites More sharing options...
colinjack Posted March 2, 2009 Author Share Posted March 2, 2009 Sorted it! $result = mysql_query("SELECT sum(tickets) AS total_tickets FROM register");while($row = mysql_fetch_array($result)){ echo $row['total_tickets']; } Many thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.