Retrieving images from MySQL Database - file path - php

I've got my query working, it brings me the results I expect, but one column is going to be images, and I'm unsure of the best way to go about displaying them.
Initially I used the below code to get my results, without worrying about the images.
<?php
#requires connect script - similar to include to avoid including security details
require 'connectscript.php';
#query
$sql = "SELECT * FROM products WHERE CategoryName = 'Surfboards'";
#result
$result = mysqli_query($dbc, $sql) or die ("Bad Query: $sql");
#Opening table
#while loop to bring all results
echo "<table border='2'>";
echo "<tr><td>ProductID</td><td>Name</td><td>Brand</td><td>Model</td>
<td>Board Length</td><td>Board Type</td><td>Colour</td><td>Image</td>
<td>UnitPrice</td></tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$row['ProductID']}</td>
<td>{$row['Name']}</td>
<td>{$row['Brand']}</td>
<td>{$row['Model']}</td>
<td>{$row['BoardLength']}</td>
<td>{$row['BoardType']}</td>
<td>{$row['Colour']}</td>
<td style='width:200px; height: 100px; overflow-hidden;'>{$row['Image']}</td>
<td>{$row['UnitPrice']}</td>
</tr>";
}
echo"</table>"
?>
I've also adjusted the code slightly with an img src in there, and it presents the broken as if it's looking for an image, but can't find one.
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>{$row['ProductID']}</td>";
echo "<td>{$row['Name']}</td>";
echo "<td>{$row['Brand']}</td>";
echo "<td>{$row['Model']}</td>";
echo "<td>{$row['BoardLength']}</td>";
echo "<td>{$row['BoardType']}</td>";
echo "<td>{$row['Colour']}</td>";
echo "<td style='width: 400px; height: 400px; overflow-hidden;'><img
src=images/{$row['Image']}</td>";
echo "<td>{$row['UnitPrice']}</td>";
echo "</tr>";
}
echo"</table>"
?>
The images are stored in an images folder, within the htdocs folder on my xampp installation. I've double checked the path. The data type us VARCHAR and i've tried with and without the extention.
Any Suggestions?
Thanks
Will

Related

How could you use PHP and a SQL database to change HTML <h> content?

I have a webpage that displays cars from the first car in the table to the last car with a while loop.
I have the following columns: Make, Model, Price. In my syntax I have an anchor tag around the Make rows that links to the description page of the Make you click on.
I want my <h> tags to change to the Model of the corresponding Make.
I've spent over an hour trying to achieve this but all I could come up with is this:
<?php
$query = "SELECT Model FROM inventory;";
$Vtitle = $conn->query($query);
$Vtitle_ar = mysqli_fetch_assoc($Vtitle);
echo "<h1>".$Vtitle_ar['Model']."</h1>";
?>
This works to an extent.
Every anchor I click replaces the <h> tags with only the first result under the Model column in my database.
Here is my code for the the entire car inventory page
<?php
$query = "SELECT * FROM inventory;";
/* Try to query the database */
if ($result = $conn->query($query)) {
// Don't do anything if successful.
}
else {
echo "Error getting cars from database:" .$conn->error()."<br>";
}
// Create the table headers
echo "<table id='Grid' style='width: 80%'><tr>";
echo "<th style='width: 50px'>Make</th>";
echo "<th style='width: 50px'>Model</th>";
echo "<th style='width: 50px'>Asking Price</th>";
echo "</tr>\n";
$class = "odd"; // keep track of whether a row was even or odd, so we can style it later
// Loop through all the rows returned by the query, creating a table for each row
while ($result_ar = mysqli_fetch_assoc($result)) {
echo "<tr class=\"$class\">";
echo "<td><a href='viewcar.php?VIN=".$result_ar['VIN']."'>".$result_ar['Make']."<a></td>";
echo "<td>".$result_ar['Model']."</td>";
echo "<td>".$result_ar['ASKING_PRICE']."</td>";
echo "</td></tr>\n";
// if the last row was even, make the next one odd and vice-versa
if ($class=="odd") {
$class = "even";
}
else {
$class = "odd";
}
}
echo "</table>";
$conn->close();
?>
Does anyone how I can do this?
I'm new to programming and I'm trying to use this for an actual project I'm working on for a hair salon's website
Add a WHERE clause to the query.
$vin = $_GET['VIN'];
$stmt = $conn->prepare("SELECT Model FROM inventory WHERE VIN = ?");
$stmt->bind_param("s", $vin);
$stmt->execute();
$stmt->bind_result($model);
$stmt->fetch();
echo "<h1>$model</h1>";
Though not a solution resolved with the use of a where clause as given by #Barmar whilst formatting the code I did find an error within the HTML which was not immediately obvious
The line echo "</td></tr>\n"; has an extra </td> which would break the flow of the html and can have detrimental effects. Also, // Don't do anything if successful. makes no sense - if there are results then process the recordset otherwise show the error ;-)
<?php
$query = "SELECT * FROM inventory;";
if ( $result = $conn->query( $query ) ) {
echo "
<table id='Grid' style='width: 80%'>
<tr>
<th style='width: 50px'>Make</th>
<th style='width: 50px'>Model</th>
<th style='width: 50px'>Asking Price</th>
</tr>";
$i=0;
while( $result_ar = mysqli_fetch_assoc( $result ) ) {
$class = $i %2 == 0 ? 'even' : 'odd';
echo "
<tr class='$class'>
<td><a href='viewcar.php?VIN={$result_ar['VIN']}'>{$result_ar['Make']}<a></td>
<td>{$result_ar['Model']}</td>
<td>{$result_ar['ASKING_PRICE']}</td>
</tr>";
$i++;
}
echo "</table>";
} else {
echo "Error getting cars from database:" .$conn->error()."<br>";
}
$conn->close();
?>
For styling alternate table rows ( the above uses a modulus function to calculate odd/even ) you can do it with some simple CSS - such as
tr:nth-child( odd ){/* rules */}

