SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql_num_rows()

    Can anybody see what's wrong here?

    It doesn't give an error or work.

    Code PHP:
      $query1 = mysql_query("SELECT * FROM naturehills") or die(mysql_error());
      $num_of_rows_naturehills = mysql_num_rows($query1) or die(mysql_error());
     
      $query2 = mysql_query("SELECT * FROM naturehills_load") or die(mysql_error());
      $num_of_rows_maintable = mysql_num_rows($query2) or die(mysql_error());
     
      $num_of_rows_maintable - $num_of_rows_naturehills = $total_new_items_added;

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) have you confirmed you are connected to the database?

    2) put

    Code:
     
    echo 'got here';
    die();
    just above your code to confirm your script actually gets there.

  3. #3
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it connected because this code is added to the bottom of database update. ...and the database updates!

  4. #4
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what about 2) then ?

  5. #5
    SitePoint Enthusiast LSC-Rob's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, NSW Australia
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Freejoy View Post
    PHP Code:
    $num_of_rows_maintable $num_of_rows_naturehills $total_new_items_added
    PHP Code:
    $total_new_items_added $num_of_rows_maintable $num_of_rows_naturehills
    Defining a variable needs be done before assigning a value to it.
    Rob G.
    ShopManager - Sales & Repair Business Management Software

  6. #6
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your talking about the two ")" I didn't add or die(mysql_error()); until after it didn't work. When I remove one like this or die(mysql_error(); I get the following error.

    Parse error: syntax error, unexpected ';' in /homepages/18/d346921776/htdocs/Home/4742/4742_db_load.php on line 155

  7. #7
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This doesn't work either???

    Code PHP:
     $num_of_rows_maintable - $num_of_rows_naturehills = $total_new_items_added;
    	// Query the database and get the count
      $query1 = mysql_query("SELECT * FROM naturehills") or die(mysql_error());
      $num_of_rows_naturehills = mysql_num_rows($query1) or die(mysql_error());
     
      $query2 = mysql_query("SELECT * FROM naturehills_load") or die(mysql_error());
      $num_of_rows_maintable = mysql_num_rows($query2) or die(mysql_error());

  8. #8
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After put the variables on top I think its working now .

    Thanks!

  9. #9
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Freejoy View Post
    Can anybody see what's wrong here?
    i can

    you're pulling in all the rows from the database tables, and counting them in php

    it's ~far~ more efficient to let mysql count them, and return just the count to you

    heck, you can even do it in one query:
    Code:
    SELECT ( SELECT COUNT(*) 
               FROM naturehills_load ) -
           ( SELECT COUNT(*) 
               FROM naturehills ) AS total_new_items_added
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  10. #10
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's better!

    At my level on the learning curve I feel lucky if it works at all.

  11. #11
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    and if they are myisam tables, that query comes back in only a few thousandths of a second, because mysql maintains current counts for myisam tables

    it's kind of like the following analogy i'm very fond of...

    suppose you and i are sitting in an office in new york, and i ask you to find out the population of los angeles

    one way is for you to get everybody in los angeles to fly over to new york, and you count them as they get off the plane(s)

    the other way is for you to go to los angeles, count them there, and come back with the answer

    which do you think is more efficient?

    and if it's a myisam table, it's even more efficient, you simply go to los angeles, look up the number in the population registry, and come back immediately

    in a database environment, shipping the entire contents of a table between the database and the application is a huge bottleneck (just as putting people on a plane is)

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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