Jump to content

[SOLVED] IF, ELSE STATEMENT HELP


johntp

Recommended Posts

Hey guys,

 

What I'm trying to acheive is pretty much call my sql database and say if this username isnt in the database then echo whatever, else echo all the data with it. Here is what i have

 

<?php

 

$query2 = "SELECT * FROM info WHERE Username LIKE '$user'";

if ($query2 = "")

{

    echo "what the heck dood";

}

else

{

$query  = "SELECT Username, Address, Phone FROM info WHERE Username = '$user'";

$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

    echo "Address: {$row['Address']} <br>";

echo "Phone:  {$row['Phone']}";

}

}

 

 

?>

 

and whats happening when i do this, it works for people with usernames in the db, but if your username is not in there than it's just blank.

 

I'm not even sure if what i have would work as an if else statement so any help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/116640-solved-if-else-statement-help/
Share on other sites

problem: you were saying "if the query variable is empty"(which it never is), "echo something", else, "run the query, more selectivly".

 

solution:

<?php   
$query2 = "SELECT * FROM info WHERE Username LIKE '$user'";
$result = mysql_query($query2);
if (mysql_num_rows($result) == 0)
{
    echo "what the heck dood";
}
else
{
$row = mysql_fetch_assoc($result);
        echo "Address: {$row['Address']}<br />\n";
   	echo "Phone:   {$row['Phone']}\n";
}
?>

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.