I am currently looking to display an image on my website using PHP. The image data is in MySQL, and I know how to display data from MySQL, but I can't figure out how to display images using insertname.png type data.
My code is currently this: (In pet_url, it basically states dogpuppyrare.png, which is a png inside my public_html inside my file manager on my hosting website.
<?php
$servername = "localhost";
$username = "dbuser";
$password = "dbpass";
$dbname = "db name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT pet_url FROM user_pets";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["pet_url"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The fact the field is labelled ['pet_url'], I'm making the assumption that the data in this key is a URL, so just display the src using the HTML img tag.
if ($result->num_rows > 0) {
//output data of each row
while ($row = $result->fetch_assoc()) {
echo '<img src="' . $row["pet_url"] . '" alt="my pet image"/>"<br>"';
}
}
You can read more about image tags at w3schools:
Convert this line:
echo $row["pet_url"]. "<br>";
To this;
echo '<img src="'. $row["pet_url"] . '" alt="my pet image" /><br>';
Related
Can anyone help please?
This is my code.I want to add an alt option for images but everything I try is throwing errors.
I have tried studying the php handbook and have tried copying code from other questions but so far no luck.
<?php
$servername = "localhost";
$username = "logosewe_5";
$password = "password";
$dbname = "logosewe_5";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM mk2";
$sql = "SELECT * FROM mk2 WHERE brand='2786'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<img src='$row['ming']'>';
}
} else {
echo "0 results";
}
$conn->close();
?>
You have a string concatenation problem :
echo '<img src="' . $row['ming'] . '">';
Will output : <img src="myimage.jpg">.
To add a alt attribute, you could do :
echo '<img src="' . $row['ming'] . '" alt="' . $row['name'] . '">';
Will output something like
<img src="myimage.jpg" alt="image name">
change your code like this:
<?php
$servername = "localhost";
$username = "logosewe_5";
$password = "password";
$dbname = "logosewe_5";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM mk2"
;
$sql = "SELECT * FROM mk2 WHERE brand='2786'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<img src="'.$row['ming'].'" alt="'.$altText.'">'; //change here
}
} else {
echo "0 results";
}
$conn->close();
?>
The problem was that you did not have an alt attribute in your echo statement and the second was your concatenation was a little off with misplaced quotes.
while($row = $result->fetch_assoc()) {
echo '<img src=' . $row['ming'] . 'alt="Your alt message">';
}
I am trying to output the results of an SQL query as a table on a page on my website. I have found a few solutions online but I can't get any of them to work properly. Right now I copied and pasted a bit of code to just output the first two columns but I can't figure out how to get every column in a table. I am new to PHP and web development in general so any help would be appreciated.
My PHP:
<?php
SESSION_START() ;
$servername = "localhost";
$username = "MY USERNAME";
$password = "MY PASSSWORD";
$dbname = "MY DATABASE NAME";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//$_session['userid'] = $userlogged;
$sql = "SELECT * FROM `climbs` WHERE `userlogged` = '" . $_SESSION['userid'] . "'";
$result = mysqli_query($conn,$sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["climb-id"]. "</td><td>" . $row["climbname"]. " " . $row["cragname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($conn);
?>
check with var_dump :
some like that:
$result = mysqli_query($conn,$sql);
var_dump($result);
if ($result->num_rows > 0) {
maybe the query it's wrong.
I have spent the better part of three days on this problem. I've tried several solutions that I've found on this site but with little success. What I'm trying to do is use a PHP variable to play a youtube video in an iframe. I'm also trying to run two MySQL queries with the same input. Here is my code as it sits right now. At this moment when it runs I get the table that I'm wanting, though I still need to format it. But the iframe isn't even showing up. A previous solution I tried would pull up the iframe but inside would be an error where I was basically passing the sql query to the iframe.
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "purpletrainer";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
$trainingid = $_POST["trainingid"];
$sql = "SELECT * FROM purpletrainer.trainingcontent WHERE trainingid = '$trainingid';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Training ID</th><th>Title</th><th>Training URL</th><th>Training Quiz URL</th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["trainingid"] . "</td><td>" . $row["trainingtitle"] . "</td><td>" . $row["trainingurl"] . "</td><td>" . $row["trainingquizurl"] . "</td></tr>";
}
echo "</table>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "purpletrainer";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$videosql = "SELECT trainingurl FROM purpletrainer.trainingcontent WHERE trainingid = '$trainingid';";
$videoresult = $conn->query($videosql);
if ($videoresult->num_rows > 0) {
while ($videourl = $videoresult->fetch_assoc()) {
}
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<div class="col-md-6">
<iframe width="420" height="315" src= '<?php echo htmlspecialchars($videourl); ?>' frameborder="0" allowfullscreen></iframe>
</div>
It's common practice not to use the variable $videourl outside of the while loop. (Put your iframe within the while loop).
sidenote: fetch_assoc() will create an array. It should be $videourl['trainingurl'];
Here's an example:
<?php
if ($videoresult->num_rows > 0)
{
while($videourl = $videoresult->fetch_assoc()){
?>
<div class="col-md-6">
<iframe width="420" height="315" src= '<?= $videourl['trainingurl']; ?>' frameborder="0" allowfullscreen></iframe>
</div>
<?php
}
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
edit for clarity and future readers
Note that the <?= $variable ?> opening tag has only been properly supported since php 5.4+. When not using this version, maintain the usage of <?php echo $variable; ?>
Is it possible to retrieve data from a certain column and have a if else statement to it where i can change the image?
So basically i want to the pic to tally with the location that i retrieve with the database.
<?php
$servername = "......byethost5.com";
$username = "....";
$password = "...";
$dbname = "b...";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Location,DateTime FROM SensorDetails LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Last Seen Location: " . $row["Location"]."<br>";
$Help= $row["Location"];
if($Help=='Toilet')
{echo '<center><img src="Toilet.jpg"></center>';}
elseif($Help=='Kitchen')
{echo '<center><img src="Kitchen.jpg</center>';}
else {echo '<center><img src="BedRoom.jpg"></center>';}
}
} else {
echo "0 results";
}
$conn->close();
?>
Another solution is use the same name for 'Location' field and its current image.
<?php
$servername = "......byethost5.com";
$username = "....";
$password = "...";
$dbname = "b...";
//////////////////////////////////////////////////////
//Image folder
//put here the folder where you save images
$img_folder = './'; //current folder
/////////////////////////////////////////////////////
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Location,DateTime FROM SensorDetails LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$img_file = $img_folder . $row["Location"] . '.jpg';
//echo $img_file; //uncomment this if you wanna check img's path.
if( ! file_exists($img_file) ){//Check if the file exists
$img_file = '';
}
echo "Last Seen Location: " . $row["Location"]."<br>";
echo "<center><img src="$img_file"></center>";
}
}else {
echo "<p> 0 results </p>";
}
$conn->close();
?>
This solution is more appropriate if you're going to have more locations in the future. I supposed that you keep the images in the same folder as this script and all the images are in JPG format.
One solution is to store the Image URL (or last part) in the database as a separated column, and echo the image same you echo the field location.
Sure you can display different images for different locations .
I have considered your code and modified it a little bit .
if( $result->num_rows > 0 )
{
while( $row = $result->fetch_assoc( ) )
{
echo 'Last Seen Location: ' .$row[ 'Location' ] .'<br>';
$location = trim( $row[ 'Location' ] ); // trim to make sure no spaces on either side
switch( strtolower( $location ) )
{
case 'toilet' : $image = 'Toilet.jpg'; break;
case 'kitchen' : $image = 'Kitchen.jpg'; break;
case 'bedroom' : $image = 'BedRoom.jpg'; break;
default : $image = 'BedRoom.jpg'; // Change default image as needed
}
echo '<center><img src="' .$image .'"></center>';
}
}
else
{
echo "0 results";
}
Notes :
I have used switch instead of if .
I have used strtolower on $location before comparing .
I have renamed $Help to $location for readability .
Iam class 11th..recently I started learning php and mysqliI have been facing a problem. I am trying to create a database which has a small list of movies.when the page loads, it displays the list of those movies from database..but the problem is, it displays them as a simple text..i Want them to be like links so that whenever it is clicked it displays the info about that particular movie..but i dont want to write anchor tag links for each movie..Is there any other way?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, title FROM movies";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["title"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
According to your code.
if ($result->num_rows > 0) {
$htmlLink = '';
while($row = $result->fetch_assoc()) {
$id = $row["id"];
$title = $row["title"];
$htmlLink .= "<a href='movie.php?{$id}'>id: {$id} - Name: {$title}</a><br>";
echo $htmlLink;
}
} else {
echo "0 results";
}
Then on your movie.php page use $_GET[] to get the query string data
Replace:
echo "id: " . $row["id"]. " - Name: " . $row["title"]. "<br>";
With:
$id = $row["id"];
$title = $row["title"];
echo "<a href='/movie/{$id}'>id: {$id} - Name: {$title}</a><br>";