Query with "if" mode

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
gioffry
Forum Newbie
Posts: 2
Joined: Tue Nov 11, 2008 2:55 am

Query with "if" mode

Post by gioffry »

Hello,
I'm writing a page that queries the database to see if a field is built or not.
The problem is that it does not work.
Someone tells me what's wrong?
Thank you very much!

Code: Select all

$sql2 = "SELECT modelloacquisto1 as modelloacquisto1
        IF(modelloacquisto1="",1,0) as modelloacquisto1
        FROM utenti";
 
$result2 = mysql_query($sql2);
 
if (!$result2) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}
 
if (mysql_num_rows($result2) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}
 
if ($row2['modelloacquisto1'] == 0) {
echo 'ti va di comprare un paio di scarpe?';
else
echo 'Complimenti per il tuo primo acquisto';
}
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Query with "if" mode

Post by novice4eva »

common man CHECK your SQL .... where is the COMMA SEPARATOR

Code: Select all

 
$sql2 = "SELECT modelloacquisto1 as modelloacquisto1/*U MISSED THIS */,/*GRRRRRR*/
        IF(modelloacquisto1="",1,0) as modelloacquisto1
        FROM utenti";
 
gioffry
Forum Newbie
Posts: 2
Joined: Tue Nov 11, 2008 2:55 am

Re: Query with "if" mode

Post by gioffry »

:oops: :roll:

Code: Select all

$sql2 = "SELECT modelloacquisto1 as modelloacquisto1, IF(modelloacquisto1="",1,0) as modelloacquisto1
         FROM utenti";
 
$result2 = mysql_query($sql2);
 
if (!$result2) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}
 
if (mysql_num_rows($result2) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}
 
if ($row2['modelloacquisto1'] == 0) {
echo 'ti va di comprare un paio di scarpe?';
else
echo 'Complimenti per il tuo primo acquisto';
}
blank page :( :banghead:
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Query with "if" mode

Post by novice4eva »

NO WONDER :crazy:
where is your mysql_fetch_array
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Query with "if" mode

Post by VladSun »

You have modelloacquisto1 column name 3 times in your query. While it's not syntaxly wrong in this case, *never* do this again ;)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply