Show picture from database don´t work - php

I wnt to show some pictures i have in a mysqli database, they are stored as blob.
But all i get is a A4 page with wierd text..
this is my code that i use too show(i connect to my database above this code):
echo "<table border=1>\n";
/*echo " <tr>\n";
echo " <th>Bild</th><th>Namn</th><th>Betygsättning</th>\n";
echo " </tr>\n";*/
while ($rad = mysqli_fetch_array($sql_result)) {
echo "<tr>";
echo "<td>";?> <img src="<?php echo $rad["Bild"]; ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $rad["Namn"]; echo "</td>";
echo "</tr>";
}
echo "</table>\n";

If you really want to embed the image to the generated html you should write it as this here:
echo "<td>";?> <img src="data:image/jpeg;base64,<?php echo base64_encode($rad["Bild"]); ?>" height="100" width="100"> <?php echo "</td>";
Here again with a little more clean code:
echo "<table border=\"1\">\n";
while($rad = mysqli_fetch_array($sql_result)) {
echo " <tr>\n";
echo " <td>\n";
echo " <img src=\"data:image/jpeg;base64," . base64_encode($rad["Bild"]) . "\" height=\"100\" width=\"100\" />\n";
echo " </td>\n";
echo " <td>" . $rad["Namn"] . "</td>";
echo " </tr>\n";
}
echo "</table>\n";
It seem that you forgot also to close your img tag.

echo '<table border=1>';
/*echo " <tr>\n";
echo " <th>Bild</th><th>Namn</th><th>Betygsättning</th>\n";
echo " </tr>\n";*/
while ($rad = mysqli_fetch_array($sql_result)) {
echo '<tr>
<td><img src="'.$rad["Bild"].'" height="100" width="100"></td>
<td>'.$rad["Namn"].'</td>
</tr>';
}
echo '</table>';
That should do it. While you are in a block of php code you can concatonate on it like above.

Related

How to put text below photo

I want add text just below the picture but I don't know how to do it.
<?php
include "includes/connexio_web.php";
obrirConnexioBD();
$sql = "SELECT * FROM Llistat_vies";
$sth = $conn->query($sql);
while($row=mysqli_fetch_array($sth)) {
echo "<tr>";
echo "<td>"; ?>
<img src="<?php echo $row["photo"]; ?>" height="200" width="200"><?php echo "</td>";
echo "<td>"; echo $row["text"]; echo "</td>";
echo "</tr>";
}
?>
Here your code updated.
<?php
include "includes/connexio_web.php";
obrirConnexioBD();
$sql = "SELECT * FROM Llistat_vies";
$sth = $conn->query($sql);
$table= "<table>";
while($row=mysqli_fetch_array($sth)){
$table= "<tr>
<td align='center'>
<img src=".$row["photo"] ." height='200' width='200'> <br>
$row["text"]
</td>
</tr>";
}
print $table . "</table>";
?>
try these new HTML5 tags:
<figure> and <figcaption>
Put your image and text inside these tags like this:
echo "<tr>";
echo "<td>"; ?>
<figure>
<img src="<?php echo $row["photo"]; ?>" height="200" width="200">
<figcaption>
<?php echo $row["text"];
echo "</figcaption>";
echo "</figure>";
echo "</td>";
echo "</tr>";
I am pretending that your <table> and <tr> are outside of the loop.
<table>
<tr>
while($row=mysqli_fetch_array($sth)) {
echo "<td>";
echo "<table>";
echo "<tr>";
echo "<td>";
?>
<img src="<?php echo $row["photo"]; ?>" height="200" width="200">
<?php
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo $row["text"];
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
}
</tr>
</table>
I put tables within the table an inside of a <td> so that they would be next to each other.
I don't know if this is the best way but it was similar to what you were doing and it worked for me. I hope this helps.

get related data from db via anchor tag

