Yahoo Answers is shutting down on 4 May 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
When does a php if statment not give an output either ways?
I have been trying out this code for a few hours now:
echo 'asdf';
if ($situation = mysql_fetch_array(mysql_query( "Select * from situation where situationID='".$id."'"))) echo 'pass';
else echo 'fail';
it displays asdf but that's it. It displays neither true nor false.
the connection is tested and connected.
id has the right value and is there inside table situation
situationID is the name of the id which is a var char
@liz i tried an echo with the sql statment and the statment is displayed fine
@King Boy i am sorry but now it does display something
3 Answers
- King BoyLv 59 years agoFavourite answer
Why do you expect for a "true" or "false" be ing printed?
First, echoing an expression of boolean in PHP will never result with a "true" or "false", if the expression is equal to "true", 1 will be printed otherwise nothing will be printed. Secondly, your code shows me that one of "pass" and "fail" should be surely printed. But nothing?
That's a bit strange. Except that your calls to mysql functions failed and threw some exception. Try using echo mysql_error() to know what it is. You can also want to try making your code simpler by writing it step by step. My experience tells me that one of your commands failed, otherwise "pass" or "fail" should be printed!
- just "JR"Lv 79 years ago
1. Add
error-reporting(E_ALL);
at the very beginning of your file. That will tell you all the mistakes you made.
2. I assume that you have open the DB and selected the db, of course
3. Do not mix your queries into one: bad habit! If something goes wrong, you can't find out why.
Rewrite (and take care of the syntax below!)
// define your query in a string:
$sql = "select * from `situation` where `situationID`='" . $id . "' ";
// Place the query:
$res = mysql_query($sql) or die ("Error: " . mysql_error() . "<br>SQL: " . $sql);
// this will report the syntax errors if you have any
// Read the results:
$row = mysql_fetch_array($res); // this assumes that it can only be ONE ID, otherwise, you need to use "while"
// now, you can display the results:
foreach ($row as $key => $value)
echo $key . " => " . $value . "<br>";
// don't forget to release the result!
mysql_free_result($res);
Source(s): http://web2coders.com/ - Free php scripts - Web development course - Php Forum - LizLv 79 years ago
echo "asdf <hr>"; // testing php is not having parse errors
$sql = "SELECT * FROM situation WHERE situationID= '{$id}' "; // some sql
$go = mysql_query($sql); // run the query
$row = mysql_fetch_array($go); // fetch the array
print_r($row); // print out the results so you can see whats up.
Source(s): (My guess is that you have waaay to much punctuation around $id :)