Jump to content

INSERT within LOOP help


coupe-r

Recommended Posts

Hey guys,

 

I'm trying to use an INSERT within a FOR loop.  When I test and echo $row['client_id'], it echos 567, which is correct.  There are 3 clients 5,6 and 7.

 

But when I take out the test and use the INSERT, it INSERTS 3 records, but only the first of 3 has the correct client_id.  I get this error:

 

mysql_fetch_array() expects parameter 1... on line 63.

 

Line 63 is $row = mysql_fetch_array($result);

 

Here is the entire code:

 

$today = date("F j, Y, g:i a");

$result = mysql_query("SELECT client_id FROM clients WHERE acct_type = '1' AND deleteStatus = '0'");

$count = mysql_num_rows($result);
  if($count < 0)
  {
    echo '0 Clients Available';
  }
    else
    {
      for($i=1;$i<=$count; $i++)
    {
$row = mysql_fetch_array($result);

        $result = mysql_query("INSERT INTO messages VALUES(NULL, '".$_SESSION['client_id']."', '".$row['client_id']."', '".$subject."', '".$message."', '1', '0', '1', '0', '".$today."', '0', '".$today."', '".$_SESSION['client_id']."', '".$_SESSION['client_id']."')");
    }
}

Link to comment
https://forums.phpfreaks.com/topic/206615-insert-within-loop-help/
Share on other sites

Hey guys,

 

I'm trying to use an INSERT within a FOR loop.  When I test and echo $row['client_id'], it echos 567, which is correct.  There are 3 clients 5,6 and 7.

 

But when I take out the test and use the INSERT, it INSERTS 3 records, but only the first of 3 has the correct client_id.  I get this error:

 

mysql_fetch_array() expects parameter 1... on line 63.

 

Line 63 is $row = mysql_fetch_array($result);

 

Here is the entire code:

 

$today = date("F j, Y, g:i a");

$result = mysql_query("SELECT client_id FROM clients WHERE acct_type = '1' AND deleteStatus = '0'");

$count = mysql_num_rows($result);
  if($count < 0)
  {
    echo '0 Clients Available';
  }
    else
    {
      for($i=1;$i<=$count; $i++)
    {
$row = mysql_fetch_array($result);

        $result = mysql_query("INSERT INTO messages VALUES(NULL, '".$_SESSION['client_id']."', '".$row['client_id']."', '".$subject."', '".$message."', '1', '0', '1', '0', '".$today."', '0', '".$today."', '".$_SESSION['client_id']."', '".$_SESSION['client_id']."')");
    }
}

 

if i am not wrong....you have two $result

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.