pak4eva Posted March 29, 2007 Share Posted March 29, 2007 Hi guys Ive managed to connect my sql database using php but the problem im coming across is outputing a message if something equals to null, i.e. i have an attribute in the database called 'Imagename1' so what i want to do is print out a message saying 'no image exist' Imagename1 = 'NULL' ...... output message 'no image exist'. The code im using is below, your help will be very much appreciated.... <?php $host ="?"; $user = "?"; $pass = "?"; $db= "?"; // Connects to your Database $connection = mysql_connect("$host", "$user", "$pass") or die("Unable to connect"); mysql_select_db("$db") or die("Unable to select database"); <?php {$query = "Select Imagename1, Imagename2 From Element Where Symbol ='$_GET'"; IF (Imagename1 = "NULL") { echo("No image available"); } } $result = mysql_query($query) or die ("error in Query: $query. " .mysql_error()); if (mysql_num_rows($result)>0) { echo "<table border=0 cellpadding=0 cellspacing=0>"; while($row= mysql_fetch_row($result)) { echo "<tr>"; echo "<td>" . $row[0]. "</td>"; echo "<td>" . ' '. "</td>"; echo "<td>" . $row[1]. "</td>"; echo "</tr>"; } echo "</table>"; } else { echo "No rows found"; } ?> Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/ Share on other sites More sharing options...
AndyB Posted March 29, 2007 Share Posted March 29, 2007 The test for equality is ==. if (whatever == something) Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/#findComment-217359 Share on other sites More sharing options...
pak4eva Posted March 29, 2007 Author Share Posted March 29, 2007 The test for equality is ==. if (whatever == something) i've tried something similar but unforunately no luck..... Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/#findComment-217364 Share on other sites More sharing options...
AndyB Posted March 29, 2007 Share Posted March 29, 2007 similar? how 'similar'. Can you post the current version of your code and use the CODE tag to show it instead of all the strikethough stuff. Is the content of the field null or NULL or ... why not just have it empty. Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/#findComment-217372 Share on other sites More sharing options...
pak4eva Posted March 29, 2007 Author Share Posted March 29, 2007 This is what im currently using... <?php {$query = "Select Imagename1, Imagename2 From Element Where Symbol ='$_GET'"; IF (Imagename1 = "NULL") { echo("No image available"); } Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/#findComment-217391 Share on other sites More sharing options...
Lytheum Posted March 29, 2007 Share Posted March 29, 2007 You'll want to use IF (Imagename1 == "NULL") instead. When you're comparing things, always use '=='. Then.. $row = mysql_fetch_array($query); $imagename = $row['Imagename1']; if ($imagename == "NULL") { echo("No image available"); } Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/#findComment-217393 Share on other sites More sharing options...
pak4eva Posted March 29, 2007 Author Share Posted March 29, 2007 You'll want to use IF (Imagename1 == "NULL") instead. When you're comparing things, always use '=='. Then.. $row = mysql_fetch_array($query); $imagename = $row['Imagename1']; if ($imagename == "NULL") { echo("No image available"); } Thanks a lot mate, but considering my code below where do i need to insert the above? <?php $host ="?"; $user = "?"; $pass = "?"; $db= "?"; // Connects to your Database $connection = mysql_connect("$host", "$user", "$pass") or die("Unable to connect"); mysql_select_db("$db") or die("Unable to select database"); <?php {$query = "Select Imagename1, Imagename2 From Element Where Symbol ='$_GET'"; IF (Imagename1 = "NULL") { echo("No image available"); } } $result = mysql_query($query) or die ("error in Query: $query. " .mysql_error()); if (mysql_num_rows($result)>0) { echo "<table border=0 cellpadding=0 cellspacing=0>"; while($row= mysql_fetch_row($result)) { echo "<tr>"; echo "<td>" . $row[0]. "</td>"; echo "<td>" . ' '. "</td>"; echo "<td>" . $row[1]. "</td>"; echo "</tr>"; } echo "</table>"; } else { echo "No rows found"; } ?> Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/#findComment-217398 Share on other sites More sharing options...
Lytheum Posted March 29, 2007 Share Posted March 29, 2007 Replace IF (Imagename1 = "NULL") { echo("No image available"); } With it. Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/#findComment-217400 Share on other sites More sharing options...
pak4eva Posted March 29, 2007 Author Share Posted March 29, 2007 sorry to be pain mate but im getting an error "unexpected T_STRING on line......... the line its refereing to is $result = mysql_query($query) or die ("error in Query: $query. " .mysql_error()); Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/#findComment-217402 Share on other sites More sharing options...
Lytheum Posted March 29, 2007 Share Posted March 29, 2007 Oh, should be echo "No image available"; Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/#findComment-217404 Share on other sites More sharing options...
pak4eva Posted March 29, 2007 Author Share Posted March 29, 2007 i still get the same error mate, can you do me a favor please and insert that code into the my code so i can see how it work.... Thanks Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/#findComment-217409 Share on other sites More sharing options...
AndyB Posted March 29, 2007 Share Posted March 29, 2007 <?php {$query = "Select Imagename1, Imagename2 From Element Where Symbol ='$_GET'"; IF (Imagename1 = "NULL") { echo("No image available"); } } Heaven knows what that's all about. Why do you issue a second <?php when you're already in php code? What are the curly braces about??? Symbol = $_GET ... what? Where is the query executed, etc. Assuming this page is called from some link like mypage.php?Symbol=wombat, then your code would probably look like this ... $symbol = $_GET['Symbol']; $query = "Select Imagename1, Imagename2 From Element Where Symbol ='$symbol'"; $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); $row = mysql_fetch_array($result); if ($row['Imagename1'] == "NULL") { echo "No image available"; } else { // do something else } Link to comment https://forums.phpfreaks.com/topic/44768-php-sql-outputting-messages/#findComment-217440 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.