Reputation: 147
In my Details table, when the complete field is '0' I want a certain image to appear when it is '1' I want a different image to appear. At the moment only the second image is showing (when complete field is 1) This is the code I am using:
$id = $_SESSION['user_id'];
$isFinished= mysql_query("SELECT complete From details where user_id = $id") ?>
<p> <img src='<?php if($isFinished) echo "correct.png"; else echo "incorrect.png"; ?>' /><a href="details.php">Details</p>
Any ideas would be great!
Upvotes: 0
Views: 137
Reputation: 1982
Is this the whole code?
In this case $isFinished is not the content retreived from db: mysql_query returns false if query is wrong otherwise an object where you can fetch the results.
In this case if($isFinished) is always true as the query is correct!
you miss the fetch part ater query execution!
http://php.net/manual/en/function.mysql-query.php
For example:
$id = $_SESSION['user_id'];
$result = mysql_query("SELECT complete From details where user_id = $id");
$isFinished= mysql_num_rows($result);
<p> <img src='<?php if($isFinished) echo "correct.png"; else echo "incorrect.png"; ?>' /><a href="details.php">Details</p>
This way if query returns one ore more rows then $isFinished is 1 or more otherwise $isFinished is 0. Your if - else should then work properly
Note i did not change the your original SQL, change it if you need.
Upvotes: 0
Reputation: 158007
the function
function getOne($query){
$res = mysql_query($query);
if (!$res) {
trigger_error("db: ".mysql_error()." in ".$query);
return FALSE;
}
if ($row = mysql_fetch_row($res)) {
return $row[0];
}
}
the code
$sql = "SELECT complete From details where user_id = ".intval($_SESSION['user_id']);
$isFinished = getOne($sql);
?>
<p>
<img src='<?php if($isFinished): ?>correct.png<? else: ?>incorrect.png<? endif ?>' />
<a href="details.php">Details</a>
</p>
Upvotes: 1
Reputation: 2671
The mysql_query
function returns false
only when the query is invalid. It does not return false if there are zero rows.
You should use the mysql_num_rows
function to determine if there was a row or alternatively use the mysql_fetch_*
functions to fetch the value of complete.
Example 1 (Similar to your original (unedited) question):
$result = mysql_query("SELECT 1 From details where user_id = $id where complete = 1");
$isFinished = mysql_num_rows($result);
Example 2 (Alternative):
$result = mysql_query("SELECT complete From details where user_id = $id");
$record = mysql_fetch_assoc($result);
$isFinished = $record['complete'];
Upvotes: 0
Reputation: 4170
Your SQL statement only selects entries with complete='1'. You should remove the "and complete='1'" from your statement.
Edite: ALso as mentioned above your if statement only check if your query returned something or got an error.
It should be
if($isFinished['complete'] == '1') {echo "correct.png";}else{echo "correcy.png";}
Upvotes: 1
Reputation: 4308
It should be something like this:
$id = $_SESSION['user_id'];
$result= mysql_query("SELECT complete From details where user_id = $id and complete='1'") ?>
$row = mysql_fetch_array( $result );
<p>
<?php if($row['complete']): ?>
<img src="correct.png"/>
<?php else: ?>
<img src="incorrect.png"/>
<?php endif; ?>
<a href="details.php">
Details</a>
</p>
Upvotes: 0
Reputation: 13285
The WHERE
clause of your query is specifying only select records where complete
= '1'
So you'll only get 1
back in the results.
You should remove that from the WHERE
clause completely and let your php IF statement decide whether complete = '1'
Upvotes: 2