Results 1 to 8 of 8

Thread: No result from an MySQL call.

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Resolved No result from an MySQL call.

    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
    ØØ
    Last edited by NoteMe; Feb 11th, 2005 at 12:52 PM.

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: No result from an MySQL call.

    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();
     } 

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: No result from an MySQL call.

    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 */

    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: No result from an MySQL call.

    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...


    ØØ

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: No result from an MySQL call.

    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
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: No result from an MySQL call.

    Quote Originally Posted by visualAd
    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 */



    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.


    ØØ

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: No result from an MySQL call.

    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:

    PHP Code:
    if(mysql_num_rows($resultat) != 0){ 
    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...


    Reps out.

    ØØ

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: No result from an MySQL call.

    It is so much more simple than that. Yuo just need to use one query with a join:
    Code:
    SELECT autor.name, author.id, article.name FROM articles LEFT JOIN 
        articles ,authors ON author.id=articles.author_id
    WHERE
        article.id = 1;
    If the author is in the table then it will be matched to that article. If it is not it will remain as NULL.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width