SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Senior Webdesigner koolbrian's Avatar
    Join Date
    Dec 2000
    Location
    Mauritius
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    is ARRAY commands obliged to be used?

    Hey,
    as a very newbie, i've tried to show a database content on a page, i've put the code that follows
    PHP Code:
    <?php

    mysql_select_db
    ("Jokes");

    $db mysql_query("Select * from Jokes");

    if (!
    $db) {
    echo (
    "sorry, the database wasn't found!");
    }
    else {
    echo (
    "Here are is the database: $db ");

    }

    ?>
    FOrget about the connect commands, and it appears like this on the page,

    " Here are is the database: Resource id #2 "


    I'm sure i should use the array command to display the contents, but i'm i OBLIGED ?, are array commands important ?

    Brian
    BrianStudio - the webdesign company
    Professional & Affordable
    Brian Dove - Senior Graphic Designer
    Direct Hire: info@brianstudio.com

  2. #2
    SitePoint Member
    Join Date
    Jun 2001
    Location
    UK
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_query : simply tells mysql to perform a query, it does not ask it to return the results.

    You are not, techically obliged to use arrays to obtain results from mysql, however it's the best solution i know. To return the results look @ mysql_fetch_array or mysql_fetch_row in the php manual.

    -dibby

  3. #3
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    don't forget mysql_fetch_object()
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You have two choices for getting data out of a table and displaying it: using arrays(mysql_fetch_array();mysql_fetch_row()) or using objects(mysql_fetch_object()).

    Even if arrays were not automatically used in the retrieving of data, you would create them yourself to make things easier. For instance, would you prefer this:

    $field1 = mysql_query("SELECT field1 FROM table WHERE id='1'");
    $field2 = mysql_query("SELECT field2 FROM table WHERE id='1'");
    $field3 = mysql_query("SELECT field3 FROM table WHERE id='1'");
    $field4 = mysql_query("SELECT field4 FROM table WHERE id='1'");
    ....
    ....
    $field10 = mysql_query("SELECT field10 FROM table WHERE id='1'");
    Last edited by 7stud; Jun 7, 2001 at 09:59.

  5. #5
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also extract() the data and use plain ol' scalars.

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    --
    Last edited by 7stud; Jun 6, 2001 at 22:57.

  7. #7
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It appears that you're attempting to show everything in the Jokes database?

    It looks as though you were trying to print the whole database, without saying what to print out.

    If that's the case, use something like:

    PHP Code:
    $sql mysql_query("SELECT * from Jokes") or die (mysql_error());

    while (
    $row mysql_fetch_array($sql));
    {
        
    extract($row);
        echo 
    "$column_name$another_column_name";

    Last edited by Robo; Jun 7, 2001 at 00:32.

  8. #8
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7stud,

    While you have an excellent grasp upon the theory, the example was very ineffecient. Remember SQL is your friend, it has tons of absolutally awesome functions and controls.

    To accomplish the same in a lot less time, and less code, because all we're doing is setting those vars.

    while($results = mysql_fetch_array(mysql_query("
    SELECT field1,field2, field3, field10
    FROM table WHERE
    id='1'"))){
    extract($results);
    }

    There! That's it! One database query, four vars. Not being critical, just trying to help.
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Aahhh..yes, but you used arrays in your example(mysql_fetch_array()), and I was trying to demonstrate what a script would look like with a language that did not provide arrays .i.e how inefficient and long it would be.
    Last edited by 7stud; Jun 8, 2001 at 00:17.


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
  •