How to display images from MySQL database using PHP - php

I want to display images from my MySQL database to an website that I'm working on but when I visit the site I see just a box with a broken image or lot of symbols.
In my MySQL database the image type is LONGBLOB (MIME: image/jpeg).
This is the code that I'm using (I have HTML codes before the PHP codes so I'm using a buffer).
<?php
mysql_connect("localhost", "root", "") or die('connection error');
mysql_select_db("produse");
$per_page = 25;
$pages_query = mysql_query("SELECT COUNT('id') FROM filtrederetea");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($GET['page'])) ? (int)$GET['page'] : 1;
$start = ($page -1) *$per_page;
$query = mysql_query("SELECT * FROM filtrederobinet");
echo "<table>";
if(isset($_GET["id"]))
{
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * FROM filtrederetea");
while ($query_row = mysql_fetch_assoc($query)) {
$imagedata = $query_row['img'];
'<img src="data:image/jpeg;base64,' . base64_encode($query_row['img']);
};
header('Content-Type: image/jpeg');
echo "($imagedata)";
}
else
echo "error";
while ($query_row = mysql_fetch_assoc($query)) {
echo "<tr>";
echo "<td>";
echo.$query_row["img"];
echo "</td>";
echo "<td>";
?>
<img src=<?php echo $query_row["img"]; ?> width="130" height="130">
<?php
echo "</td>";
echo "</tr>";
}
echo "</table>";
if ($pages >=1) {
for ($x=1;$x<=$pages;$x++) {
echo ''.$x.' ';
}
}
ob_end_flush()
?>

Related

Sort doesn't work with pagination

Sorting is work perfectly before I combine it with pagination. But right now I am facing with the problem this when to pass value of sorting to pagination:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?page=1 ASC LIMIT 0, 10' at line 3
Below is my code:
<?php
session_start();
$host = 'localhost';
$user = 'root';
$pass ='password';
$name = 'dbname';
$con = new mysqli ($host,$user,$pass,$name);
?>
<html>
<h2>Your Redeemed Ticket (<?php echo $_SESSION['username'];?>):</h2>
<table border='1' align='center' cellpadding='10'>
<tr align = "center">
<td>
Purchase ID
<td>
Ticket Type
<td>
Quantity
<td>
Date
</tr>
</html>
<?php
// default sorting
if(isset($_GET['sort'])){
$sortOrder = $_GET['sort'] ? :0;
/*Pagination*/
$limit = 10;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $limit;
$username = $_SESSION['username'];
$data =
"
SELECT *
FROM redeem
WHERE redeem_by = '$username' ORDER BY ".$sortOrder." ASC LIMIT $start_from, $limit
";
$result = $con->query($data);
if ($result === false)
die (mysqli_error($con));
while($rows = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'>";
echo $rows['purchase_id'];
echo "<td align='center'>";
echo $rows['ticket_type'];
echo "<td align='center'>";
echo $rows['qty'];
echo "<td align='center'>";
echo $rows['date'];
echo "</tr>";
}
echo "</table>";
/*PAGE NUMBER DISPLAY*/
$data2 = "SELECT COUNT(redeem_by) FROM redeem";
$rs_result = mysqli_query($con,$data2);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / $limit);
$pagLink = '';
for ($i=1; $i<=$total_pages; $i++)
{
$pagLink .="<a href='user_history.php?sort=".$sortOrder."?page=$i'>" .$i."/</a>";
};
echo "<center>".$pagLink."</center>";
/*LOGOUT*/
if(isset($_POST['logout']))
{
session_destroy();
header ("location:login.php");
exit();
}
}
?>
There are a few weird issues in your code, but the problem with your screwed up GET variable can probably be traced to here:
<a href='user_history.php?sort=".$sortOrder."?page=$i'>"
Obviously when you have url parameters, you delineate the starting point with the '?' but after that you need to seperate additional parameters with the '&'.
So it should be:
<a href='user_history.php?sort=".$sortOrder."&page=$i'>"

unable to produce result correctly in div php

I'm not very sure how can I put them into words. I'm trying to display result in each div as shown in the image but unfortunately I'm only able to make it appear only in the "request for quote" div.
May I know where have I gone wrong?
<?php
$query2 = "SELECT * FROM client c, sales_card s WHERE c.id = s.client_id and emp_id_followup = '".$_SESSION["ID"]."'";
$result2 = mysql_query($query2);
if (mysql_num_rows($result2) > 0) {
while($row2 = mysql_fetch_assoc($result2)) {
$swimlaneID = $row2['swimlane_id'];
}
}
$query = "SELECT * FROM swimlane";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
echo '<div id="right">';
echo '<div style="border-style:solid; height:1020px;">';
echo '<h2>'. $row["swimlane_name"].'</h2>';
echo '<ul id="'. $row["shortform"].'">';
if ($swimlaneID == $row["id"])
{
echo $display-> $row["shortform"]();
}
echo '</ul>';
echo '</div>';
echo '</div>';
}
}else{
echo "no row";
}
?>

