Jump to content

SQL Query inside IF/Else Statements


Marx

Recommended Posts


<?php
$constructors = array ('Renault', 'Citroen', 'Ferrari', 'Fiat', 'Seat', 'Honda', 'Toyota', 'Hyundai');
for ($u=0; $u< count ($constructors); $u++)
{
if ($u % 2 == 0)
{
echo 'Pair results:'. $constructors[$u].' ';
echo '<br />';
}
else
{
echo 'Odd results:'. $constructors[$u];
echo '<br />';
}
}?>

I have to insert the Pair result in one of the SQL Tables (Constructor1) and the Odd results in another of the tables (Constructor2) but as they are inside and if and else, i don't know how to do it. I tried with function but i noticed you can't use functions there. Any idea?

 

Example: $query = "INSERT INTO constructors (Constructor1, Constructor2) VALUES ('$constructors[$u]', '$constructors[$u]')";

 

Thanks for your help.

Link to comment
https://forums.phpfreaks.com/topic/285167-sql-query-inside-ifelse-statements/
Share on other sites

You can run queries just fine within if/else statements. What have you tried?

 

What doesnt make sense if your query will add duplicate data to your table

Example: $query = "INSERT INTO constructors (Constructor1, Constructor2) VALUES ('$constructors[$u]', '$constructors[$u]')";
//                                                                                    ^------ duplicated -----^

They are not duplicated because it should have been:

Example: $query = "INSERT INTO constructors (Constructor1, Constructor2) VALUES ('$constructors[0]', '$constructors[1]')";

and so on, that the part of the if/else statements. So i can get the pair and odd positions in the array. Obviously it doesn't make sense because it was just an example.

 

I tried to do a concatenated query that starts in the if and ends in the else but i'm not sure if that possible because i keep getting mysql errors.

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.