conker87 Posted April 17, 2008 Share Posted April 17, 2008 I've got a profile site that counts the content that they have added to my site. All well and good, works peachy. However, now I want to add in so that I can calculate the percentage of the total they have added. I tried the following, but i just says the MySQL client ran out memory, here is the code: <?php $listArticle = mysql_query("SELECT * FROM `articles` WHERE `author` = '{$row['username']}' ORDER BY `id` DESC") or die(mysql_error()); $countArticle = mysql_num_rows($listArticle); $listReview = mysql_query("SELECT * FROM `reviews` WHERE `author` = '{$row['username']}' ORDER BY `id` DESC") or die(mysql_error()); $countReview = mysql_num_rows($listReview); $listStrip = mysql_query("SELECT * FROM `strips` WHERE `author` = '{$row['username']}' ORDER BY `id` DESC") or die(mysql_error()); $countStrip = mysql_num_rows($listStrip); $listAll = mysql_query("SELECT `id` FROM `articles`,`reviews`,`strips`") or die(mysql_error()); $countAll = mysql_result($listAll, 0); $totalContent = $countArticle + $countReview + $countStrip; $totalPercent = round(($totalContent / $countAll) * 100, 2); ?> Is there any way I could clean this up? Make it more efficient? Link to comment https://forums.phpfreaks.com/topic/101519-solved-mysql-client-out-of-memory/ Share on other sites More sharing options...
Gamic Posted April 17, 2008 Share Posted April 17, 2008 Let the query do the counting. <?php $listArticle = mysql_query("SELECT count(*) as c FROM `articles` WHERE `author` = '{$row['username']}' ORDER BY `id` DESC") or die(mysql_error()); $listReview = mysql_query("SELECT count(*) as c FROM `reviews` WHERE `author` = '{$row['username']}' ORDER BY `id` DESC") or die(mysql_error()); $listStrip = mysql_query("SELECT count(*) as c FROM `strips` WHERE `author` = '{$row['username']}' ORDER BY `id` DESC") or die(mysql_error()); $listAll = mysql_query("SELECT count(`id`) as c FROM `articles`,`reviews`,`strips`") or die(mysql_error()); $countArticle = mysql_fetch_array($listArticle, MYSQL_ASSOC); $countArticle = $countArticle['c']; $countReview = mysql_fetch_array($listReview, MYSQL_ASSOC); $countReview = $countReview['c']; $countStrip = mysql_fetch_arrary($listStrip, MYSQL_ASSOC); $countStrip = $countStrip['c']; $countAll = mysql_fetch_array($listAll, MYSQL_ASSOC); $countAll = $countAll['c']; $totalContent = $countArticle + $countReview + $countStrip; $totalPercent = round(($totalContent / $countAll) * 100, 2); ?> Link to comment https://forums.phpfreaks.com/topic/101519-solved-mysql-client-out-of-memory/#findComment-519313 Share on other sites More sharing options...
conker87 Posted April 17, 2008 Author Share Posted April 17, 2008 That works pretty well, thanks BUT! I noticed that the percentage was rather small, so I echoed the formula: echo "(" . $totalContent . "/" . $countAll . ")". "* 100"; And apparently, there's a total of '39312' items between those 3 sections! There's no where near that amount, anyone know why it thinks there's so many? Link to comment https://forums.phpfreaks.com/topic/101519-solved-mysql-client-out-of-memory/#findComment-519892 Share on other sites More sharing options...
conker87 Posted April 17, 2008 Author Share Posted April 17, 2008 Meh, no matter, I just used separate queries. Thanks Gamic Link to comment https://forums.phpfreaks.com/topic/101519-solved-mysql-client-out-of-memory/#findComment-519925 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.