My code:
<?php
if (isset($_GET["q"]))
{
$q= $_GET["q"]; # this is team name
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"onesports");
$result=mysqli_query($link," select * from team where team_name='" .$q. " ' ");
$result2=mysqli_query($link,"select players.full_name, team.* from players,team where team.team_name='" .$q. " ' and players.team_name=team.team_name ");
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td style="font-size: 30px; font-variant-caps:petite-caps;" > <?php echo $row['team_name']; ?> </td>
<td style="text-transform: capitalize; "> <?php echo "("; echo $row['city'] ; echo ","; echo $row['prov']; echo ")"; ?> </td>
</tr>
<tr>
<td style="text-transform: capitalize;">
<?php echo $row['captain']; echo "(c)"; ?>
</td>
<td style="text-transform: capitalize;"> <?php echo $row['coach']; ?> </td>
</tr>
</table>
<hr>
<?php
}
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo "<tr>";
echo "<th>";
echo "Players";
echo "</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td>";
echo '<a href= " " >' . $row['full_name'] . '</a>';
echo "</td>";
echo "</tr>";
}
echo "</table>";
The problem is that I'm getting full_name dynamically, every name has some details in db I want to get.
When I click on full_name, I want it to give me all the related data from the DB.
This is the part with the links:
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo "<tr>";
echo "<th>";
echo "Players";
echo "</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td>";
echo '<a href= " " >' . $row['full_name'] . '</a>';
echo "</td>";
echo "</tr>";
}
echo "</table>";
So kindly help me. Do you suggest that I use any other tag rather than an anchor tag, or you can tell me how to get data this way?
As I got the point, you need to retrieve the information by clicking the relevant link.
You can use the following structure:
echo ''.$row["full_name"].'';
Then at your server-side code, get the data by using
$_GET['FN']
I hope this answer help you as well as.
browse image
db image
here is both pic browser and db onesports is my db name and players is table name,
in browser image you can see players name i want when i click on players name it show related data on same page
Here is the php code, please review
As I mentioned, because of setting "player_id" as your primary key, you have to fetch the information by using it.
echo ''.$row["full_name"].'';
Your Query should be in the following format:
Select * from `players` Where `player_id` = $_GET['player_id']

How to display two distinct MySQL tables on same PHP page

