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>
1

5 Answers 5

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; ?>
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

Don't forget htmlspecialchars
I put it in the code you suggested all within my php tag, and it only writes: "Successfully connected to database!." Any idea why?
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.
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

Comments

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'].'"/>';

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.