A while loop .. not sure how to

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

A while loop .. not sure how to

Post by nite4000 »

I am trying to do a loop and what i need done is i am selecting all from the user table then I need it to take the id of each users put it into the mem_id field in the support table and go until it does all the users

Here is the code i have so far

$r = mysql_query("SELECT * FROM user") or die(mysql_error());
while($info = mysql_fetch_array($r, MYSQL_ASSOC)

$q=mysql_query("INSERT into support(id,mem_id,email,subject,message,sent,status)VALUES(null,'" . $info['id'] . "','$email','$subject','$msg','$date','4')")or die(mysql_error());

@mysql_free_result($r);
}


as you can see i have it all done ecept where i make it go into the support table even though i have the query it not working I am lost on loops. anyone who can help would be nice

Thanks
coolpositive
Forum Newbie
Posts: 4
Joined: Sat Jun 07, 2008 6:13 am

Re: A while loop .. not sure how to

Post by coolpositive »

Hi nite4000,

First you optimized your mysql query like

$resultSet = mysql_query("SELECT id FROM user")or die(mysql_error());

while($row = mysql_fetch_array($resultSet))
{
$userId = $row["id"];

$strQuery = "INSERT into support(id,mem_id,email,subject,message,sent,status)VALUES(null,'$userId','$email','$subject','$msg','$date','4')" ;

$resultSet1 = mysql_query($strQuery)or die(mysql_error());
}

mysql_free_result($resultSet);
mysql_free_result($resultSet1);

still if you got error then

echo your both query and direct put on the phpmyadmin sql section and got the exect error.

or debug your code through echo language constructor.

:offtopic:
coolpositive
Forum Newbie
Posts: 4
Joined: Sat Jun 07, 2008 6:13 am

Re: A while loop .. not sure how to

Post by coolpositive »

Hi nite4000,

First you optimized your mysql query like

$resultSet = mysql_query("SELECT id FROM user")or die(mysql_error());

while($row = mysql_fetch_array($resultSet))
{
$userId = $row["id"];

$strQuery = "INSERT into support(id,mem_id,email,subject,message,sent,status)VALUES(null,'$userId','$email','$subject','$msg','$date','4')" ;

$resultSet1 = mysql_query($strQuery)or die(mysql_error());
}

mysql_free_result($resultSet);
mysql_free_result($resultSet1);

still if you got error then

echo your both query and direct put on the phpmyadmin sql section and got the exect error.

or debug your code through echo language constructor.

:|
Post Reply