kurtos123 Posted August 30, 2015 Share Posted August 30, 2015 [ code ]<?php$iKolommen = 5;$iGamesPerPagina = 60;$aSpelNamen = array( "bratz", "yasmine ski dressup", );?> [ / code ] I use above code to determine all games with name bratz and yasmin ski dressup in my database and to print them on my webpage. How can i print the exact number of all the games with those names ? Can you give me the exact code because i am a newbie in php. I tried a code but it only gives the number 2 because it counts the names and not the amount. Many thanks for your help in advance. Quote Link to comment Share on other sites More sharing options...
kurtos123 Posted August 30, 2015 Author Share Posted August 30, 2015 figured this out and it works but is there a much shorter way? Figured this out and it works, but is there a much shorter way ? <?php $sWhere = ""; $i = 0; foreach($aSpelNamen AS $sSpelNaam) { $sWhere .= "name LIKE '%" . $sSpelNaam . "%'"; if(++$i < count($aSpelNamen)) { $sWhere .= " OR "; } } $rQuery = mysql_query("SELECT id FROM " . $pre . $sqlgame . " WHERE (" . $sWhere . ")"); $iCount = mysql_num_rows($rQuery); ?> bratz spellen (<?php echo $iCount; ?>) Quote Link to comment Share on other sites More sharing options...
Barand Posted August 30, 2015 Share Posted August 30, 2015 Rather than fetch all the records just to see how many there are it would be more efficient to fetch 1 row with the desired count $rQuery = mysql_query("SELECT COUNT(id) as total FROM " . $pre . $sqlgame . " WHERE (" . $sWhere . ")"); Quote Link to comment Share on other sites More sharing options...
kurtos123 Posted August 31, 2015 Author Share Posted August 31, 2015 Tried inplementing your code Barand but i am sorry it did not give any result. I need to print on my webpage the total number of games with the name "Bratz" and "yasmine ski dressup" which are in my mysql database. (SpelNamen means GameNames, SpelNaam means GameName). <?php $sWhere = ""; $i = 0; foreach($aSpelNamen AS $sSpelNaam) { $sWhere .= "name LIKE '%" . $sSpelNaam . "%'"; if(++$i < count($aSpelNamen)) { $sWhere .= " OR "; } } $rQuery = mysql_query("SELECT COUNT(id) as total FROM " . $pre . $sqlgame . " WHERE (" . $sWhere . ")"); $iCount = mysql_num_rows($rQuery); ?> bratz games (<?php echo $iCount; ?>) Quote Link to comment Share on other sites More sharing options...
Barand Posted August 31, 2015 Share Posted August 31, 2015 As I said in my reply, the query will return one row with the required count. You need to get the result from the total field returned in the query, not count the rows. $rQuery = mysql_query("SELECT COUNT(id) as total FROM " . $pre . $sqlgame . " WHERE (" . $sWhere . ")"); $row = mysql_fetch_assoc($rQuery); $iCount = $row['total']; Stop using the mysql_ functions (they will not be available in future versions of php) and start using mysqli_ functions or PDO. Quote Link to comment Share on other sites More sharing options...
kurtos123 Posted August 31, 2015 Author Share Posted August 31, 2015 It works now, thanks for all your help Barand, i really appreciate it, have a nice day ! Quote Link to comment 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.