PHP MYSQL displaying picture link from dB - php

Hey all I have a table caleld Box1, with a simple structure of: id(increment,primary) imageurl, link.
What I am looking to do, is on my site in the HTML file, display the imageurl into a imgsrc, so that the Image URL loads the image when going to the site
I cannot find specifically what I am looking for online and know I must be close somewhere, this is what I have so far in my coding for my .php file.... thanks.
<?php
$con = mysql_connect("localhost","data","data");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}else {
mysql_select_db("database1", $con);
$result = mysql_query("SELECT * FROM Box1 ORDER BY id DESC LIMIT 1");
while($row = mysql_fetch_array($result))
{
echo $row['image'];
// NEXT STEP IS TO DISPLAY THE the IMAGE EXAMPLE: '<img src code<?php echo 'image'; ?>' >
}
}
mysql_close($con)
?>
if you could guide me to an exmaple of how to do this, that would be amazing, im so confused how one would implement HTML with PHP, thanks!!

<?php
... connect to DB ...
$sql = "SELECT imgurl FROM Box1 WHERE id=XXX";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
?>
<img src="<?php echo $row['imgurl'] ?>" />

Also, it is helpful to become familiar with this operator for large chunks of html
echo <<<YOUR_TOKEN_NAME
<img src="$row['imgurl']" />
...misc html and php vars intermixed
YOUR_TOKEN_NAME;

Related

php mySQL how to show images, problems on img src

I've been working on this for four hours and I still have no clue, so I came here to seek help. I'm learning PHP and mySQL and one of the way I learn is to learn from open source projects. Today I'm trying to understand an open source project and I have some problems. Here is the link to the open source project: https://github.com/markpytel/Printstagram Basically, it has something like the following, let's call it profileinfo.php (name of this file is pretty deceiving, it is a page that shows images uploaded by different users)
<?php
$sql="SELECT pid,poster, pdate FROM photo WHERE poster='$myusername' OR pid
in (select pid from tag where taggee='$myusername') OR pid in (select pid
from ingroup natural join shared where username='$myusername' and username
!= ownername) ORDER BY pdate desc";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<hr>";
echo "Posted by: " . $row["poster"]. " Time: " . $row["pdate"]."<br>";
// pid is each photo's unique id in the database
$pidrow=($row["pid"]);
?>
</head>
<body>
<form action="listsingleREV.php?pidrow=<?php echo $pidrow; ?>" method="POST">
<input type="submit" id="pidrow" value="View this image" />
</form>
</body>
</html>
If you click on the button that says "view this image", image uploaded by users will appear. The first question I have is: What's the meaning of the question mark in the image src. I understand that there is a PHP tag in the img src, but I don't understand why there is a question mark between listsingleREV.php and pidrow.
listsingleREV.php looks like this:
<?php
session_start();
$pidrow=$_GET['pidrow'];
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("printstagram");
$sql = "SELECT pid FROM photo WHERE pid=$pidrow";
$result = mysql_query($sql);
?>
<?php
while($row = mysql_fetch_array($result)) {
?>
<img src="imageview.php?pid=<?php echo $row["pid"]; ?>" /><br/>
<?php
}
?>
imageview.php looks like this:
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("printstagram") or die(mysql_error());
if(isset($_GET['pid'])) {
$sql = "SELECT image FROM photo WHERE pid=" . $_GET['pid'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
header("content-type: image/jpeg");
echo $row["image"];
}
mysql_close($conn);
?>
Of course, if a user has to click on a button to see each image, it is very inconvenient. So I decide to give myself some practice and modify the code(it is Apache license so I can do it)such that images will be automatically presented on profileinfo.php without the need to click on a button. Since imageview.php and listsingleREV.php show the images, I tried to substitute the form in profileinfo.php with these two files. I worked on it for four hours without achieving my goal. Can someone tell me the correct way to show the images on profileinfo.php without the need to click on the button?
As it appears in imageview.pho the $row[pid] contain the link of your image in photo table so add this line to profileinfo.php under the while loop in the place you want it to display
<img src="<?php echo". $row["pid"]."; ?>">

how to make echo PHP link

I have a table called as feedback and the table look like this;
UserName image
abc image/x.jpeg
def image/y.png
I want to make a link in php by using echo.
my coding as I try is ,
<?php
require_once('Connections/localhost.php');
mysql_connect("localhost","root","");
mysql_select_db("iis");
$result= mysql_query("SELECT * FROM feedback") or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo ''.$row['UserName'].'<br />';
}
?>
I want results in a form as below,
abc = image/x.jpeg
def = image/y.png
so that when you click on the link, it will direct to the folder image and show the image stored by each person.
Suggest you to do this.
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr><td>'.$row['UserName'].' </td><td>=</td><td>'.$row['image'].'</td></tr>';
}
echo '</table>';

