Hi guys,
I really need some help.
I have two tables tbl_question with 2 columns question_id , question_name and
tbl_answers with 3 columns answer_id,answer_name and questionFK.
I want to get values from question_id and compare them with questionFK if some values are missing in questionFK then I need a insert of missing values.
values from questionFK have to be always same as question_id
Thanx in advance
Compare two columns
How about
$q='SELECT id FROM tbl_q';
while($r=&mysql_fetch_assoc(&mysql_query($q)){
$q2='SELECT COUNT(*) FROM tbl_a WHERE qFK = \''.$r['id'].'\'';
$r2=mysql_fetch_assoc(mysql_query($q2));
if($r2['COUNT(*)']>0){continue;};
mysql_query('INSERT INTO tbl_a VALUES (\''.$r['id'].'\',\'\',\'\')');
};//end while
I'm getting this error Fatal error: Can't use function return value in write context in
The solution from Studio Junkies seem to work. However it will require lots of queries. With the solution below you only have to use 2 queries to the database.
$sql = "SELECT tbl_q.q_id
FROM tbl_q
LEFT JOIN tbl_a ON tbl_q.q_id = tbl_a.qFK
WHERE tbl_a.qFK IS NULL"
$result = mysql_query($sql);
$sql2 = "INSERT INTO tbl_a (qFK) VALUES (";
$first = 1;
while ($row = mysql_fetch_array($result))
{
if ($first == 0) // If $first is 0 then we should add a comma
{
$sql2 = $sql2 . ", " . $row['q_id'];
}
else
{
$sql2 = $sql2 . $row['q_id'];
$first = 0;
}
}
$sql2 = $sql2 . ");";
mysql_query($sql2);
Studio Junkies
Thank you wery much it works great
Yes, I wanted to use a join query but I couldn't get one that worked, so I gave a quick PHP solution.
And I use a DB wrapper class for all my queries. It has been a while since I used the core functions by hand. Maybe the error is due to the reference symbols '&'
Glad it works.
come to think of it, this would probably solve the error:
$r=&mysql_fetch_array(mysql_query($q));
Studio Junkies wrote:Maybe the error is due to the reference symbols '&'
Unless you've got a very good reason for using it (that you can explain), it's very probably wrong.
No, I dont have a very good reason that I can explain, it is just a habit of mine to reduce duplicate variables for RAM efficiency.
If you can explain a reason why I should not be using it, I'd gladly like to hear it.
Cheers.