Okay, so my end goal is to display an image (or multiple in a blog type style) that is stored in a database(MySQL) as a blob. Right now it display a broken image icon (). This is what I've tried so far:
<?php
$link = mysql_connect('HOST','USER','PASS!') or die("Could not connect to database");
$dbsel= mysql_select_db('DATABASE', $link) or die("Couldn't select database.");
$result = mysql_query("SELECT * FROM bmblog");
while($row = mysql_fetch_assoc($result))
{
echo "<img src='php/imgView.php?imgId=".$row['media']."' />";
echo "<center>" . "<font color='white'>" . "<FONT FACE='timesnewromans'>" . nl2br(htmlspecialchars($row['msg']));
echo "<br>";
echo "<br>";
};
?>
Also
echo "<img src='data:image/jpeg;base64," . base64_encode($row['media']) . '" />';
echo "<center>" . "<font color='white'>" . "<FONT FACE='timesnewromans'>" . nl2br(htmlspecialchars($row['msg']));
Both display the same broken image icon. I did notice however without trying to echo the second line (of text) I did get a full boarder around the broken image icon that would have been about the size of the image. This is not the same for the first method though.
I've been searching for a few hours now and have found tons of post about this, but none that seemed to work for me or make sense, I am fairly new to PHP so this may be something simple I am missing; Either way thank you in advance for any help it is greatly appreciated!
What might solve the problem is the to add header("Content-Type: image/jpeg"); just before you print/echo the image.
Also it is strange that base64 example you included would not have worked. When I looked at the example provided, the first line of the Also section the echo opens with double quote(") and end with single quote('). This might cause exception and why you couldn't test base64 properly.
It must be:
echo "<img src='data:image/jpeg;base64," . base64_encode($row['media']) . '" />";
You can also add a charset: data:image/jpeg;charset=utf-8;base64,
Lastly, I would suggest looking at the following example that might help
How to Base64 encode your images
Related
I need to figure out how to query several columns of data into a table on my webpage using PHP and get one column to display as clickable audio files. In my MySQL table the audio column consists of absolute paths to audio files saved in a folder on the disk of my web host. All attempts so far have only succeeded in querying the file path as text, not as clickable audio.
I've been using a PHP loop statement to try to query several data columns into a table, like this:
<?php
$sql = "SELECT id, word, audio FROM dictionary";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["word"]. " " . $row["audio"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Here's a photo of my table with one record in it:
enter image description here
But I've also tried the URL in place of the absolute path: https://memoriaelinguagrumentina/dictionaryAudio/a1.com
In either case, the path displays as text.
Any suggestions on how to get the audio to display on my webpage as something clickable?
You have to output the $row["audio"] as a HTML a tag, but the absolute path is not always the path the file is accessible from outside.
The PHP code should look like something like this:
echo "id: ".$row["id"]." - Name: ".$row["word"]." <a href='".$row["audio"]."'>Click here to play audio</a><br>";
But for example if your audio file's path is /var/www/website/files/01.mp3 this isn't the path that can be used as a http link. You should convert it to a useable URL. If in the above example, your domain is website.com and your document root on the server is the /var/www/website you should do something like this:
func FilePathToURL($fp) {
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
return $protocol."://website.com".substr($fp, strlen("/var/www/website"));
}
And use this function to echo the correct link like:
echo "<a href='".FilePathToURL($row['audio'])."'>Click here</a>";
You would just need to change it so the echo that you output to the page is wrapped in an anchor tag something like this. You'll need to add in the href where it is going to be going based on your data tho.
echo "<a href=''>" . "id: " . $row["id"]. " - Name: " . $row["word"]. " " . $row["audio"] . "</a>" . "<br>";
Since your audio values begin with "/public_html/...", you need to remove that leading string since presumably your web root already points there:
echo 'id: ' . $row['id'] . ' - Name: ' . $row['word'] . '<br>';
This should result in HTML that looks something like:
id: 1: - Name: word<br>
I am working on a large project and ran into a problem where I can only display one video from a table of several videos. The first video displays correctly, but the WHILE loop to display the additional videos does not produce any result and seems to end the PHP code altogether.
I have removed all non-essential elements of the code I am currently working on with the exception of comments. Similar code is working elsewhere in my project for images, but this code will not seem to work when displaying videos.
<?php
include_once '../includes/db_connect.php'; // THIS CONNECTS THE DATABASE
include_once '../includes/functions.php'; // THIS PROVIDES VARIOUS FUNCTIONS
sec_session_start();
// THIS ENABLES ERROR MESSAGE DISPLAY //
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
// THIS QUERIES ALL COLUMNS FROM THE 'VIDEOS' TABLE //
$query = mysqli_query($mysqli,"SELECT * FROM videos");
$result = mysqli_fetch_array($query);
// THESE TWO LINES WILL DISPLAY THE FIRST VIDEO IN THE TABLE //
echo "<br><br>" . $result['name'] . "<br>";
echo "<video src='" . $result['location'] . "' controls width='320px' height='200px' >";
while($result = mysqli_fetch_array($query))
{
// THIS SHOULD DISPLAY THE VAR_DUMP INFORMATION //
echo "<br> ******************** TROUBLESHOOTING INFORMATION ********************** <br>";
var_dump($result); // THIS IS JUST FOR DIAGNOSTIC INFO //
echo "<br> ***************************************************************************** <br>";
// THIS SHOULD DISPLAY THE REMAINING 2 VIDEOS IN THE TABLE AS ABOVE //
echo "<br><br>" . $result['name'] . "<br>";
echo "<video src='" . $result['location'] . "' controls width='320px' height='200px' >";
}
The code should display 3 small video images with controls to play the each video. The first video displays correctly so the database is connected, the table is being read correctly, and the commands to display the video are all working, but the WHILE loop is not working. I believe something in the video display line is causing the PHP code to quit but I haven't figured it out yet.
One loop should display all of the videos. You shouldn't have to fetch twice.
/ THIS QUERIES ALL COLUMNS FROM THE 'VIDEOS' TABLE //
$query = mysqli_query($mysqli,"SELECT * FROM videos");
while($result = mysqli_fetch_array($query))
{
// THIS SHOULD DISPLAY ALL ROWS //
echo "<br><br>" . $result['name'] . "<br>";
echo "<video src='" . $result['location'] . "' controls width='320px' height='200px' ></video></br>";
}
In addition, you need to close the </video> tag on each row.
I'm looking to create a formatted product list from an SQL database. My aim is to have a store on my website with a series of small boxes containing some shorthand information about each product, that when clicked will open a pop-up containing detailed information. (I have a working Javascript/JQuery code to create the pop-ups.)
Here is the PHP code so far, simply to get the information from the database and display it on a webpage...
(I've been using XAMPP to provide an environment for me to test the code in)
<?php
mysql_connect("localhost", "root", "") or die (mysql_error ());
mysql_select_db("Database1") or die(mysql_error());
$strSQL = "SELECT * FROM Products";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "<br />";
}
mysql_close();
?>
I want the echoed line to be displayed in a divider, with a divider generated for each record in the SQL database (say I have 10 products available, there would be ten dividers, and 10 different boxes on the webpage). The divider's class is "ProductBox".
echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
This was the closest I have come to a solution, which was simply managing to write a code with no syntax errors - alas, nothing actually displays on the webpage.
If I'm going about this entirely the wrong way please tell me - I'm fairly sure I need to use a SQL database to dynamically update stock on a live website, but if I need to implement a different programming language or whatever then just tell me what you think would work and help me with a solution.
You have an extra semicolon in your code
echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
Replace with
echo "<div class=\"ProductBox\">". $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
mysql_fetch_array needs to be used like this (see PHP Doc):
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
}
or you could just use "mysql_fetch_assoc" instead.
HOWEVER, if you're new to PHP, I HIGHLY RECOMMEND that you get started on the right foot. mysql_query functions are soon to be deprecated. DON'T USE THEM. Most recommend using "PDO" for querying your database. Here's a great tutorial to teach you: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
Also, as mentioned, you have an extra semi-colon.
Dont forget these basics markups :
`<HTML>
<HEAD>
</HEAD>
<BODY> put in here your divs
</BODY>
</HTML>`
This is the first time I have ever posted a question. I am a newbie, and I hope I get all the formatting correct in this message. I love stack overflow. It has helped me get through many challenging situations. But this one has me stymied.
I would like to replace a MySQL database result with an image. I have a plant database that I'm querying to show lists of plants. My results table consists of the plant type, the botanical name, the common name, and whether it is a native plant.
The result for the native plant comes in from the database as the letter 'y' (for yes). I would like to replace that 'y' with an image of an oak leaf. The code I'm currently using displays the oak leaf on all plants, not just the ones that are native plants.
What to do? I feel like I need to insert an if statement in the while loop, but it never works when I try it. I've tried tons of different solutions for a few days now, and would like to ask for help with this.
Here's the latest code I've got that displays the image for every plant, not just the natives:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$native_image = $row['native'];
$image = $native_image;
$image = "images/oak_icon.png";
echo '<tr><td>' .
$row['plant_type'] .
'</td><td>' .
$row['botanical_name'] .
$row['common_name'] .
'</td><td>' .
$image .
'</td></table>';
}
I hope I understood your question.
Some of rows in MySQL have $row['native'], that are equals 'y'. If it is so, then you are on the right way - you have to put an if statement in your loop.
Try it
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
if ($row['native'] == "y") {
//we show our image
$image = "images/oak_icon.png";
} else {
//we don't have to show our image
$image = "";
}
echo '<tr><td>' .
$row['plant_type'] .
'</td><td>' .
$row['botanical_name'] .
$row['common_name'] .
'</td><td>' .
$image .
'</td></table>';
}
And where is your IMG tag? =)
For the future: In my Projects, if I want to save the information about image, that belongs to the row in MySQL, I just save the name of the picture, where it has to be, and if not, i save the name of empty 1x1 PNG image (usual "blank.png"). If you put blank.png in an IMG tag, that has fixed height and width, so IMG takes just the place on your page.
I hope I helped you, and this is my first Answer! =)
P.s. sorry for my english...
Explosion Pills is correct. You will need to check if the $row['native'] is 'y' then set the image html to whatever is required. Hope this helps.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$image='';
if($row['native']=='y'){
$image = '<img src="images/oak_icon.png" />';
}
echo '<tr><td>' .
$row['plant_type'] .
'</td><td>' .
$row['botanical_name'] .
$row['common_name'] .
'</td><td>' .
$image .
'</td></table>';
}
This is my page from an online radio station site of mine on localhost, it's a basic PHP/MySQL one for test purposes:
<?php
mysql_connect('localhost', 'root', 'mypass') or die (mysql_error());
mysql_select_db('radiotest') or die (mysql_error());
$result = mysql_query("SELECT *, TIME_FORMAT(airtime, '%H:%i') `airtime`
from presenters");
//Table starting tag and header cells
while($row = mysql_fetch_array($result)){
//Display the results in different cells
echo "<dd><dl><img src=' " . $row['image'] . " '>" . $row['airtime'] ."
" . $row['presenter'] . "</dd></dl>";
echo "<dd><dl>" . $row['showinfo'] . "</dd></dl>";
}
?>
It works properly, displays the data from the table in the required format.
However, I want to try doing it this way:
<dd><dl><img src='<?php echo $row['image'] ?'> <?php echo $row['airtime']?>
<?php echo. $row['presenter']?> </dd></dl>
My problem: I admit I've forgotten how to do echo without displaying it in the PHP/MySQL query like above, so how can I ensure it displays the variables using echo without having to declare it in the MySQL connection? I know my original is correctly formatted, but I don't want it to have the echo variables after the while part of the syntax, I wanted to echo them within the dd / dl HTML (definition list).
Basically, I'm just trying to brush up my skills in this area; had a look on Google but am not quite sure
Any help is appreciated!
It's really no different, and definitely not better, but I think you are asking to do:
while($row = mysql_fetch_array($result)){
//Display the results in different cells
?>
<dd><dl>
<img src='<?php echo $row['image']; ?>'>
<?php echo $row['airtime']; ?> <?php echo $row['presenter']; ?>
</dd></dl>
<dd><dl>'<?php echo $row['showinfo']; ?></dd></dl>
<?php
}
Try this instead:
<dd>
<dl>
<img src='<?=$row['image'] ?>'> <?=$row['airtime'] . " - " .$row['presenter']?>
</dd>
</dl>