Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    I am just starting to learn PHP and it is also my first programming language I have tried to tackle. So I am trying to understand every aspect of code I use(versus stealing it and not knowing how it works). Below i have a piece of code that counts how many images i have in a database table. My question is: what is the reason for the brackets "[0]" at the end of $numguest? It doesn't seem to be an array to me, but I guess it is. Can anyone help me with this?
    -Eric


    PHP Code:
    <?php 
    $query 
    "SELECT COUNT(*) FROM photos WHERE img_name = '$img_name'";
    $numguests mysql_query($query) or die("Select Failed!");
    $numguest mysql_fetch_array($numguests);

    $total_img $numguest[0];

    echo 
    $total_img
    ?>
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    You're accessing the first element of an array.

    To see what's in the array, do this:
    Code:
    echo "<pre>";
    print_r($numguest);
    echo "</pre>";
    Technical: it's an associative array where each key is the name of the column in the query.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Because you're fetching a single record from the database and accessing it in an array, you are required to ask for the first record (PHP arrays begin their indexing at 0 and increment from there).

    You could optionally do something like this to fetch the record by an associative name that you assign to the column in your SQL query:
    PHP Code:
    <?php 
    $query 
    "SELECT COUNT(*) AS total_images FROM photos WHERE img_name = '$img_name'";
    $numguests mysql_query($query) or die("Select Failed!");
    $numguest mysql_fetch_assoc($numguests);

    $total_img $numguest["total_images"];

    echo 
    $total_img
    ?>
    If you study the example I've given above, you can see the changes I made to allow this. Important to note the change of mysql_fetch_array to mysql_fetch_assoc

  5. #4
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    mysql_fetch_array should work as well, although it returns the numeric keys as well as the associative keys. Which I suppose makes mysql_fetch_assoc better because it saves memory.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:40 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com