Reading SQL database using PHP code.......

Discussion in 'PHP' started by CaseyC, Nov 1, 2007.

  1. #1
    Someone please help this 67 year old retired programmer out. I am teaching myself PHP and have run into a problem. It looked like a pretty plain vanilla thing to do, but I am really confused with the results.

    I have added a table (image_folder) to an existing SQL database. The table is made up of two fields: (1) an auto-incrementing field (Index), and a text field (img_dir) which contains a text pathname (i.e. /image/uploads/outdoor/). I have manually added 23 records to the table using phpMyAdmin.

    I am using the following code to retrieve the data:

    $query = "SELECT img_dir FROM ".$glob['dbprefix']."CubeCart_image_folder";
    echo $query."<br>";
    $result = mysql_query($query);
    echo "here<br>";
    echo $result[0];
    if(mysql_error()) {
    echo mysql_error() }

    The output from the first debug echo is:
    SELECT img_dir FROM CubeCart_image_folder (which is correct)

    The second debug echo does echo "here" to the screen.

    The third debug echo (attempting to see the first element retrieved) outputs nothing.

    There is no error output from the mysql_error echo.

    I would appreciate any suggestions on how to get this stuff out of the db.

    Thanks,
    Klaus Cook
    Houston, Texas:confused:
     
    CaseyC, Nov 1, 2007 IP
  2. Lordy

    Lordy Peon

    Messages:
    1,643
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this
    $result= mysql_query($query);
    while ($row = mysql_fetch_assoc($result))
    {
    echo $row['field_name'];
    }

    you can also use print_r to print all the keys => value in an array
     
    Lordy, Nov 1, 2007 IP
  3. CaseyC

    CaseyC Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you very much, Lordy......worked like a charm.
     
    CaseyC, Nov 1, 2007 IP