Want to show images information from Database

I am new to PHP, I want to get image information from database when I click on a specific image.
This is the code I'm using, could you help me out?
Code:
<?php
$connection = mysql_connect("localhost","root","");
$select_db = mysql_select_db("fashion",$connection);
$winter = mysql_query("Select * from winter",$connection);
while($row = mysql_fetch_array($winter))
{
echo "<img src=\"winter images/" . $row['image_name']. "\" width=\"200\" //height=\"293\"/>";
}
?>
Did you try echo $row['image_name']; to see whether you are getting the image name or not. If you are getting it properly then try this
while($row = mysql_fetch_array($winter))
{
$imagePath='winter/'.$row['image_name'];
echo '<img src="'.$imagePath.'" width="200" height="293" >';
}
1. Why are you messing up so much with the image tag in your echo statement. if you put double quotes inside the single quotes then also it will work.
2. **Please stop using *mysql_ as it was deprecated before it was deprecated.
Since mysql_fetch_array only pulls regular array, replace it with mysql_fetch_assoc. This should work !!
<?php
$connection = mysql_connect("localhost","root","");
$select_db = mysql_select_db("fashion",$connection);
$winter = mysql_query("Select * from winter",$connection);
while($row = mysql_fetch_assoc($winter)){
echo '<img src= "folder/'.$row["image_name"].'" width="200" height="293">';
}
?>
Since mysql is deprecated, try using Mysqli or PDO

PHP/MYSQL: How to create image galleries that rotate/change during each page refresh/load

