display images stored in SQL database - php

I have stored images to SQL database with a text box and store it in database no how can I retrieve the image from database. I used the following code to display but its not working
// Grab the data from our people table
$sql = "select * from upload";
$result = mysql_query($sql) or die("Could not access DB: " . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "<div class=\"picture\">";
echo "<p>";
// Note that we are building our src string using the filename from the database
echo "<img src=$row['data']/>";
echo $row['title'] . " " . $row['title'] . "<br />";
echo "</p>";
echo "</div>";
}

The following link may helps you.
PHP- Image upload to MySQL
set blob type for data column.

Related

My WHILE loop to display videos from mysql table is not working. What am I missing?

I am working on a large project and ran into a problem where I can only display one video from a table of several videos. The first video displays correctly, but the WHILE loop to display the additional videos does not produce any result and seems to end the PHP code altogether.
I have removed all non-essential elements of the code I am currently working on with the exception of comments. Similar code is working elsewhere in my project for images, but this code will not seem to work when displaying videos.
<?php
include_once '../includes/db_connect.php'; // THIS CONNECTS THE DATABASE
include_once '../includes/functions.php'; // THIS PROVIDES VARIOUS FUNCTIONS
sec_session_start();
// THIS ENABLES ERROR MESSAGE DISPLAY //
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
// THIS QUERIES ALL COLUMNS FROM THE 'VIDEOS' TABLE //
$query = mysqli_query($mysqli,"SELECT * FROM videos");
$result = mysqli_fetch_array($query);
// THESE TWO LINES WILL DISPLAY THE FIRST VIDEO IN THE TABLE //
echo "<br><br>" . $result['name'] . "<br>";
echo "<video src='" . $result['location'] . "' controls width='320px' height='200px' >";
while($result = mysqli_fetch_array($query))
{
// THIS SHOULD DISPLAY THE VAR_DUMP INFORMATION //
echo "<br> ******************** TROUBLESHOOTING INFORMATION ********************** <br>";
var_dump($result); // THIS IS JUST FOR DIAGNOSTIC INFO //
echo "<br> ***************************************************************************** <br>";
// THIS SHOULD DISPLAY THE REMAINING 2 VIDEOS IN THE TABLE AS ABOVE //
echo "<br><br>" . $result['name'] . "<br>";
echo "<video src='" . $result['location'] . "' controls width='320px' height='200px' >";
}
The code should display 3 small video images with controls to play the each video. The first video displays correctly so the database is connected, the table is being read correctly, and the commands to display the video are all working, but the WHILE loop is not working. I believe something in the video display line is causing the PHP code to quit but I haven't figured it out yet.
One loop should display all of the videos. You shouldn't have to fetch twice.
/ THIS QUERIES ALL COLUMNS FROM THE 'VIDEOS' TABLE //
$query = mysqli_query($mysqli,"SELECT * FROM videos");
while($result = mysqli_fetch_array($query))
{
// THIS SHOULD DISPLAY ALL ROWS //
echo "<br><br>" . $result['name'] . "<br>";
echo "<video src='" . $result['location'] . "' controls width='320px' height='200px' ></video></br>";
}
In addition, you need to close the </video> tag on each row.

create hyperlink from database MySQL in PHP

I want to get the registered users list on a page but not just their names, but with clickable hyperlink that will lead to their profiles. I have fetched their names but just in text form but how to add hyperlink to that text so that link should lead to their profile.
<?php
$con=mysqli_connect("localhost","root","","dva");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM apnt");
while($row = mysqli_fetch_array($result)) {
echo "<li>";echo "User Reg.";
echo $row['name'];
echo " at ";
echo $row['time'];
echo " on ";
echo $row['day'];
echo "<br>";
echo "<br>";
;
}
mysqli_close($con);
?>
I have got the data from the database and successfully printed it on the page via above code but the hyperlink still needs to be generated and I need help in that please.
Create a profile.php and use GET to get the id number of your user which you send through with the link you create where you fetch your users.

Using PHP variable as name of table in MySQL

use php variable in name of mysql create table
I want to use a PHP variable ( $userAltId ) as the name of the SQL table I am creating. However, despite looking at several examples, I have been unable to successfully create a table with this PHP variable. Using echo I have shown that the variable ( $userAltId ) does contain the predicted value, and if I plug in that value instead of the variable I can successfully create the table.
My latest attempt at the code is below, notice I use $useAltId three times, from creating, inserting, and selecting. Thanks for the help!
<?php
$con=mysqli_connect('localhost', 'alexpnqo_johnny', '?KlI+TtfNEIO', 'alexpnqo_johnny');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
session_start();
$userAltId = $_SESSION['userAltId'];//I am passing $userAltId from another php file
// escape variables for security
$FBname = mysqli_real_escape_string($con, $_POST['FBname']);
echo $userAltId;//echos desired value
$sqlFriendList="INSERT INTO '$userAltId' (FacebookFriend)
VALUES ('$FBname')";
if (mysqli_query($con,$sql)) {
echo "Table created successfully";
} else {
echo "Error creating table: " . mysqli_error($con);";
}
echo "You have added ";
echo $FBname;
echo " to your friend's list!";
echo "<br>";
echo "Below is your friends list";
echo "<br>";
echo "<br>";
$result = mysqli_query($con, "SELECT FacebookFriend FROM '$userAltId'");
echo "<table border='1'>
<tr>
<th>Friends List</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FacebookFriend'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Use backticks $userAltId to all your queries (table name) so that it'll read your variable even spaces.
$sqlFriendList="INSERT INTO `$userAltId` (FacebookFriend) VALUES ('$FBname')";

