Multiple queries with mysql and php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whitep8
    New Member
    • Oct 2009
    • 65

    Multiple queries with mysql and php

    Hi all,

    Im really baffled with this one. I have 2 tables that i simply wish to row count and display. the first works fine, but i just cant get the second to work, even if i rename the results variable

    the code is below

    Code:
    <?php
    
    $link = mysql_connect("localhost", "un", "pw") or die('error 1<hr>'.mysql_error());
    mysql_select_db("db", $link) or die('error 2<hr>'.mysql_error());
    
    $result = mysql_query("SELECT ID FROM tbl_sellers", $link) or die('Query1 Error<hr>'.mysql_error());
    $num_rows = mysql_num_rows($result);
    
    echo "$num_rows Private Seller records\n";
    echo "<br>\n";
    
    
    $result = "";
    
    $result2 = mysql_query("SELECT id FROM index-peaform", $link) or die('Query2 Error<hr>'.mysql_error());
    $num_rows = mysql_num_rows($result2);
    
    echo "$num_rows records\n";
    echo "<br>\n";
    mysql_free_result($result2);
    
    mysql_close($link);
    
    ?>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    why don’t you use MySQL’s internal row counter?
    Code:
    SELECT COUNT(`id`) FROM table

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      I find aliasing the COUNT makes it easier to access.

      Code:
      $result = mysql_query('select count(*) as `count` from `table`');
      echo mysql_fetch_object($result)->count;
      * NOTE: PHP4 dislikes using the dereferencer (->) on non-variables, including directly on return values.

      Comment

      • whitep8
        New Member
        • Oct 2009
        • 65

        #4
        hi,

        Thanks for the responses.

        I dont think ive been clear. I understand the counts, but my problem is that the second query returns only an error. The table exists, and there is data to count, but all i get is the same error.

        4 Private Seller records
        Query2 Error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index-peaform' at line 1

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #5
          It's a good idea to always surround table and column names with "`" characters. Also, it is standard to use underscores instead of dashes for this exact reason. SQL sees dashes as the subtraction operation, not as a character in a name.

          Comment

          • dgreenhouse
            Recognized Expert Contributor
            • May 2008
            • 250

            #6
            Ding, ding, ding... And the prize goes to kovik!

            Comment

            • whitep8
              New Member
              • Oct 2009
              • 65

              #7
              Hi,

              Thank you all for your responses but my question is regarding the code in that the first query runs, the second one doesnt.

              I dont see how your responses relate to my question

              Comment

              • kovik
                Recognized Expert Top Contributor
                • Jun 2007
                • 1044

                #8
                Really, ~whitep8? Did you even read my response...?

                Basically, replace this:
                Code:
                SELECT id FROM index-peaform
                With this:
                Code:
                SELECT `id` FROM `index-peaform`
                And like I said, for future reference, DO NOT USE DASHES in table names or column names. Use underscores.

                Comment

                Working...