I am building a website that will consist of only images on a single page. There are 300 Images on this page. The home page. Each one of these images is a link and can be clicked. I have a database and this database contains a table where the image URL and the link/target URL exists for each image. There are 2,000 image in the database and only 300 images positions on the page.
Using PHP/MySQL how can I make it so that every time the page is loaded/refreshed new images are shown. So that it randomly (however each image needs to be different relative to each refresh. No duplicates on page) grabs 300 images out of the database to be displayed on the page. Kinda like a rotating advert would work. Each time the page is loaded a new advertisement is shown.
It doesn't have to show 300 different images each time the page is refreshed It just can't show duplicates at anyone time on the page.
Looking for a quick solution, ideas, or links to tutorials that I can follow. Thanks!
EDIT:
Here is the code I currently have so far..
<?php
// Connects to the database.
$connection = mysql_connect("localhost", "root", "");
if (!$connection) {
die("Connection Failed: " . mysql_error());
}
// Selects a database from the previously opened connection.
$select = mysql_select_db("affiliate_links", $connection);
if (!$select) {
die("Connection Failed: " . mysql_error());
}
// Performs query to table 'affiliates' in previously opened database.
$result = mysql_query("SELECT * FROM affiliates ORDER BY RAND() limit 1", $connection);
if (!$result) {
die("MySQL Query/Connection Failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
}
?>
** The following code is where I want each new image to appear on the page.
<img src="<?php echo $row['thumb_url'] ?>">
<img src="<?php echo $row['thumb_url'] ?>">
<img src="<?php echo $row['thumb_url'] ?>">
<img src="<?php echo $row['thumb_url'] ?>">
<img src="<?php echo $row['thumb_url'] ?>">
<img src="<?php echo $row['thumb_url'] ?>">
As is, the code above(the echo $row statements) does not work. The only way I have been able to pull the data from the data base is by putting the echo statements right under the 'while loop' like this.
while ($row = mysql_fetch_array($result)) {
echo $row['target_url']." ".$row['thumb_url']."<br />";
If I keep ORDER BY RAND() limit 1 in the code it will only show me 1 iteration. 1 row of the table. If I remove it I see all the links/image links where ever I echo it out right under the while loop.
So basically what I need now: Be able to echo out the $row OUTSIDE of the while loop and it be displayed inside the html to complete the links. And I need it to beable to display a new image during each iteration of the loop. OR if there is a better way to do it than a while loop. Thanks!
How about storing all the results from the database in an array that you can use anywhere you want. I also changed the SQL query to limit the results to 300 and select only the required columns.
<?php
// Connects to the database.
$connection = mysql_connect("localhost", "root", "");
if (!$connection) {
die("Connection Failed: " . mysql_error());
}
// Selects a database from the previously opened connection.
$select = mysql_select_db("affiliate_links", $connection);
if (!$select) {
die("Connection Failed: " . mysql_error());
}
// Performs query to table 'affiliates' in previously opened database.
$result = mysql_query("SELECT target_url, thumb_url FROM affiliates ORDER BY RAND() limit 300", $connection);
if (!$result) {
die("MySQL Query/Connection Failed: " . mysql_error());
}
$images_array = array();
while ($row = mysql_fetch_array($result)) {
$target_url = $row['target_url'];
$thumb_url = $row['thumb_url '];
// you might consider changing the next line to include a class on the images for formatting purposes
$image_link = '<img src = "' . $thumb_url . '">';
$images_array[] = $image_link;
}
// use this wherever you want to display your images
foreach ($images_array as $current){
echo $current . '<br>';
}
?>
EDIT - to add banner text in the middle change the loop to
$counter = 0;
while ($counter < 150){
echo $images_array[$counter];
$counter++;
}
// put banner text here
while ($counter < 300){
echo $images_array[$counter];
$counter++;
}

Video not streaming mysql php

I have no idea why that code doesn't work...
no error message nothing...
thanks
<?php require_once('sqlscript.php'); ?>
<?php
$link = mysql_connect('mysql', 'user', 'pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db(database);
$sql = 'SELECT * FROM `videos` ORDER BY creation_date desc LIMIT 3';$result=mysql_query($sql);
$row = mysql_fetch_array($result);
while($row = mysql_fetch_array($result))
{
echo '<video width="320" height="240" controls> <source src="/upload/'.$row['path'].'"> Your browser does not support the video tag. </video>
<br />';
}
mysql_close();
?>
The column creation_date is not exist in the table videos that will result a syntax error that could be not appearing in the page because you turned off the error displaying
you have another note here but not related to your issue you called mysql_fetch_array($result); before calling it in the loop and that will result loosing the first row in your results of query
Is there anything in your database tables?
If there isn't, you would be able to run the script, and it would run without any output or errors.
The mysql query wouldn't fail, and the while loop would never run.
Try putting something like this that checks to see if you get any data returned from your query.
var_dump($result);
And depending on the output now, as it will print something, even if there isn't any data in your table, people can have an easier time finding problems.
Remove 2 times fetch_array, use instead fetch_assoc and stop using mysql_* since its deprecated.
Creation_date doesn't exist.
Try this:
<?php require_once('sqlscript.php'); ?>
<?php
$link = mysql_connect('mysql', 'user', 'pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db(database);
$sql = 'SELECT * FROM `videos` ORDER BY id desc LIMIT 3';
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo '<video width="320" height="240" controls> <source src="/upload/'.$row['path'].'"> Your browser does not support the video tag. </video>
<br />';
}
mysql_close();
?>

Categories