Web Analytics Made Easy -
StatCounter Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource - CodingForum

Announcement

Collapse
No announcement yet.

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

    Hi there, I am getting weird error on my site. the error is above in the subject and here is the code:

    code:

    function get_username($user_id){
    $query = "select * from registered_users where id=".$user_id;
    $result = mysql_query($query);
    --> if(mysql_num_rows($result) == 1){
    $row = mysql_fetch_array($result);
    if($row['display_real_names']=="N"){
    return $row['username'];
    }else{
    if($row['m_initial']==0){
    $row['m_initial'] = "";
    }
    $name = $row['f_name']." ".$row['m_initial']." ".$row['l_name'];
    if(trim($name)==""){
    return $row['username'];
    }else{
    return $name;
    }
    }
    }else{
    return "-NA-";
    }
    }

    the error stems from the line marked with the -->

    Thanks in advanced!
    Matt Sartain
    Internet Solutions Developer
    - Zonecom Communications

  • #2
    Usually means you're having a problem with your query.

    Comment


    • #3
      I'm not sure here, so if I'm totaly lost please forgive me... :-)

      I was looking at your query: $query = "select * from registered_users where id=".$user_id;

      Isn't the last " in wrong place? Shouldn't it be: $query = "select * from registered_users where id='$user_id'"; instead?
      [Windows took my virginity, Linux took my soul.]

      BoonZie

      Comment


      • #4
        The quoting is correct. an id is normally a numerical fields so no need to enclose the value in single quotes, and he is just concatenating the sql-string and the variable, which is fine.

        Try changing the code to
        PHP Code:
        $query "select * from registered_users where id=".$user_id;
        $result mysql_query($query) or die("Nightfire was right. There is something wrong with my query !\n
        executed query = " 
        $query "\nError =" mysql_error());
        if(
        mysql_num_rows($result) == 1){ 
        Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

        Comment


        • #5
          OK, I see...

          I have never seen the quoting like that before. Are there any differenses in performance, security or something else when writning like that instead of how I wrote, or is it just two ways of doing the same thing?
          I have also seen other ways of quoting in the forums here. Are there a "best" way of doing it?

          Must say it's very educating to hang around here...
          Thanks for your work guys
          Last edited by boonzie; Feb 20, 2004, 04:41 PM.
          [Windows took my virginity, Linux took my soul.]

          BoonZie

          Comment


          • #6
            In PHP, when double quoting anything, PHP will look through what is quoted for anything to parse. When using single quotes, PHP does not look for anything to parse, so from a technical perspective, single quotes (when used properly) reduce load on the parser and thus render output faster. Though this is not something most humans would notice (except for in massively large scripts, but that has issues of its own). Many people simply double quote everything in the SQL select statement and add single quotes as needed, though some people (like myself) find it better to break out what needs to be parsed and what doesn't:

            PHP Code:
            $query 'select * from registered_users where id=' $user_id ' LIMIT 1'
            Moderator, Perl/CGI Forum
            shadowstorm.net - subvert society

            Comment


            • #7
              Originally posted by Feyd
              ...some people (like myself) find it better to break out what needs to be parsed and what doesn't
              [/php]
              I completely agree. Always break out and concatenate.
              I always use single quotes (since i break out for variables and other parsed code), with one exception --> the sql-statements. Becasuse inside an sql statement, you often use single quotes ('), and you can include single quotes inside a doublequoted string, without needing to escape them.
              so you can do

              $sql="select var1 from table where var2='test'";
              which i find more elegant and better readable then
              $sql='select var1 from table where var2=\'test\'';
              + it allows untreated cut and paste just like that so you can run it inside a db-front to check it out
              Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

              Comment


              • #8
                add some error checking to let mysql tell you whats wrong , you may not want to die() in production code, but whilst developing its priceless.


                PHP Code:
                <?php
                $result 
                mysql_query($query) or die( "$query\n" mysql_error() ) ;
                ?>
                resistance is...

                MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

                Comment


                • #9
                  I see.
                  I must confess that I have never really understood the difference in ' and " in php, but now it starts to get clearer.
                  Another quotation that I don't understand is the \", when shall I use that?
                  I also have difficulties when I want to echo out a string with both html and some variables in php, and I guess that it is the quotation that I don't can figure out. If I would like to create a row in a table by echo a string like:
                  <tr><td>$var1</td><td>$var2</td><tr>
                  How shall I write that?
                  Is it also possible to mix javascript and php like that? I would like to use javascript to output an alert saying things like errormessages and that submitted files have been successfully uploaded and stuff like that. I would also like to have the texts as defined constants to be able to translate them in different languages. How shall I write that, if it's possible?
                  [Windows took my virginity, Linux took my soul.]

                  BoonZie

                  Comment


                  • #10
                    Originally posted by boonzie
                    I see.
                    I must confess that I have never really understood the difference in ' and " in php, but now it starts to get clearer.
                    Another quotation that I don't understand is the \", when shall I use that?
                    You use it when you want to, print a " inside a doubl-quoted string. Like
                    PHP Code:
                    echo ("<a href=\"foobar.php\" title=\"whatever\">test</a>"); 
                    But i then use
                    PHP Code:
                    echo ('<a href="foobar.php" title="whatever">test</a>'); 
                    Originally posted by boonzie
                    I also have difficulties when I want to echo out a string with both html and some variables in php, and I guess that it is the quotation that I don't can figure out.
                    either
                    PHP Code:
                    echo ("<a href=\"$urlsent\" title=\"$urlsenttitle\">test</a>"); 
                    or what i would use
                    PHP Code:
                    echo ('<a href="'$urlsent .'" title="' $urlsenttitle '">test</a>'); 
                    If you look at the colourcoding then you'll see why. But the realadvantages is that you seperate parsed and non parsed echoed code (see previous posts). If you write valid code (--> always use double quoted values for html attributes) then you can echo out a complete html page by just putting echo(' '); arrounf it.
                    Originally posted by boonzie
                    If I would like to create a row in a table by echo a string like:
                    <tr><td>$var1</td><td>$var2</td><tr>
                    How shall I write that?
                    PHP Code:
                    echo ("<tr><td>$var1</td><td>$var2</td><tr>"); 
                    or what i woul use
                    PHP Code:
                    echo ('<tr><td>' $var1 '</td><td>' $var2 '</td><tr>'); 
                    Originally posted by boonzie
                    Is it also possible to mix javascript and php like that? I would like to use javascript to output an alert saying things like errormessages and that submitted files have been successfully uploaded and stuff like that.
                    You can include it just like regular HTML. The same quotingrules apply.
                    Originally posted by boonzie
                    I would also like to have the texts as defined constants to be able to translate them in different languages. How shall I write that, if it's possible?
                    No idea what you mean. Best start a new thread for this since you all allready hijacking this one and that question is a complete new one.
                    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

                    Comment


                    • #11
                      Thank you for your answers, they have really helped me to start understand the quotation issues. I'll have to poke around with it for a while, and then I might get back to you.

                      Sorry for hijacking the thread, I just got so carried away...

                      I'll try to get more diciplined in the future
                      [Windows took my virginity, Linux took my soul.]

                      BoonZie

                      Comment

                      Working...
                      X