I wan't to do a search in a MySQL table. But some times that search will not give a result. How can I check if I got a valid result, and print one thing if I did get a valid result, or an other thing if I didn't.
Thanks
ØØ
Printable View
I wan't to do a search in a MySQL table. But some times that search will not give a result. How can I check if I got a valid result, and print one thing if I did get a valid result, or an other thing if I didn't.
Thanks
ØØ
this is basicly what i do
PHP Code:$result = mysql_query("SELECT * FROM table");
if($result)
{
//do something with the results
}
else
{
//didnt find anything or error
print mysql_error();
}
You can test whether the query succeeded or not by testing the result pointer returned from mysql_query(). If the query produces an empty result set however, it is not deemed to have failed. In which case you should use the mysql_num_rows() function.
PHP Code:$query = "Select * FROM tablename";
if (! $result = mysql_query($query)) {
die ('Query faliure: ' . mysql_error());
} else if (mysql_num_rows($result) == 0) {
/* code here to handle empty result set */
}
Hmmmm....that doesn't seem to work...what am I doing wrong. Here is that part of the code:
PHP Code:$resultat = mysql_query("select aAnmelderID, aNavn, aSign from anmelder WHERE aNavn LIKE '%".$rAnmelder."%'",$db);
if($resultat){
$row = mysql_fetch_array($resultat);
$aAnmelderID = stripslashes($row["aAnmelderID"]);
$aSign = stripslashes($row["aSign"]);
echo '<br><br><p class="dato"> anmelder <a href=redaksjonen.php?id=' . $aAnmelderID . '>' . $aSign . '</a>' .
'</p>';
}else{
echo '<br><br><p class="dato">test' . $rAnmelder . '</p>';
}
And there i no one (at least anymore) that has the name $rAnmelder in that table anymore. But still the if test under is TRUE for some reason...:(
ØØ
Yuor query is valid, so mysql_query() will always return a vlid result. Can oyu explain again because I don't understand what you mean :confused:
Quote:
Originally Posted by visualAd
Ohhh...didn't see your reply before now. Well, I am not testing for an invalide querery (I think), I am checkin if a table has info on a person called something. But if that guy has been deleted, I just want to check that I didn't get some result on his name and then print something else if we didn't find him. So I guess I have to got for the ROW thingy == 0 or soething.
ØØ
Just to clear out the confusion here:
We have a lot of articles on our old page. All the articles are stored in ONE MySQL table. It consists of date, topic, text and an authour (SP?). But the information about that authour is stored in an other table.
So what I wanted:
I wanted to use the name of the authour from the "article" table, querery the authour table and ask if he still was there. If he was, then I wanted to print a link to his info. If he wasn't, then I wanted to just print his name.
So the solutin was to use:
To test if we got an answer from the authour search, and then do an else branching if we didn't. Thats how simple it was. I am dumb, and havn't done this in a million years...:DPHP Code:if(mysql_num_rows($resultat) != 0){
Reps out.
ØØ
It is so much more simple than that. Yuo just need to use one query with a join:
If the author is in the table then it will be matched to that article. If it is not it will remain as NULL.Code:SELECT autor.name, author.id, article.name FROM articles LEFT JOIN
articles ,authors ON author.id=articles.author_id
WHERE
article.id = 1;