How can I arrange this code so I can get different entry from database for each table row

I want every different record of database to be display on each table rows, but I'm unable to retrieve different record for each row and column. Please give me suggestion where I can paste than while block so it will give different result for each row and column.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$rec_limit = 10;
$scriptname=$_SERVER['PHP_SELF'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('online_shopping'); // include your code to connect to DB.
$tbl_name="mobile_db"; //your table name
$start = 0;
$limit = 5;
$sql = "SELECT id,company,model,price,availability,image FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$company=$row['company'];
$model=$row['model'];
$available=$row['availability'];
$price=$row['price'];
}
echo "<table border='2'>";
$j = 0;
for($j = 0; $j<5; $j++)
{
echo "<tr>";
for($i = 0; $i<3; $i++)
{
echo "<td>";
echo "<table border='2'>";
echo "<tr>";
echo "<td><img src='abc.jpg' height='250' width='250'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<table border='2'>";
echo "<tr>";
echo "<td><b>Brand : </b></td>";
echo '<td>'.$company.'</td>';
echo "</tr>";
echo "<tr>";
echo "<td><b>Model : </b></td>";
echo '<td>'.$model.'</td>';
echo "</tr>";
echo "<tr>";
echo "<td><b>Availability : </b></td>";
echo '<td>'.$available.'</td>';
echo "</tr>";
echo "<tr>";
echo "<td><b>Price : </b></td>";
echo '<td>'.$price.'</td>';
echo "</tr>";
echo "</table>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
You need to move the table rows inside the fetch loop or store the row in an array. I have simplified your tables to make the example clearer:
$result = mysql_query($sql);
if (!$result) {
/* Error */
}
echo '<table>';
while ($row = mysql_fetch_array($result)) {
echo '<tr><td><img src="', htmlspecialchars ($row['image']), '">';
echo '<tr><td><table>';
echo ' <tr><th>Brand<td>', htmlspecialchars ($row['company']);
echo ' <tr><th>Model<td>', htmlspecialchars ($row['model']);
echo ' <tr><th>Availability<td>', htmlspecialchars ($row['availability']);
echo ' <tr><th>Price<td>', htmlspecialchars ($row['price']);
echo ' </table>';
}
echo "</table>\n";
Some notes about the code:
Test the return value of mysql_query(). The query might fail.
Escape your output using htmlspecialchars().
You should use <th> elements for your headings and style those, instead of using inline <b> elements.
I added output of $row['image'] which might not do what you want.
And do not use the deprecated mysql extension. Use PDO or mysqli instead.
In your while loop you always rewrite the same variables, after loop you have only last record saved.
In the loop, you have to save records into array.
In your code you have nested tables, but in the first one, there is only one row and one table cell which contains another table. I use just nested table.
<?php
...
$result = mysql_query($sql);
$data = array();
while($row = mysql_fetch_array($result)) {
$data[] = $row;
}
if (count($data) > 0) {
echo '<table>';
foreach ($data as $row) {
echo '<tr>';
echo '<td>Brand: ' . $row['company'];
echo '<td>Model: ' . $row['model'];
echo '<td>Availability: ' . $row['availability'];
echo '<td>Price: ' . $row['price'];
}
echo '</table>';
} else {
echo 'no records';
}
?>
Not sure I fully understand what you are trying to accomplish but try this.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$rec_limit = 10;
$scriptname=$_SERVER['PHP_SELF'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('online_shopping'); // include your code to connect to DB.
$tbl_name="mobile_db"; //your table name
$start = 0;
$limit = 5;
$sql = "SELECT id,company,model,price,availability,image FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);
echo "<table border='2'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['company']."</td>";
echo "<td>".$row['model']."</td>";
echo "<td>".$row['availability']."</td>";
echo "<td>".$row['price']."</td>";
echo "</tr>";
}
echo "</table>";
?>

PHP - Pagination and place results is a table

I have tried my hand at some pagination but it gives me very unexpected results....
The first is that it on the first page of the pagination it seems to override the CSS and align all divs to the right hand side of the page... when you then click 'next' to view the next three results and so on the page goes back as per the CSS tells it!
The second problem is that the table displaying the results doesn't seem to work. The very first result on each page of the pagination is in the table, the next two results (three results per page) are displayed on the page however below and not in the table!
The code is quite long but here we go:
<?php
session_start();
if ( !isset($_SESSION['username']))
{
header("Location:index.php");
exit();
}
//connect to database
require "dbconn.php";
$per_page = 3;
$start = $_GET['start'];
$sort = #$_POST['order'];
if (!empty($sort)) {
$query = "SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY ".mysql_real_escape_string($_POST['order'])." ASC";
}
else {
$query = "SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY bookname ASC";
}
$results = mysql_query($query)
or die (mysql_error());
$record_count = mysql_num_rows($results);
if (!$start)
$start = 0;
$get = mysql_query("SELECT * FROM booktable LIMIT $start, $per_page");
?>
<?php
if (isset($_GET['showerror']))
$errorcode = $_GET['showerror'];
else
$errorcode = 0;
?>
Then I will cut out all the unnecesary html
<div id="mid">
<?php
echo "<table border='2px'>";
echo "<tr>";
echo "<th>";
echo "</th>";
echo "<th>";
echo "Book Title";
echo "</th>";
echo "<th>";
echo "Book Author";
echo "</th>";
echo "<th>";
echo "Book Publisher";
echo "</th>";
echo "<th>";
echo "Book ISBN";
echo "</th>";
echo "<th>";
echo "</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($get))
{
// get data
$bookname = $row['bookname'];
$bookauthor = $row['bookauthor'];
$bookpub = $row['bookpub'];
$bookisbn = $row['bookisbn'];
echo "<tr>";
echo "<td>";
echo "<a href='addtolist.php?bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>Add to basket</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "</tr>";
echo "</table>";
}
$prev = $start - $per_page;
$next = $start + $per_page;
if (!($start<=0))
echo "<a href='products.php?start=$prev'>Prev</a> ";
//set variable for first page number
$i=1;
//show page numbers
for ($x = 0; $x < $record_count; $x = $x + $per_page)
{
if ($start != $x)
echo "<a href='products.php?start=$x'>$i</a>";
else
echo "<a href='products.php?start=$x'><b>$i</b></a>";
$i++;
}
//show next button
if (!($start >= $record_count - $per_page))
echo "<a href='products.php?start=$next'>Next</a>";
?>
<?php echo $record_count; ?>
This is how the page looks (image 1) (the table border shows the problem)
As I said before I also get the problem when the list first gets displayed and the page ends up looking like this: (image2)
You can see how they differ!
I hope that I have made sense!
For starters, you have your </table> tag in your while loop. Change that and see if everything else falls into place.

PHP loop position

Can someone help me on this. I'm made an image uploader and i want the image to make another tr if it reach to 5 pics so it will not overflow. Here is my code:
$dbc = mysql_connect("localhost" , "root" , "") or die (mysql_error());
mysql_select_db('blog_data') or die (mysql_error());
$sql = "SELECT * FROM img_uploaded";
$result = mysql_query($sql);
while($rows=mysql_fetch_array($result))
{
if ($rows)
{
echo "<tr><td><img src='user_images/".$rows['img_name'] . "' width='100' height='100'></td></tr>";
}
else
{
echo "<td><img src='user_images/".$rows['img_name'] . "' width='100' height='100'></td>";
}
}
mysql_close();
E.g. by using the modulus operator:
$dbc = mysql_connect("localhost" , "root" , "") or die (mysql_error());
mysql_select_db('blog_data', $dbc) or die (mysql_error($dbc));
$sql = "SELECT * FROM img_uploaded";
$result = mysql_query($sql, $dbc) or die(mysql_error($dbc));
echo '<table><tr><th>image</th>';
for($cnt=0; false!==($row=mysql_fetch_array($result)); $cnt++) {
if ( 0===$cnt%5 ) {
echo '</tr><tr>';
}
echo '<td><img src="user_images/'.$rows['img_name'] . '" width="100" height="100"></td>';
}
echo '</tr></table>';
It uses the modulus operator, but in addition it checks that a has been opened.
$dbc = mysql_connect("localhost" , "root" , "") or die (mysql_error());
mysql_select_db('blog_data') or die (mysql_error());
$sql = "SELECT * FROM img_uploaded";
$result = mysql_query($sql);
$numOfRows = 0;
while($rows = mysql_fetch_array($result))
{
if (($numOfRows % 5) === 0)
{
if ($numOfRows != 0)
{
echo '</tr>';
}
echo '<tr>';
}
$numOfRows++;
if ($rows)
{
echo "<td><img src='user_images/".$rows['img_name'] . "' width='100' height='100'></td>";
}
else
{
echo "<img src='user_images/".$rows['img_name'] . "' width='100' height='100'>";
}
}
mysql_close();
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
$cols = 5;
$chunkSize = ceil(count($areaArray) / $cols);
echo $chunkSize * $cols;
foreach (array_chunk($rows, $chunkSize) as $itemsInThisTr) : ?>
<tr>
<?php foreach ($itemsInThisTr as $item) : ?>
<td><?php echo $item['img_name']; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>

Categories