RaythMistwalker Posted January 4, 2010 Share Posted January 4, 2010 Is there a way to count in total how many rows in a table? I made a PM system, and i want to make it so the admin can send a "system message" which is sent from userid number 7. However to do this it should send to all users except userid 7 in the table "members" on my database like so: Subject & Message are entered in a form. get total number of members count = 0 while count < number of members if userid(count) != 7 send message increase count and stop when complete. I can handle the actual coding im just wondering how to count the total number of rows for a table. Quote Link to comment https://forums.phpfreaks.com/topic/187185-number-of-table-rows/ Share on other sites More sharing options...
mikesta707 Posted January 4, 2010 Share Posted January 4, 2010 something like $query = "select count(userid) as numRows FROM tablename"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $num = $row['numRows']; should be about what you want. Look into the count mysql function Quote Link to comment https://forums.phpfreaks.com/topic/187185-number-of-table-rows/#findComment-988458 Share on other sites More sharing options...
laffin Posted January 4, 2010 Share Posted January 4, 2010 Only problem I foresee in that is that you don't actually get the userid. without the userid to send to, ya end up doing 2 more queries. But I dont think you need the count, (unless for some displaty purposes before the actual msg sending) use 2 queries like <?php $fromid=7; $msg='Sample test'; $res=mysql_query("SELECT id FROM users WHERE NOT id={$fromid}"); while($arr=mysql_fetch_row($res)) { $to[]=$arr[0); } $count=count($to); foreach($to as $toid) { mysql_query('INSERT INTO msgs (fromid,toid,msg) VALUES({$fromid},{$toid},'". mysql_real_escape_string($msg) ."')"); } well that is off the top of my head. } Quote Link to comment https://forums.phpfreaks.com/topic/187185-number-of-table-rows/#findComment-988473 Share on other sites More sharing options...
RaythMistwalker Posted January 4, 2010 Author Share Posted January 4, 2010 i just done it via $qry="SELECT member_id FROM members"; $result=mysql_query($qry); $num=mysql_numrows($result); $i = 0; while ($i < $num) { $to_id=mysql_result($result,$i,"member_id"); if ($to_id != '7') { $send = "INSERT INTO messages(to_id, sender_id, subject, message) VALUES('$to_id','7','$subject','$message')"; $sendres = @mysql_query($send); } ++$i; } and it works thanks for help though Quote Link to comment https://forums.phpfreaks.com/topic/187185-number-of-table-rows/#findComment-988478 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.