Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.