Jump to content

SQL Query inside IF/Else Statements


Marx
Go to solution Solved by cyberRobot,

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.

Edited by Marx
Link to comment
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 -----^
Edited by Ch0cu3r
Link to comment
Share on other sites

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.

Edited by Marx
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.