I'm trying to insert a image from my database on phpmyadmin and I'm gettin a situation where it just doesnt show my image. The type I'm using on my table is a longblob because my image is bigger. Is there any way I could fix this output so I can just output my image?
NOTE : It seems everything else works EXCEPT for the image part, I just felt like showing the whole php code just in case
HTML / PHP
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
echo "<div class='service-title'>";
echo "<h2>". $row["serviceName"] ."</h2>";
echo "</div>";
echo "<a href=". $row["servicePageName"] .">";
echo '<div class="service-container" style="background-image: data:image/jpg;base64,'.base64_encode($row['serviceImage']).'"/>';
echo "<h1>Click Me!</h1>";
echo "</div>";
echo "</a>";
}
}
$conn-> close();
Figured it out by just adding a simple url() into the background-image
Related
I'm new to php and mysql. I created databse in mysql, and I would also like to display my image from the database to my php site, but I only get a weird code (something like this: ����JFIF,,��), everything else works as it should. This is my code:
<?php
$mysqli = new mysqli('localhost','root','','bas');
$result = $mysqli->query("SELECT * FROM bas1");
if($result->num_rows !=0)
{ while($rows = $result->fetch_assoc()
{$name=$rows['name'];
$price=$rows['price'];
$rate=$rows['rate'];
$image=$rows['image'];
echo "<tr>";
echo "<td>$name</td><td>$price</td><td>$rate</td><td>$image</td>";
echo "</tr>";}
} else {
echo "<tr>";
echo "<td>";
echo "no rusults";
echo "<td>";
echo "</tr>";}
?>
The image in databse is set to longblob. I would be very thankful if someone could help me
try this
use img tag and encode the image
echo '<img src="data:image/jpeg;base64,\'.base64_encode( $image ).\'"/>';
I recommend you to not to store an image in the database you should store image somewhere in server directory and save the path of that image in the database.
and display image like this
<img src="<?php echo $image?>">
and your image data type in your database should be text.
I was wondering how I could add a scroll to my table that is written in the php file. I do not want to write it in a style.css file, I want it directly in the php file. below is my code, but I am not able to make it to work. The table gets content from mySql database, which works. but the problem is that I get to much of content so it fills out the whole page. That is why I want to make it scrollable :
if(mysqli_num_rows($result) > 0){
echo '<table border="1">';
echo "<tr>";
echo "<th>Name</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
If im getting your question right you need to set a width and height then set the overflow-y. This will give you a table set to a certain size with a scroll bar. Note you need to set your own width and height.
echo "<table style=\"width:500px; height:500px; overflow-y:auto\">";
I'm trying to add background image property do div. I know that I can display normal image from database like this:
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'" />';
but what about adding background to div? I tried, I really did but just my code doesn't work so i ask how to do that?
This is my code:
<?php
require_once('config.inc.php');
$sql = "SELECT id, title, author, image FROM articles ORDER BY count DESC LIMIT 3";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<a href="'.$row["id"].'">';
echo '<div class="module" style="background:'.base64_encode($row["image"]).'">';
echo '<h3 class="article-title">'.$row["title"].'<br />'.$row["author"].'</h3>';
echo '</div>';
echo '</a>';
}
} else echo "Wystąpił błąd z bazą danych";
$mysqli->close();
?>
"DIV" tag does'nt support an a base64 format. If you want to show an image in, you have to use the background-image property of CSS.
Example:
<div style="background-image:url(<?=$YourBase64?>);padding:50px;background-size:cover;background-position:center center; background-repeat:no-repeat;"></div>
You can see more about it in: https://www.w3schools.com/cssref/pr_background-image.asp
You are not telling your CSS to use a base64 encoded image, also remember in CSS you need the url() function:
echo '<div class="module" style="background-image:url(data:image/jpeg;base64,' . base64_encode($row['image']) . ')">';
JSFiddle here, with unfunctionaing PHP obviously, but on the local host it pulls through the information just fine.
I am trying to append data in a loop into a specific set of tags, inside a di of a certain class.
For example, for every row in the the table, add a div with the class "card" and append "Title" into a H2, "description" into a P tag etc.
Anyone know how I could go about doing this? I'm googling but finding it hard to phrase my question right.
Div "Card" markup:
<div class="card">
<h2>Health and Wellbeing</h2>
<p>Sometimes you just did too much sitting not enough jumping go go go.</p>
</div>
PHP pull:
$sql = "SELECT title, description, count FROM relevant_topics";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<b>Title: </b>" . $row["title"]. " - Description: " . $row["description"]. " " . $row["count"]. "<br>";
}
} else {
echo "0 results";
}
You can echo the div markup into the PHP code like this:
while($row = $result->fetch_assoc()) {
echo'<div class="card">
<h2>'.$row["title"].'</h2>
<p>'.$row["description"].'
</div>';
}
Not sure where you want the Row['count'] though.
Also you might wanna use PDO instead of mysql, Since mysql is outdated: How can I prevent SQL injection in PHP?
check this
if ($result->num_rows > 0) {
// output data of each row
while($row = $result-> mysql_fetch_array()) {//if not works change mysql_fetch_array() to fetch_assoc()
echo'<div class="card"><h2>';
echo '.$row["title"].';
echo '</h2><p>';
echo '.$row["description"].';
echo '</P></div>';
}
So I have looked through many of the other questions posted but none seemed to properly answer my question.
I currently have a php script that reads rows from a database and runs a loop to display the info of each row in a html div.
The issue I am now having is that in each "card" of info previously loaded there has a picture. And when the picture is clicked I need to load a different file names listing.html. The problem is that depending on which picture is clicked on I want to load different information based on which listing was clicked on. I looked into using sessions but since i dynamically load all of the pictures in one loop I do not know a way to differentiate between them.
here is the code I currently have that loads all the info from the database.
$query = "SELECT * FROM Listings ORDER BY OrderNumber";
if ($result = mysqli_query($con, $query))
{
while ($row = mysqli_fetch_row($result))
{
echo "<div class ='listing'>";
echo "<a href='listing.html'>";
echo "<img src=$row[2] alt='' width='60%' align='left' >";
echo "</a>";
echo "<span style = 'font-size: 25px;'>";
echo $row[4];
echo "</span>";
echo "<br>";
echo "<br>";
echo "MLS Number: #";
echo $row[1];
echo "<br>";
echo "<br>";
echo "<b>Open House Details: </b>";
echo $row[6];
echo "<br>";
echo "<br>";
echo $row[5];
echo "<br>";
echo "<br>";
echo "<a href='";
echo $row[9];
echo "' style='color: #8a0b0b; font-weight: bold; font-style: italic; font-size: 18px;' target='_blank'>Virtual Tour</a>";
echo "<div class='listingPrice'>";
echo "$ ";
echo $row[7];
echo "</div>";
echo "</div>";
}
mysqli_free_result($result);
}
I need some way that when the a tag is clicked to first be able to discern which picture was clicked then probably make a session variable with the corresponding row.
When you output the link, include a query string value from the row in the data. Something like this:
echo "<a href='listing.html?id=" . row[0] . "'>";
(I'm just guessing on the row[0] part, it would be any identifier which uniquely identifies that record.) That way all of the links would have the data you need on the listing.html page embedded directly in them. (Shouldn't it be listing.php?)
So on the page being referenced, the value would then be available in the query string. Something like this:
$_GET["id"]
There really isn't a need for session state in this case, if all you need to know is which record was clicked then that information can be placed directly in the link. This reduces coupling in the code, keeps the links a little more RESTful, even lets people bookmark the link directly if they want to.