I have this code has been working for few months without any problem. Now this page does not work and the problem from php code but I dont know where
<?php
$query = "SELECT * FROM offerimage order by name";
$result = mysqli_query($connection, $query);
if ($result->num_rows > 0) {
echo "<table>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
$image = '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'" height="100" width="100"/>';
//echo '<br></br>';
echo $image;
echo '<br></br>';
echo "</tr>";
}
} else {
echo "</table>";
}
?>
I GET THIS ERROR
"ERR_EMPTY_RESPONSE"
Why don't you output something when the query returns no rows, since that's probably the cause?
Also you should have the images inside a <td> element.
Edited to show row-by-row retrieval of images. Assumes the database table offerimage has a unique integer ID (like an AUTO_INCREMENT column) named id, change queries to match the actual table definition.
<?php
// get array of offerimage IDs ordered by name
$query = "SELECT id FROM offerimage order by name";
$result = mysqli_query($connection, $query);
$ids = [];
while ($row = $result->fetch_assoc()) {
$ids[] = (int)$row['id'];
}
if (count($ids) > 0) {
// display each image
echo '<table>';
foreach ($ids as $id) {
$query = "SELECT image FROM offerimage WHERE id = {$id}";
$result = mysqli_query($connection, $query);
$row = $result->fetch_assoc();
echo "<tr><td>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'" height="100" width="100"/>';
echo "</td></tr>";
mysqli_free_result($result);
}
echo "</table>";
} else {
echo "<p>No offers found.</p>";
}
Other suggestions: Don't use SELECT * if you only need one column. Use SELECT image instead.
Check for errors when making the database connection (you don't show that code) and from the query, use error_log() or send to the browser so you can see if something went wrong there.
I solve it by save images in folder and then save image location in DB.
Related
Having a problem uploading a picture from a database, wondering if i'm doing something wrong..
I've checked that the image is there by using <img scr='/upload/imgpath.jpg'>
Any suggestions?
$sql1 = "SELECT pic, stocknr, status FROM stock WHERE status = '1'";
$result = mysqli_query($con,$sql1);
while($row1 = mysqli_fetch_assoc($result));
{
$stocknr2 = $row1['stocknr'];
$picname = $row1['pic'];
$upfile = $picname;
echo "<center>";
echo "<a href='details.php?stocknr=$stocknr2'>";
echo "<img src='/upload/$upfile' border=1 width=350 height=280></a>";
echo "</center>";
}
mysqli_free_result($result);
You just need to change while row;
while($row1 = mysqli_fetch_assoc($result)
I have managed to create dropdown from Mysql column and also get query result with get method but here webpage is directed to other page when hit button.
I am looking to get query output with some default option set in dropdown when page loads or want query result on same page reloading it again when user changes option.
Any help will be appreciated.
Code on main page for dropdown:
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id");
echo "<form action='process.php' method='get'>";
echo "<html>";
echo "<body>";
echo "<p></p>";
echo "<center>";
echo "<strong> Select Base Verison To Compare With : </strong>";
echo "<select name=nx_version>";
while ($row = $result->fetch_assoc()) {
unset($nx_version);
$nx_version = $row['nx_version'];
echo '<option value>'.$nx_version.'</option>';
}
echo "</select>";
echo " <button type='submit'>See items</button>";
echo "</center>";
echo "</body>";
echo "</html>";
echo "<p></p>";
echo "<form>";
Code I wrote when hit button and gives query output (in process.php):
$nx_version = $_GET['nx_version']; // The name attribute of the select
$query = "SELECT step1 FROM workflow1 WHERE nx_version = '$nx_version' ORDER BY id DESC";
$query1 = mysqli_query($conn, $query);
$array = Array();
while($result1 = $query1->fetch_assoc()) {
$array[] = $result1['step1'];
}
print_r($array);
process.php file should be like this -
<?php
session_start();
$nx_version = $_GET['nx_version']; // The name attribute of the select
$query = "SELECT step1 FROM workflow1 WHERE nx_version = '$nx_version' ORDER BY id DESC";
$query1 = mysqli_query($conn, $query);
$array = Array();
while($result1 = $query1->fetch_assoc()){
$array[] = $result1['step1'];
}
$_SESSION['data'] = $array;
// storing the data as session
header("location:main_page.php");
?>
Now get back the data from session in your main page by adding this-
$array = $_SESSION['data'];
$sql = "SELECT * FROM today WHERE heading='$heading' and day='$day'";
$sql1 = "SELECT * FROM today WHERE day='$day'";
$result = $conn->query($sql);
$result1 = $conn->query($sql1);
if ($result->num_rows > 0) {
echo "<div id='post'><h1>".$row["heading"]."</h1>
<aside class='related-post'>".while($row = $result1->fetch_assoc())
{echo'<img src='".$row["image"]."'>;}
.</aside>}";
I have been using while loops for fetching data from table. My connection is working is perfect but I need another loop in the first that is not working. Isn't it the good way?
Update: I tried to finish echo and again started as follow but still an error
while($row = $result->fetch_assoc()) {
echo "<div id='post'><h1>"
.$row["heading"].
"</h1><div class='post-side'><img class='post-image' src='"
.$row["image"].
"'><div class='post-data'><p><strong>Age: </strong><span>$age</span></p><p><strong>Date of birth: </strong><span>"
.$row["day"].
"-"
.$row["month"].
"-"
.$row["year"].
"</span></p></div></div></div><div class='description'><p>"
.$row["description"].
"</p></div><div class='bottom-related'><aside class='related-post'>";
while($row = $result1->fetch_assoc())
{echo"<img src='"
.$row["image"].
"'>/";}.echo"</aside><aside class='ad2'>".$includead."</aside></div>";
}
echo "</div>";
} else {
echo "No table found";
}
$conn->close();
You're trying to concatenate to a string a WHILE loop; this is wrong.
You should echo your first part, end with it and then do your while loop, and echo the end afterwards:
Your quotes are a bit messed up as well
if ($result->num_rows > 0)
{
echo "<div id='post'><h1>".$row["heading"]."</h1>
<aside class='related-post'>";
while($row = $result1->fetch_assoc())
{
echo'<img src="'.$row["image"].'">';
}
echo '</aside>';
}
You can't concatene while with String, it's a syntaxic error
Also, you have a probleme when trying to echo a String, you can use this syntax:
echo "PHP"; // will evaluate PHP variables and whitespace inside a string
echo 'PHP'; // will evaluate nothing;
But you can not start flushing a string ' and finish by " or vice-versa.
Here the correct code :
<?php
$sql = "SELECT * FROM today WHERE heading='$heading' and day='$day'";
$sql1 = "SELECT * FROM today WHERE day='$day'";
$result = $conn->query($sql);
$result1 = $conn->query($sql1);
if ($result->num_rows > 0) {
echo "<div id='post'><h1>" . $row["heading"] . "</h1><aside class='related-post'>";
while($row = $result1->fetch_assoc()) {
echo'<img src="' . $row["image"] .'">';
}
echo "</aside>";
}
The code is supposed to get a row at each loop and build td elements for each data piece. However, it only gets some rows while missing others even though all rows were selected:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
foreach ($rows as $val) {
echo "<td>{$val}</td>";
}
}
mysql_fetch_assoc returns an associative array. To display each returned row, do something like this (taken directly from the php.net page for mysql_fetch_assoc
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
Try removing the curly braces like this:
echo "<td>$val</td>";
Beside you didn't close the <tr> tag. So you should do this:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
foreach ($rows as $val) {
echo "<td>$val</td>";
}
echo"</tr>";
}
Now like Jack Albright said you should consider various columns of your table to display specific data. Upon that the while() is already a look, I think you don't need to add any foreach() inside. SO your final code should look like this:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
echo "<td>$rows['columnName']</td>";
echo "</tr>";
}
I have a php script that tries to find a particular name in a database where the specified program is $q, a variable passed from an html page. I'm very new to this so I'm having trouble figuring out how to code an if not found, then display type of message. Below is what I currently have:
$sql="SELECT * FROM names WHERE program='".$q."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
And I need it to echo a message saying if nothing was found. I tried looking at NOT IN condition in SQL and http://www.techonthenet.com/sql/exists.php along with other things on the internet but I'm not sure if this is the right thing to use. Any help would be appreciated.
use th php function
mysql_num_rows($result);
to check results found
So your code should be like:
$sql="SELECT * FROM names WHERE program='".addslashes($q)."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
while($row = mysql_fetch_array($result))
{
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
else
echo "No data found";
please note i added
addslashes($q)
in query, in order to avoid SQL injection problems.
$sql="SELECT * FROM names WHERE program='".$q."'";
$result = mysql_query($sql);
$found = false;
while($row = mysql_fetch_array($result))
{
$found = true;
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
if ($found == false)
echo "I found nothing";