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:

$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($query, MYSQLI_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

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…

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

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?

Did you also change your query? Changing it to something wrong would result in that error.

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

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.

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

that would be exact reason where im going wrong! thank you for the help.