I have two sql tables I'm reading from and I would like to display each table as its own, separate table. First the internal, and then the external. And I'd like to have a simple title prefacing each one. For instance:
INTERNAL
Table here
EXTERNAL
Table here
What's happening though is the INTERNAL and EXTERNAL titles keep appearing
on the same line, or both come before the tables. Like this:
INTERNAL
EXTERNAL
Internal Table here
External Table here
I've tried including the titles as part of the php tags. Those are the currently commented out lines. And I've also tried adding the html outside of the php tags like this:
<br><br>
<b>INTERNAL</b>
<br><br>
<?php Internal Table here ?>
And then:
<br><br>
<b>EXTERNAL</b>
<br><br>
<?php External Table here ?>
But that still results in both titles appearing first, before the tables. Does the html get processed prior to the php? I must be interpreting this too linearly, like a script because it's certainly not being processed in the order in which it is coded on my page.
This is my code as is, which has the titles commented out. The tables appear sandwiched together which tells me they are not being interpreted as two distinct elements on the page. Do I need to put each one in it's own tag?
<?php
$internal = getInternalNetworkTable();
if ($internal->num_rows > 0){
//echo " <b>INTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $internal->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
} else {
echo "No results founds";
}
?>
<?php
$external = getExternalNetworkTable();
if ($external->num_rows > 0){
//echo " <b>EXTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $external->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
} else {
echo "No results founds";
}
$conn->close();
?>
EDIT:
Needed to add:
echo "</table>";
New code:
<?php
$internal = getNovatoInternalNetworkTable();
if ($internal->num_rows > 0){
echo " <b>INTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $internal->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
echo "</table>";
} else {
echo "No results founds";
}
?>
<?php
$external = getNovatoExternalNetworkTable();
if ($external->num_rows > 0){
echo " <b>EXTERNAL</b>";
echo "<table cellspacing=\"0\">";
echo "<tr>";
echo " <th><b>Device</b></th>";
echo " <th><b>Label</b></th>";
echo " <th><b>Address</b></th>";
echo " <th><b>Type</b></th>";
echo " <th><b>DisplayNumber</b></th>";
echo " <th><b>Wiki</b></th>";
echo "</tr>";
while ($row = $external->fetch_assoc()){
echo "<tr>";
echo ("<td>" . $row["Device"] ."</td>");
echo ("<td>" . $row["Label"]."</td>");
echo ("<td>" . $row["Address"]."</td>");
echo ("<td>" . $row["Type"]."</td>");
echo ("<td>" . $row["DisplayNumber"]."</td>");
echo ("<td>" . $row["Wiki"]."</td>");
echo "</tr>";
}
echo "</table>";
} else {
echo "No results founds";
}
$conn->close();
?>
You're missing </table> in both display of tables.
Therefore your table will be merge in each other.
echo '</table>';
Before both of your } else { should do the trick

image does not fetch from database php pdo [duplicate]

I've posted my code below. I can able to fetch the text values from database. But, i'm unable to fetch the image. i don't know where i made mistake?
try
{
$stmt = $conn->prepare("SELECT * FROM ebusers");
$conn->errorInfo();
$stmt->execute();
while($userrow = $stmt->fetch())
{
echo "<table class=glowing width=800 border=0 align=center cellpadding=0 cellspacing=0>";
echo "<tr>";
echo "<td height=40 colspan=3 class=user_id >User ID : " . $userrow['UserID'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td bgcolor=wheat colspan=3> </td>";
echo "</tr>";
echo "<tr>";
echo '<td width=334 rowspan=9 bgcolor=wheat align=center >';
if( $userrow['UserProfilePicture'] >= '[BLOB - 0B]' )
{
echo '<img class=shadow_img width=160 height=180 src="data:image/png;base64,' . base64_encode($userrow['UserProfilePicture']) . '"/>';
}
else
{
echo '<img src=\'images/nophoto.gif\' class=shadow_img width=160 height=180 />';
}
echo "<tr>";
echo "<td bgcolor=wheat style=\"border-bottom-right-radius:5px; border-bottom-left-radius:5px;\" colspan=3> </td>";
echo "</tr>";
echo "</table>";
echo "<br>";
echo "<br>";
}
}
catch(PDOException $e)
{
'Error : ' .$e->getMessage();
}

PDO cannot able to fetch image from database

I've posted my code below. I can able to fetch the text values from database. But, i'm unable to fetch the image. i don't know where i made mistake?
try
{
$stmt = $conn->prepare("SELECT * FROM ebusers");
$conn->errorInfo();
$stmt->execute();
while($userrow = $stmt->fetch())
{
echo "<table class=glowing width=800 border=0 align=center cellpadding=0 cellspacing=0>";
echo "<tr>";
echo "<td height=40 colspan=3 class=user_id >User ID : " . $userrow['UserID'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td bgcolor=wheat colspan=3> </td>";
echo "</tr>";
echo "<tr>";
echo '<td width=334 rowspan=9 bgcolor=wheat align=center >';
if( $userrow['UserProfilePicture'] >= '[BLOB - 0B]' )
{
echo '<img class=shadow_img width=160 height=180 src="data:image/png;base64,' . base64_encode($userrow['UserProfilePicture']) . '"/>';
}
else
{
echo '<img src=\'images/nophoto.gif\' class=shadow_img width=160 height=180 />';
}
echo "<tr>";
echo "<td bgcolor=wheat style=\"border-bottom-right-radius:5px; border-bottom-left-radius:5px;\" colspan=3> </td>";
echo "</tr>";
echo "</table>";
echo "<br>";
echo "<br>";
}
}
catch(PDOException $e)
{
'Error : ' .$e->getMessage();
}

Categories