SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Retrieving text from the database

    i have an upload form with a text area and when the user uploads his image and writes in the description the text gets uploaded to the database all fine.the problem im having is not knowing how to get the text from the database and display it in its own <p>. what is happening at the moment is when the user clicks a thumbnail it takes them to a page called image.php which detects what id the image is and displays that correct image. what i need it to do is display the correct text for the correct id.

    the image.php form looks like this:

    PHP Code:
    <?php
    $dbLink 
    = new mysqli('localhost''root''''gallery');
    if(
    mysqli_connect_errno()) {
    die(
    "MySQL connection failed: "mysqli_connect_error());
    }

    if(isset(
    $_GET['id']) && is_numeric($_GET['id'])) {

    $id$dbLink->real_escape_string($_GET['id']);
    } else {

    $id 53;
    }



    $query "SELECT name, size, image_path FROM images WHERE id = $id";

    $result $dbLink->query($queryMYSQLI_STORE_RESULT); 

    while(
    $row $result->fetch_row()) {
    echo 
    "<img src=\"" $row[2] . "\" />"// you could also play with the other $row[] variables returned by the query here.
    }

    ?>
    i am not very good at php and im pretty sure this is a very basic thing to do but i cant get my head around it.

    thanks for listning

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    In your SELECT query, add the column that holds the description.

    I would recommend using fetch_array instead of fetch_row, so you don't need to count numeric indices...

    PHP Code:
    while($row $result->fetch_array()) { 
        echo 
    '<img src="' $row['image_path'] . '" />';
        echo 
    '<p>' $row['description'] . '</p>';


  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the reply but i get this error now:

    Fatal error: Call to a member function fetch_array() on a non-object in C:\wamp\www\Blean_Photos\image.php on line 66

    any ideas?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Did you also change your query? Changing it to something wrong would result in that error.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my query now looks like this:

    $query = "SELECT name, size, image_path, desc FROM images WHERE id = $id";

    where desc is where the description of the image is stored

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The problem is that DESC is a reserved word in SQL (it means descending order, as in "ORDER BY something DESC"), so you're not supposed to use that as a name. You can either change the name of your column to something else (like "description"), or every time you write a query involving that column, put backticks around the name.

    Code:
    $query = "SELECT name, size, image_path, `desc` FROM images WHERE id = $id";

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that would be exact reason where im going wrong! thank you for the help.


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
  •