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
Related
I'm a student studying web programming im trying to display some of information from my db table which are(name, date, dll), all of the information are displayed perfectly except my id number from the db table and also there are no error so its hard for me to detect what i did wrong. can anyone see what i did wrong in my coding and explain what have i missed?
for further reference this is my php coding:
<?php
$con = mysql_connect("localhost", "root", "");
mysql_select_db("tempahperalatan");
if(isset($_POST['hantar']))
{
$noID = 'noID';
$pemohon = $_POST['pemohon'];
$trkhMula = $_POST['trkhMula'];
$trkhAkhir = $_POST['trkhAkhir'];
$n_program = $_POST['n_program'];
$lokasi = $_POST['lokasi'];
$n_anjuran = $_POST['n_anjuran'];
$catatan = $_POST['catatan'];
$masa = $_POST['masa'];
$t_Log = $_POST['t_Log'];
$modified_date;
$modified_time;
$sql = "INSERT INTO daftartempah (pemohon, trkhMula, trkhAkhir, n_program, lokasi, n_anjuran, catatan, modified_date, modified_time) VALUES ('$pemohon', '$trkhMula', '$trkhAkhir', '$n_program', '$lokasi', '$n_anjuran', '$catatan', CURDATE(), CURTIME())";
$res = mysql_query($sql);
}
$viewPerson = "SELECT * FROM daftartempah";
$viewPersonRes = mysql_query($viewPerson);
?>
and this is a table im trying to display some of my info from my db table:
<?php
while($row = mysql_fetch_array($viewPersonRes)){
echo "<tr>";
echo "<td".$row['noID']."</td>";
echo "<td>".$row['trkhMula']."</td>";
echo "<td>".$row['modified_time']."</td>";
echo "<td>".$row['n_program']."</td>";
echo "<td>".$row['pemohon']."</td>";
echo "<td>".$row['n_anjuran']."</td>";
echo "<td>".$row['lokasi']."</td>";
echo "<td>".$row['catatan']."</td>";
echo "</tr>";
}
?>
my table name from my db is: daftartempah
thank you in advance, your help is much needed :)
You have a missing closure for your <td> tag.
echo "<td".$row['noID']."</td>";
^ right there.
That's why it's not displaying.
Therefore:
echo "<td>".$row['noID']."</td>";
and looking at your developer console and the HTML source would have shown you something about it.
Make sure that you also have the opening and closing <table> - </table> tags. That's unknown.
You're also practicing with an outdated API, which isn't good practice to begin with.
Use either the mysqli_ or PDO API for "this century".
http://php.net/manual/en/book.mysqli.php
http://php.net/manual/en/book.pdo.php
You're also open to an serious SQL injection; use a prepared statement:
https://en.wikipedia.org/wiki/Prepared_statement
over the last couple of days I've been working on a application that let's a user upload a image and store the image in my filesystem and the file path in my database. I'm almost done but i have come across a brick wall.
The image gets uploaded to my filesystem and the file path stored in my database just fine. put when i go to the page that displays the images. it returns them as
"broken images"
here's the code that is giving me trouble
<?php
error_reporting (E_ALL ^ E_NOTICE);
session_start();
$uname = $_SESSION['username'];
$userid = $_SESSION['id'];
?>
<!DOCTYPE html>
<html>
<head>
<title>OurFile's Page</title>
</head>
<body>
<?php
require("pdoconn.php");
//img_path is the column in my DB that holds the image URL.
$stmt = $conn->prepare("SELECT img_path FROM ourimages");
$stmt->execute();
while($result = $stmt->fetch(PDO::FETCH_BOTH))
{
echo '<br><img src="' . $result['img_path'] . '" />';
}
$conn = null;
?>
</body>
</html>
any help would be appropriated.
Thank you for your future responses
i edited the code. using ed and jay's suggestions...but its still
output the same result
You can't do this:
<img src="<?php echo $value; ?>" />
because you're using $value to loop over every column in the table. The src attribute needs to be a URL, but I'm guessing only one column in your ourimages table holds a URL. You're outputting an image for every column thanks to this:
$stmt = $conn->prepare("SELECT * FROM ourimages"); // gets every column
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_BOTH);
foreach ($result as $value) // uses every column
{
The simple fix is to change your SQL:
$stmt = $conn->prepare("SELECT whateverColumnHasTheURL FROM ourimages");
Then use that column like this:
<img src="<?php echo $result->whateverColumnHasTheURL; ?>" />
Or, you can use the SELECT * ..., but just use the one column in the <img> tag:
$stmt = $conn->prepare("SELECT * FROM ourimages");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_BOTH);
echo "<br>";?> <img src="<?php echo $result->whateverColumnHasTheURL; ?>" /><?php
Note: If you're actually trying to loop over the rows, you need to put $stmt->fetch in a loop, as in while ($result = $stmt->fetch(PDO::FETCH_BOTH);) { echo... }
Using a foreach loop to output the results of the query is an interesting way of doing things. Most folks use a while loop like this:
while($result = $stmt->fetch(PDO::FETCH_BOTH))
{
echo '<br><img src="' . $result['column_with_image_path'] . '" />';
}
Since you're selecting all of the columns from your table you need to be specific about the identifier you use in the image tag.
Ok obviously i'm a beginner.
What i'm trying to do here is probably simple.
I'm trying to get all the field of my database to echo on my website. So i use this to show only the basic details:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases")
or die(mysql_error());
while($row = mysql_fetch_array( $result )
) {
// Print out the contents of each row into a table
echo '<ul class="releaselist">';
echo '<li>';
echo $row['products_name'];
echo '</li>';
echo '<li>';
echo $row['products_title'];
echo '</li>';
echo '<li>';
echo '<img class="releaseimg" src="'.$row['products_image'].'">';
echo '</li>';
echo '</ul>';
}
?>
So far eveything works fine.
What i want to do with this is get my URL to look like this when i click on a single image.
www.mywebsite.com/detailed.php?id=1
and show everything available in the table RELEASES
The way i've done it which is not working is:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases
WHERE products_id=products_id")
or die(mysql_error());
if ($result){
$row = mysql_fetch_array($result);
echo $_GET['products_id'];
}
?>
If I'm understanding you correctly, your first script needs to be updated to:
echo '';
That will turn your image into a link. At that point your second script should be able to use:
_GET['id']
to retrieve this value and use it in your query. Thusly:
$result = mysql_query("SELECT * FROM releases
WHERE products_id=" . mysql_real_escape_string($_GET['id']));
Now if you really want to products_id in the URL instead of id, just change it 'id' to products_id in the echo line in the first script and all of the $_GET uses in the second script.
Try changing this line:
echo '<img class="releaseimg" src="'.$row['products_image'].'">';
To this one:
echo "<a href='detailed.php?products_id=".$row['products_id']."'><img class='releaseimg' src='".$row['products_image']."'></a>";
Hmm try using $row['products_id'] instead of $_GET['products_id']; $_GET is used when accessing a webpage and the reason that $row['products_image'] worked is because you accessed the database results.
You have misplaced the use of products_id...
Your code should like something like this...
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases
WHERE products_id='".intval($_GET['id'])."'")
or die(mysql_error());
if ($result){
$row = mysql_fetch_array($result);
echo $row['products_id'];
}
?>
I'm a PHP beginner and lately I've been having a problem with my source code.
Here it is:
<html>
<head>
<title>
Bot
</title>
<link type="text/css" rel="stylesheet" href="main.css" />
</head>
<body>
<form action="bot.php "method="post">
<lable>You:<input type="text" name="intrebare"></lable>
<input type="submit" name="introdu" value="Send">
</form>
</body>
</html>
<?php
//error_reporting(E_ALL & ~E_NOTICE);
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("robo") or die(mysql_error());
$intrebare=$_POST['intrebare'];
$query = "SELECT * FROM dialog where intrebare like '%$intrebare%'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
?>
<div id="history">
<?php
foreach($row as $rows){
echo "<b>The robot says: </b><br />";
echo $row['raspuns'];
}
?>
</div>
It returns the result 6x times.
This problem appeared when I've made that foreach because I wanted the results to stuck on the page one by one after every SQL query.
Can you please tell me what seems to be the problem? Thanks!
You are doing it wrong. ;-)
First of all you have to fetch your result with mysql_fetch_array in a loop like this:
while (true == ($row = mysql_fetch_array($result))) {
echo "<b>The robot says: </b><br />";
echo $row['raspuns'];
}
Second I want to tell you that all mysql_* functions are marked as deprecated. If you want to learn PHP you should try to learn how to connect to mysql using PDO.
mysql_fetch_array fetches one row per call. You'll want to do like this:
while ($row = mysql_fetch_array($result)) {
echo "<b>The robot says:</b><br>";
echo htmlentities($row['raspuns']);
}
and get rid of that first mysql_fetch_array.
(Notice that i am HTML-escaping the variable output. Unless you know what you're doing, you should not output raw data into a page.)
By the way, mysql_query is effectively deprecated. It is not at all recommended for new code. Take a look at mysqli (the replacement) or PDO (the new hotness). With the new mysqli (objecty) interface, the PHP part would look a bit like this:
<?php
//error_reporting(E_ALL & ~E_NOTICE);
$db = new mysqli('localhost', 'root', '', 'robo');
# turn into a wildcard
$intrebare='%' . $_POST['intrebare'] . '%';
$stmt = $db->prepare('SELECT * FROM dialog WHERE intrebare LIKE ?');
$stmt->bind_param('s', $intrebare);
$result = $stmt->execute();
echo '<div id="history">';
# 5.4 lets you do it like this;
# older versions, try `while ($row = $result->fetch_assoc())`
foreach ($result as $row) {
echo "<b>The robot says: </b><br />";
echo htmlentities($row['raspuns']);
}
?>
You're only getting one result (only one call to mysql_fetch_array()). There are six columns, I bet, in dialog.
...
$result = mysql_query($query) or die(mysql_error());
?>
<div id="history">
<?php
while($row = mysql_fetch_array($result))
{
echo "<b>The robot says: </b><br />";
echo htmlentities($row['raspuns']);
}
?>
</div>
Also, mysql_* functions are being deprecated. Switch to mysqli_* or PDO.
Use while to fetch all the data and check variable names
while($row = mysql_fetch_array($result)){
echo "<b>The robot says: </b><br />";
echo $row['raspuns']; // Here
}
You are trying it reversed way:
<?php
while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
echo '<strong>The robot says: </strong><br />', $row['raspuns'];
}
?>
Try now :)
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;