how to add scrollbar to table in php?

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\">";

cannot display multiple image

Image cannot be displayed in html page.
for example code :
<?php
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id='$id'");
while ($row =mysql_fetch_array($data))
{
$location = $row['location'];
echo '<img src="'.$location.'" width=30% height=10%>';
echo '<td><div align="center">Delete</div></td>';
echo "<br>";
}
?>
One obvious mistake in your code is that the closing tag '>' is missing for img. So it needs to be
echo "<img src='$location' width='30%' height='10%'>";
Apart from this be sure $location var has the correct absolute or relative path picked from DB to show the image. Then, you are using $id in the query. Be sure this is not the unique ID in the img_homestay table, as that will only return you one row. I believe you want to fetch all the images for a particular post id, so ensure that you are using that ID only for the correct field in the query.
Another suggestion that done switch between double quote and single quote string notation in PHP. This will make your code hard to read and comprehend. In one echo statement you are using
echo " ";
and in next statement you are using:
echo ' ';
Try with this
<?php
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id='".$id."'");
while ($row =mysql_fetch_array($data))
{
$location = $row['location'];
?>
<img src="<?php echo $location;?>" width="30%" height="10%">
<td><div align="center">Delete</div></td>
<br>
<?php }
?>
<?php
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id='".$id."'");
while ($row =mysql_fetch_array($data))
{
$location = $row['location'];
echo '<img src="'.$location.'" width=30% height=10%>';
echo '<td><div align="center">Delete</div></td>';
echo "<br>";
}
?>
Your processing is wrong. Your query is select * from img_homestay WHERE id='$id' which means it's going to fetch a single row (I assume, because you're making a query based on an ID which, I again assume, is a unique key) so you actually don't need to use the while loop (if you intend to use single image).
Still, if that's not the case, you may need to use <tr> for each row, so maybe, try this:
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id=$id");
while($row=mysql_fetch_array($data)){
$location = $row['location'];
echo "<tr><td><img src='$location' width=30% height=10%>";
echo "<td><div align='center'><a href='#' imgid='$row[imgid]' class='delbutton' title='Click To Delete'>Delete</a></div></td>";
echo "</tr>";
}
?>
Also, I see, you're using td here which means, you're using table but I see no table tags. My guess is, there is some issue in table structure. A better check to see if you're image is being parsed or not would be to check the source code of the rendered HTML page. I'm sure you're getting the img tag in resulting HTML but it is not being rendered due to error in HTML structure so you better check and correct your HTML with respect to the table you're using.

Get line breaks in the my-sql_fetch_array table results

I have a simple project that I am working on and I'm having a hard time finding the code I need to get line breaks in the following table:
<?php
// connect to the database
$host = '###';
$username = '###';
$pass = '####';
mysql_connect($host,$username,$pass) or die(mysql_error());
mysql_select_db("####") or die(mysql_error());
// select everything from the table
$query = "SELECT * FROM Employees";
$result = mysql_query($query) or die(mysql_error());
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
mysql_close();
?>
Everything works correctly and it grabs data from the correct database and table. But when it displays results it is all on the same line ("record1record2record3") and I'd like a line break between employee records.
I've searched this question and it seems like my results all show me an entirely different way of doing this. I've already got the code written and fussed with it a lot to get it working. Can I just make a simple alteration to the above code to get the breaks I want?
The <tr> tags must also be inside the while loop so that they are outputted for each row. They make the rows in the HTML table.
while( ($row = mysql_fetch_array($result)))
{
echo "<tr>";
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
echo "</tr>";
}

make table using php data

I have created a table using information from my database which all works fine. I have this all echo'd back in position how i want it.
My question is when i add a background to this, it adds a background to everything in every row. How can i add a background to each row with maybe 10px padding in between each background?
echo "\n<table id=\"messageboard\" cellpadding=\"0\" cellspacing=\"0\">\n";
echo "<tr><th width=\"150px\" style=\"text-align: center;\"></th>\n";
echo "<th width=\"330px\" style=\"background-color: #c01718;\"></th>\n";
echo "</tr>";;
while($row = mysql_fetch_array($result)){
echo "<tr><td>\n";
echo $row ['username']."<br />".$row ['date_time'];
echo "<td>\n";
echo $row ['message'];
echo "</td></tr>\n";
}
You need to add the css class to the tr in order to give a style for every row
The starting row line should read
echo "<tr class=\"styledRow\"><td>\n";
Check this out:
http://www.utexas.edu/learn/html/colors.html
your problem can be solved by to ways:
add style in tr tag ,and give padding to tr 10px or more
alternatively
$style = ['style1','style2']
$i = 0
while($row = mysql_fetch_array($result))
{
if {$i ==0{$i = 1}}
else {$i = 0}
echo "<tr";
echo "class=$style[$i]";
echo "><td>\n";
echo $row ['username']."<br />".$row ['date_time'];
echo "<td>\n";
echo $row ['message'];
echo "</td></tr>\n";
}
now in css define two style sheet style1 and style2, your table row will have alternating style as style1 and style2,
please ignore syntax error

Categories