SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have the following problem:
    ---
    <?php
    mysql_connect("localhost","username","passw")
    or die ("unable to connect to database");

    mysql_select_db("db_name")
    or die ("unable to select database");

    // Query

    $sql = "SELECT * FROM medewerker";

    $result = mysql_query($sql);
    echo "$result";
    ?>
    ---
    What I get on the page is:
    ---
    Resource id #2
    ---

    I don't understand why I won't show all the records I have in the database.
    What am I doing wrong?

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    According to your code, $result is in fact an int.
    $result = mysql_query($sql);
    mysql_query() returns an int. This int is a handle ( a resource identifier) to the actual result set. So your are missing the code required to extract the data from the result set. You can only extract one row at a time from the result set, so you need to do this within a loop to extract all the rows.

    Below is an example of how to do this. I don't know what the names of the fields are in your table medewerker (sounds Deutsche?) so I'll assume there are three fields "foo", "bar" and "funk". Note my use of the function mysql_fetch_array($result). This returns an associative array that contains the next row of the result set.
    PHP Code:
    <?php 
    mysql_connect
    ("localhost","username","passw"
    or die (
    "unable to connect to database"); 

    mysql_select_db("db_name"
    or die (
    "unable to select database"); 

    $sql "SELECT * FROM medewerker"

    $result mysql_query($sql); 

    while ( 
    $row mysql_fetch_array($result)) {
       echo 
    $row["foo"] . $row["bar"] . $row["funk"] . '<br>';
    }
    ?>
    Hope that helps.

  3. #3
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot...
    it was exactly what I needed to make some progress!

  4. #4
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or...

    while ( $row = mysql_fetch_row($result)) {
    echo "$row[0] $row[1]<br>";//etc//
    }



    only mention it as I am a mysql_fetch_row() freak ! - mysql_fetch_row is 'supposed' to be faster than mysql_fetch_array() - not that I have ever noticed the difference!

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or ...
    PHP Code:
    while ( $row mysql_fetch_object($result)) { 
       echo 
    "$row->foo $row->bar <br>";//etc// 

    or ...
    PHP Code:
    while ( $row mysql_fetch_row($result)) { 
       
    extract($row);
       echo 
    "$foo $bar <br>";//etc// 

    Although I concede that technically mysql_fetch_row is supposed to be more efficient.


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
  •