Jump to content

Number of table rows?


RaythMistwalker

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/187185-number-of-table-rows/
Share on other sites

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.

 

}

 

 

Link to comment
https://forums.phpfreaks.com/topic/187185-number-of-table-rows/#findComment-988473
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/187185-number-of-table-rows/#findComment-988478
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.