coupe-r Posted July 3, 2010 Share Posted July 3, 2010 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 More sharing options...
robert_gsfame Posted July 3, 2010 Share Posted July 3, 2010 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 Link to comment https://forums.phpfreaks.com/topic/206615-insert-within-loop-help/#findComment-1080620 Share on other sites More sharing options...
coupe-r Posted July 3, 2010 Author Share Posted July 3, 2010 I didn't even notice that. That was the problem. Thank you!!! Link to comment https://forums.phpfreaks.com/topic/206615-insert-within-loop-help/#findComment-1080621 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.