1

I am just starting to use html, php, and mysql. I've successfully logged into my database using php, and formed a query. I now want to go a step further and show a picture instead of just strings or numbers.

The variable 'result' will be returning a string that has a url to an image i want to display on this webpage. How would I do this?

<html>
<head>
<title>My First Question</title>
</head>
<body>

<?php

$dbhost = 'someURL.com';
$dbname = 'user';
$dbuser = 'user';
$dbpass = 'password';

$mysql_handle = mysql_connect($dbhost, $dbuser, $dbpass)
    or die("Error Connecting To Database Server");

mysql_select_db($dbname, $mysql_handle)
    or die("Error selecting database: $dbname");

echo 'Successfully connected to database!';

$first = 'bobbie';

$query = sprintf("SELECT image FROM Player 
    p WHERE name='%s'", mysql_real_escape_string($first));

$result = mysql_query($query);

mysql_close($mysql_handle);     

?>

</body>
</html>
Robert Bain
  • 395
  • 1
  • 7
  • 19
  • 7
    You are using [an obsolete database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Nov 01 '12 at 07:22

5 Answers5

10

Inside PHP, This will turn your SQL response into a usable variable.

$result = mysql_fetch_assoc(mysql_query($query));

Outside of your PHP tags, Echo the URL from the table into the SRC of an IMG element.

<img src="<?= $result['url_column_name'] ?>"/>

This will create a new IMG element with the source being the URL that you have fetched from your SQL query.

Short tags are also a way of echoing PHP variables in HTML.

<?= $var1, $var2 ?>

is the equivalent of using

<?php echo $var; echo $var2; ?>
Lix
  • 47,311
  • 12
  • 103
  • 131
Matt Clark
  • 27,671
  • 19
  • 68
  • 123
3

This is a simple case of echoing the relevant HTML. You'll also have to fetch the results after you execute the query -

$result = mysql_query($query);     
$data = mysql_fetch_assoc($result);
echo '<img src="'.$data['image'].'" />;

For added security, a good practice would be to escape any possible unwanted HTML content in your images path - htmlspecialchars($data['image']).

It should also be noted here that you are using a very old deprecated method to access your database. You might want to think about updating your code to use more modern PDO methods.

Lix
  • 47,311
  • 12
  • 103
  • 131
  • 1
    Don't forget `htmlspecialchars` – Quentin Nov 01 '12 at 07:23
  • I put it in the code you suggested all within my php tag, and it only writes: "Successfully connected to database!." Any idea why? – Robert Bain Nov 01 '12 at 08:23
  • After you execute `mysql_query()`, try and see if there were any errors. You can use `mysql_error()` to see if there were any errors on the last query executed. – Lix Nov 01 '12 at 08:28
2

So what? simply use it as a source to your image

<?php $imgname = mysqli_fetch_array($connection, $result); ?>
<img src="<?php echo $imgname['image_column_name']; ?>" />

And btw use mysqli_() or PDO instead of using mysql_() as community is not maintaining it anymore

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
1

Once you update your mysql to mysqli you can echo the image's url in an html img tag as so:

echo '<img src="'.$result['image'].'"/>';
Robert Bain
  • 395
  • 1
  • 7
  • 19
0
<?php

$dbhost = 'someURL.com';
$dbname = 'user';
$dbuser = 'user';
$dbpass = 'password';

$mysql_handle = mysql_connect($dbhost, $dbuser, $dbpass)
    or die("Error Connecting To Database Server");

mysql_select_db($dbname, $mysql_handle)
    or die("Error selecting database: $dbname");

$first = 'bobbie';

$query = sprintf("SELECT image FROM Player 
    p WHERE name='%s'", mysql_real_escape_string($first));

$result = mysql_query($query);

mysql_close($mysql_handle);     
header("Location: $result");
?>

should work

Ed Heal
  • 59,252
  • 17
  • 87
  • 127