display number as image in sql

I'm designing a website for a neighbor for a potential restaurant he wants to open. I need to create a page for testimonials/review. I'd like to code it so that the number of stars is in a SQL, I'm just not sure how to do it.
Essentially what I'm looking for is a way to determine the integer of the ratings field (1-5) and for each number, display a star image.
So:
if rating = 1, display star.png once
if rating = 2, display star.png twice
...
if rating = 5, display star.png five times
I'm just not sure how to write the code to do so.
The website is being written in PHP and CSS. I'd like to avoid JQuery, Javascript, .NET, and so forth, as I'm not as familiar with them and will be the one keeping the site up to date.
Here's what I've got so far, but it's not working right, and I get a syntax error:
$result = mysqli_query($con,"SELECT * FROM Reviews");
while($row = mysqli_fetch_array($result))
{
IF (Rating = "1"()){
echo '<img src="star.png">' . }
ELSE IF (Rating = "2"()){
echo '<img src="star.png"><img src="images/star.png">' . }
Else IF (Rating = "3"()){
echo '<img src="star.png">star.png"><img src="images/star.png">' . }
ELSE IF (Rating = "4"()){
echo '<img src="star.png"><img src="images/star.png">star.png"><img src="images/star.png">' . }
ELSE
echo '<img src="star.png"><img src="images/star.png">star.png"><img src="images/star.png"><img src="images/star.png">' .
"<br/> <b>" .
$row['Name'] .
"</b> <em>" .
$row['City'] . $row['State'] . $row['Country'] . "</em><br/>" .
$row['Review'] . "<br/> <hr> <br/>"
}
?>
Use a select statement to get the ratings for a place from your database.
Store the result in a php variable (lets call it $rating)
Use php logic to output number of stars (in html obviously) based on value of $rating.
Hope that helps :)
I would recommend 3 tables for this idea.
Users Table
UserRatings Table
Dish Table
Users table is used to store just that. User information. Possibly a username, password, first name, last name for example. The table should have a primary key. Call it UsersID. It should auto increment itself and be unique for every row.
The Dish table is next. Put a dish name in it. It should have a primary key as well. Call it DishID.
Lastly is the UserRatings table will store UserRatingsId, Rating, InsertTimeStamp, UpdateTimeStamp.
Use a loop to output your HTML based on your rating.
$rating = 4; //Figure this out in your script and set accordingly
for($i = 0; $i < $rating; $i++) {
echo '<img src="star.png" />';
}
Should print out four stars for you.
Help from a friend:
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$starCount = 0;
$result = mysqli_query($con,"SELECT * FROM Reviews");
while($row = mysqli_fetch_array($result)) {
$starCount = $row['Rating'] ;
while ($starCount > 0) {
echo '<img src="images/star.png">';
$starCount -- ;
}
$starCount = 0;
echo "<br/> <b>" . $row['Name'] . "</b> - <em>" .
$row['City'] .", ". $row['State'] ." ". $row['Country'] . "</em><br/>" .
$row['Review'] . "<br/> <hr> <br/>" ;
}
?>
$number=$row->rating ;
$middle="";
$first="<td width='200' align='left'>";
for($x=1;$x<=$number;$x++) {
$middle=$middle.img($fullimage_properties);
}
if (strpos($number,'.')) {
$middle=$middle.img($halfimage_properties);
$x++;
}
while ($x<=5) {
$middle=$middle.img($blankimage_properties); ;
$x++;
}
echo $last=$first.$middle."</td>";

php - using a hyper link to send variable used in data base to use get command to echoe results

I have a table with arrays pulling information from a database, I have linked the fix to be a hyperlink "click me for fix" I have entered the link to send the variable to a php that will use $GET to echoe the information.
code below , i am new to php and been racking brains . the only out put i get is Welcome . (done welcome to test if information was being passed)
<div id=list>
<?php
// Create connection
$con=mysqli_connect('172.16.254.111',"user","password","Faults"); //(connection location , username to sql, password to sql, name of db)
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//where statement in the sql syntax will select where in db to get infor, use AND to add another condition
$result = mysqli_query($con,"SELECT * FROM Fixes WHERE Product='Serv1U' AND Fault_type='Broadcast Manager'"); //this creates a variable that selects the database
//below is the echo statment to create the results in a table format, list collumn titles
echo "<table id=tables border='1'>
<tr>
<th>Products</th>
<th>Fault_type</th>
<th>Fault_Description</th>
<th>Fix</th>
</tr>";
//below is script to list reults in a table format, $row [row name on table]
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Product'] . "</td>";
echo "<td>" . $row['Fault_type'] . "</td>";
echo "<td>" . $row['Fault_Description'] . "</td>";
echo "<td>Click for Fix</td>"; //this is how you link into an echo, alsothe id=" hopefully means i can send ID information.
}
echo "</tr>";
echo "</table>";
// below closes the coonection to mysql
mysqli_close($con);
index.php:
Welcome <?php echo $_GET["Fix"]; ?>.
I'm lost. Any help is appreciated.
Thanks
?>
Is it just a typo here? $GET must be $_GET.
And it should be $row['Fix'] not $rows['Fix']! Note the 's'!

Categories