Jump to content

Archived

This topic is now archived and is closed to further replies.

corillo181

:|????

Recommended Posts


why is this not working.. if there is a title it show it but if the tittle is empty it does not show the no title
[code]
<?php
$getdes=mysql_query("SELECT descrive FROM tra_photo WHERE photo_id=$foto_id")or die(mysql_error());
$row=mysql_num_rows($getdes);
$fet=mysql_fetch_array($getdes);
if(!row){
echo "Not Title";
}else{
echo $fet['descrive'];

}
?>
[/code]

Share this post


Link to post
Share on other sites
not fully awake yet, but...

a missing $ perhaps?

[code]if(!row){[/code]

Share this post


Link to post
Share on other sites
mm that is the point of the if statement, if there is something is going to echo that something if there is nothing is going to say no tittle..

when there is something it works and echos what ever is in the query, but when ther eis nothing it does not show the no tittle..

Share this post


Link to post
Share on other sites
Don't get me wrong, i am NOT by any stretch a guru; but my understanding of

[code]$row=mysql_num_rows($getdes);[/code]

is that it counts the number of rows that are returned. Thus even if the number is 0, it does have a value. in other words it is NOT a TRUE/FALSE variable. IF that is correct then (and again my understanding may be wrong), this

[code]if(!row){[/code]

says "Is there are value in $row". Oviously if I am correct so far in my thinking, the answer is "Yes, the value is 0". Moving along, since the answer is "yes" the script says "display the row contents"; however since there is NO row that met the conditions it will display nothing.


I may be all wet but remember I am simply an old fart learning as I go and totally sefl taught.

Lite...


Share this post


Link to post
Share on other sites
I took the liberty to rewrite it:
[code]<?php

$getdes = mysql_query("SELECT descrive FROM tra_photo WHERE photo_id='$foto_id' LIMIT 1")or die(mysql_error());

if (mysql_num_rows($getdes) == 0) {
   echo "Invalid Id";
} else {
   $fet = mysql_fetch_array($getdes);
   $title = empty($fet['descrive']) ? 'No Title' : $fet['descrive'];
}

echo '<title>' . $title . '</title>';

?>[/code]
I added singlequotes (='$foto_id') because MySQL won't work without them here. Also LIMIT 1 to ensure it returns 1 row only.

And, I made it fetch the results only if there is a row. $title is determined by the ternary operator.

Share this post


Link to post
Share on other sites

×

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.