Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    13
    Rep Power
    0
    Ok, using PH4, how does one retrieve data from a table?

    lets say for example, your making a guestbook (hehe, MySQL would be overkill, but, thats besides the point), and you have a table, auto incremented id, and these columns: name, ip, message.

    How would you go about returning the data from them all?

    this is what I currently use:

    $db = "sample";
    $mysql = mysql_connect($host, $user, $pass);


    if(!$mysql)
    die (print "Could not connect to MySQL server!");

    mysql_select_db($db, $mysql)
    or die (print "Could not select MySQL DB:" .mysql_error());

    $query = "INSERT INTO forum (name, mail, post) values ('$web_user', '$email', '$message')";

    mysql_query($query, $mysql)
    or die (print "Could not add data to table:" .mysql_error());

    $result = mysql_query("SELECT * FROM forum");
    $num_rows = "mysql_num_rows($result)";


    while($a_row = mysql_fetch_row($result))
    {
    foreach($a_row as $field)
    print "$field<BR>";
    print "<HR><BR>";
    }

    mysql_close($mysql);

    the problem with this method is that my output is numbered, the ID# of each and every entry is displayed....I dont want that.

    I read DevShed's MySQL beginner Tut. but it failed to really tell me how to get data out of the DB....

    Any help would be greatly appreciated....
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Posts
    108
    Rep Power
    15
    Hey.

    $myrow = mysql_fetch_row($result)
    To get one column of that row, use
    $myrow[0] or $myrow[1], etc.

    I hope that helps!

    yoshi
    datera@datera.com http://www.datera.com/
    Tour The Web!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    61
    Rep Power
    14
    $query = mysql_query("SELECT * FROM table;");
    $num = mysql_num_rows($query);

    for($i=1; $i<=$num; $i++)
    {
    $query2 = mysql_query("SELECT * FROM table WHERE ID = '$i';");
    $fetch = mysql_fetch_row($query2);

    echo $fetch[0];
    }

    there you go. that'll get everything from the table. each element in the fetch array is the column at that row.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    13
    Rep Power
    0
    he ok, the table has these columns

    id int not null auto_increment
    primary key (id)
    name varchar(20)
    ipaddr varchar(20)
    email varchar(20)
    post blob

    how do I get it to print out everything BUT the id/primary key? (maybe you have answered this and Iam just to stupid to catch it
  8. #5
  9. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,640
    Rep Power
    4476
    It all starts with your SELECT statement. You're getting everything because you're selecting everything. Only select what you want.

    SELECT name,ipaddr,email,post FROM your_table

    So you'd do something like this to retrieve all of the data...

    <?php
    $link_id = mysql_connect("localhost","user","password");
    $db = mysql_db_select("your_database");

    $result = mysql_query("SELECT name,ipaddr,email,post FROM your_table");
    while ($row = mysql_fetch_array($result))
    {
    echo "Name: $row[name]<BR>n";
    echo "IP Address: $row[ipaddr]<BR>n";
    echo "Email Address: $row[email]<BR>n";
    echo "Message: $row[post]<BR>n";
    //just for fun, we'll show name
    //again, this time in bold.
    echo "<b>$row[name]</b><BR>n";
    echo "<HR>n";
    }
    ?>

    That will print out everything with a horizontal line between them all. If nothing is in the table, nothing is printed out.

    There are tons of methods of getting the data out using many different functions. Read the manual on all of the MySQL functions and array functions...

    Hope that helps.

    ---John Holmes...



    ------------------
    *************************************************************
    * The manual can probably answer 90% of your questions...
    *
    * PHP Manual. www.php.net/manual
    * MySQL Manual: www.mysql.com/documentation/mysql/bychapter
    *************************************************************
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    13
    Rep Power
    0
    would it be too forward of me to say I love you?

    thanks
  12. #7
  13. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,640
    Rep Power
    4476
    Gee, I hardly know you...

    Don't love me, love the manual.

    ---John Holmes...
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    13
    Rep Power
    0
    lol I didnt mean in a Richard Simons/Prison Sex kind of way

    More like the "can I please have your Bud Light" kind of way
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    13
    Rep Power
    0
    that doesnt work

    this is what it outputs


    Name: name
    IP Address: ipaddr
    Email Address: email
    Message: post
    name
  18. #10
  19. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    18
    Did you copy John's code exactly?

    Post the exact code (cut and paste) that is producing these results.
  20. #11
  21. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,640
    Rep Power
    4476
    You CAN NOT have my Bud Light.
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    4
    Rep Power
    0
    instead of this:

    echo "Name: $row[name]<BR>n";
    echo "IP Address: $row[ipaddr]<BR>n";
    echo "Email Address: $row[email]<BR>n";
    echo "Message: $row[post]<BR>n";

    try:

    echo "Name: $row["name"]<BR>n";
    echo "IP Address: $row["ipaddr"]<BR>n";
    echo "Email Address: $row["email"]<BR>n";
    echo "Message: $row["post"]<BR>n";
  24. #13
  25. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    13
    Rep Power
    0
    it gives me a "Expecting T_STRING" error....

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by SepodatiCreations:
    It all starts with your SELECT statement. You're getting everything because you're selecting everything. Only select what you want.

    SELECT name,ipaddr,email,post FROM your_table

    So you'd do something like this to retrieve all of the data...

    <?php
    $link_id = mysql_connect("localhost","user","password");
    $db = mysql_db_select("your_database");

    $result = mysql_query("SELECT name,ipaddr,email,post FROM your_table");
    while ($row = mysql_fetch_array($result))
    {
    echo "Name: $row[name]<BR>n";
    echo "IP Address: $row[ipaddr]<BR>n";
    echo "Email Address: $row[email]<BR>n";
    echo "Message: $row[post]<BR>n";
    //just for fun, we'll show name
    //again, this time in bold.
    echo "<b>$row[name]</b><BR>n";
    echo "<HR>n";
    }
    ?>

    That will print out everything with a horizontal line between them all. If nothing is in the table, nothing is printed out.

    There are tons of methods of getting the data out using many different functions. Read the manual on all of the MySQL functions and array functions...

    Hope that helps.

    ---John Holmes...

    [/quote]

  26. #14
  27. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2000
    Posts
    13
    Rep Power
    0
    This is what John said to use:

    <?php
    $link_id = mysql_connect("localhost","user","password");
    $db = mysql_db_select("your_database");

    $result = mysql_query("SELECT name,ipaddr,email,post FROM your_table");
    while ($row = mysql_fetch_array($result))
    {
    echo "Name: $row[name]<BR>n";
    echo "IP Address: $row[ipaddr]<BR>n";
    echo "Email Address: $row[email]<BR>n";
    echo "Message: $row[post]<BR>n";
    //just for fun, we'll show name
    //again, this time in bold.
    echo "<b>$row[name]</b><BR>n";
    echo "<HR>n";
    }
    ?>

    what I NEEDED to use was this:

    <?php
    $link_id = mysql_connect("localhost","user","password");
    $db = mysql_db_select("your_database");

    $result = mysql_query("SELECT name,ipaddr,email,post FROM your_table");
    while ($row = mysql_fetch_array($result))
    {
    echo "Name: $a_row[name]<BR>n";
    echo "IP Address: $a_row[ipaddr]<BR>n";
    echo "Email Address: $a_row[email]<BR>n";
    echo "Message: $a_row[post]<BR>n";
    //just for fun, we'll show name
    //again, this time in bold.
    echo "<b>$a_row[name]</b><BR>n";
    echo "<HR>n";
    }
    ?>

    And actually I didnt go with $a_row[fieldname] I just went with the number of the row, like for example, name was the first field after the PRIMARY KEY field, making it #1

    so
    print $a_row[1];
    worked....
    Thanks for the input guys, finally got what I needed
  28. #15
  29. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,640
    Rep Power
    4476
    ????????

    $row = $a_row????

    did you change
    while ($row = mysql_fetch_array($result))
    into
    while ($a_row = mysql_fetch_array($result))
    also?????

    Using the numbers or the names works. it just makes your code easier to read if you use the names. You shouldn't have to use quotes around the names in the array, either.

    $row["name"]
    and
    $row[name]

    both work. they do for me at least.

    ---John Holmes...
Page 1 of 2 12 Last
  • Jump to page:

Similar Threads

  1. PHP data retrieval question with MySQL
    By gursharn in forum PHP Development
    Replies: 7
    Last Post: December 31st, 2003, 09:43 AM
  2. beginner question...
    By mosley in forum HTML Programming
    Replies: 2
    Last Post: December 8th, 2003, 03:28 PM
  3. document.all question (beginner)
    By mosley in forum HTML Programming
    Replies: 1
    Last Post: December 4th, 2003, 09:51 AM
  4. Javascript question mark question?
    By tajmiester in forum JavaScript Development
    Replies: 3
    Last Post: November 30th, 2003, 05:36 PM
  5. Radio Buttons
    By Ulysses Freeman in forum HTML Programming
    Replies: 4
    Last Post: November 15th, 2003, 09:57 PM

IMN logo majestic logo threadwatch logo seochat tools logo