denno020 Posted December 12, 2010 Share Posted December 12, 2010 I wish to have a page that will display a form if there aren't already enough registrations in the database. The following it an outline of how it will be: <?php require_once "../scripts/connect_to_mysql.php"; //this works fine // Count how many records in the database (1) $sqlCommand = "SELECT * FROM teams"; (2) $sqlCommand = "SELECT COUNT (*) FROM teams"; (3) $sqlCommand = "SELECT COUNT (id) FROM teams"; $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); (1) $numRegistrations = mysql_num_rows($query); //Count how many rows are returned from the query above (2) $numRegistrations = $query (3) $numRegistrations = mysql_num_rows($query); mysqli_free_result($query); ?> (Some HTML code, like DOCTYPE, head, start of body tags) <?php if($numRegistrations > 35){ echo "Sorry, registrations for this event is at it's maximum."; }else{ ?> <form blah blah blah> </form> <?php } ?> (end of body and html tag) You'll see above there is (1), (2), (3). These are the main 3 that I've been trying, and they match up the $sqlCommand to the $numRegistrations. What would be the problem with this code? I use wamp server to test, have been using it for ages. Chrome browser to test in. If I remove the database query, the rest of the page will load. With the query in there, only the HTML code up until the database query is parsed, so therefore nothing is display on the page. Please help. Thanks Denno Quote Link to comment https://forums.phpfreaks.com/topic/221394-counting-the-rows-in-mysql-database-the-obvious-choices-arent-working/ Share on other sites More sharing options...
litebearer Posted December 12, 2010 Share Posted December 12, 2010 is this what you want? $result = mysql_query("SELECT * FROM tablename"); $num_rows = mysql_num_rows($result); // Display the results echo $num_rows; Quote Link to comment https://forums.phpfreaks.com/topic/221394-counting-the-rows-in-mysql-database-the-obvious-choices-arent-working/#findComment-1146150 Share on other sites More sharing options...
denno020 Posted December 12, 2010 Author Share Posted December 12, 2010 yeah mate, that's what I want. That functionality, however it just refuses to work. I've got that listed in my code as (1). Also, be aware that I don't have all 3 of those options in there at the time of testing, I've only put them in to illustrate the different combinations I've used. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/221394-counting-the-rows-in-mysql-database-the-obvious-choices-arent-working/#findComment-1146151 Share on other sites More sharing options...
litebearer Posted December 12, 2010 Share Posted December 12, 2010 you are mixing mysql and mysqli Quote Link to comment https://forums.phpfreaks.com/topic/221394-counting-the-rows-in-mysql-database-the-obvious-choices-arent-working/#findComment-1146152 Share on other sites More sharing options...
OOP Posted December 12, 2010 Share Posted December 12, 2010 This should work // Count how many records in the database $sqlCommand = "SELECT * FROM teams"; $query = mysql_query ( $sqlCommand ) or die ( mysql_error () ); $numRegistrations = mysql_num_rows ( $query ); echo $numRegistrations; Quote Link to comment https://forums.phpfreaks.com/topic/221394-counting-the-rows-in-mysql-database-the-obvious-choices-arent-working/#findComment-1146154 Share on other sites More sharing options...
denno020 Posted December 12, 2010 Author Share Posted December 12, 2010 you are mixing mysql and mysqli Ohhhhkk. So because I use mysqli_query(), the mysql_num_rows doesn't work? I have tried to connect to my database using mysql before, however it didn't work. But I shall try again right now and see how I go, using the code provided by OOP. Will post back with a result. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/221394-counting-the-rows-in-mysql-database-the-obvious-choices-arent-working/#findComment-1146157 Share on other sites More sharing options...
denno020 Posted December 12, 2010 Author Share Posted December 12, 2010 I feel like a douche bag! The mixing of the mysqli and mysql is exactly what the problem was! I was having troubles with trying to run mysql queries before because I was using mysqli to connect to the database. Problem is now fixed, and it works like a charm. Thanks heaps! Quote Link to comment https://forums.phpfreaks.com/topic/221394-counting-the-rows-in-mysql-database-the-obvious-choices-arent-working/#findComment-1146161 Share on other sites More sharing options...
Pikachu2000 Posted December 12, 2010 Share Posted December 12, 2010 If all you need is a count, a SELECT COUNT(`pk_id_field_here`) query is more efficient than using a mysqli_num_rows() query. Quote Link to comment https://forums.phpfreaks.com/topic/221394-counting-the-rows-in-mysql-database-the-obvious-choices-arent-working/#findComment-1146187 Share on other sites More sharing options...
denno020 Posted December 12, 2010 Author Share Posted December 12, 2010 If all you need is a count, a SELECT COUNT(`pk_id_field_here`) query is more efficient than using a mysqli_num_rows() query. I'm not really in need of a more efficient way, I don't think. I guess I could still implement the SELECT COUNT. After trying it before, without success, I just wanted to use the first thing that I could get working, which was the other method lol. If I wanted to use the SELECT COUNT query though, I would just assign $numRegistrations = $query? (where $query = mysql_query($sqlCommand, $myConnection) or die (mysqli_error()) Is there any other steps or is the returned query the exact answer I'm after? Tah Denno Quote Link to comment https://forums.phpfreaks.com/topic/221394-counting-the-rows-in-mysql-database-the-obvious-choices-arent-working/#findComment-1146195 Share on other sites More sharing options...
Pikachu2000 Posted December 12, 2010 Share Posted December 12, 2010 Assuming you settled on the mysql_ extension . . . $query = "SELECT COUNT(`pk_id_field`)"; $result = mysql_query($query); $array = mysql_fetch_row($result); echo $array[0]; // $array[0] contains the number returned by COUNT Quote Link to comment https://forums.phpfreaks.com/topic/221394-counting-the-rows-in-mysql-database-the-obvious-choices-arent-working/#findComment-1146204 Share on other sites More sharing options...
denno020 Posted December 12, 2010 Author Share Posted December 12, 2010 Assuming you settled on the mysql_ extension . . . $query = "SELECT COUNT(`pk_id_field`)"; $result = mysql_query($query); $array = mysql_fetch_row($result); echo $array[0]; // $array[0] contains the number returned by COUNT Awesome, thankyou very much. I will change to that code . Thanks to everyone for their advice too, it all helped me to understand the problem. Denno Quote Link to comment https://forums.phpfreaks.com/topic/221394-counting-the-rows-in-mysql-database-the-obvious-choices-arent-working/#findComment-1146207 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.