Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have 2 php files that retrieve a BLOB image from mysql DB. My DB has stored a couple of different images, as i try to display them on the browser, only the first image is displayed multiple times, for example: The DB table has stored 5 images, the browser is going to display the first image 5 times.
here is a snippet from my main php file:
$strSQL = "SELECT * FROM images";
$rs = mysql_query($strSQL) or die (mysql_error());
echo "<table>";
while($row = mysql_fetch_array($rs)) {
echo "<tr><td>";
echo " <img src=load_pic.php?id=".$row["id"]." id='img' width='100' height='100'></a>";
echo "</td></tr>";
}
echo "</table>"
and the php file that gets the images "load_pic.php"
$q="select * from images";
$rec=mysql_fetch_array(mysql_query($q));
$data=$rec['image'];
header('Content-Length: '.strlen($data));
header("Content-type: image/".$rec['type']);
echo $data;
Your load_pic.php script is not using the id parameter. It should be:
$q = "select * from images where id = " . mysql_real_escape_string($_GET['id']);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
this my code but unable to display image from db in php.i think this is not pick up the path.any one can help in this regard.
<?php
include('connect.php');
$result = $db->prepare("SELECT image FROM info WHERE empid= '". $empid ."'");
$result->bindParam('. $empid .', $empid);
$result->execute();
for($i=0; $rows = $result->fetch(); $i++){
echo '<img src="images/".$row["image"]." ">';
echo '<img src="images/".$row["image"]. > ' ;
}
?>
You either concatenate the id onto the text string containing the query or use a parameter place holder and then bind a value to it. Not both, as you were doing.
The most secure way is to use parameters.
<?php
include('connect.php');
// I assume you have set $empid somewhere in the missing code here
$result = $db->prepare("SELECT image FROM info WHERE empid= :empid");
$result->bindParam(':empid', $empid, , PDO::PARAM_INT);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
// also changed these 2 rows to correct the concatenation
echo '<img src="images/"' . $row["image"] . '">';
echo '<img src="images/"' . $row["image"] . '">';
}
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
My search is running perfectly but I need to link each result to a description page. Any ideas as to how to do it?
<?php
session_start();
require_once 'login.php';
$conn = mysqli_connect($server,$user,$password,$dbase);
if(!$conn){
die("Failure to connect".mysqli_connect_error());
}
$id = $_SESSION['twitcher'];
$srch = $_POST['searchstr'];
$sql = "select * from gallery where title like '%$srch%'";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result) > 0){
while ($rows=mysqli_fetch_assoc($result)){
echo $rows['title'].$br.$br;
}
}else echo "No posts found.";
mysqli_close($conn);
?>
Exactly as #Dagon stated: You need to simply echo out a link tag (<a>)
while ($rows=mysqli_fetch_assoc($result)) {
echo "<a href='/link_to_where_you_want_to_go'>" . $rows['title'] . "</a>" . $br . $br;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hello i am beginner to PHP.I am not getting an idea regarding how to get all the rows as an output in the form of anchor tags .Here is my following code where blogtable is the name of the table . For instance i have five rows in a table . I want the entire rows as links as a resultant.By executing the following code i am getting the records in a table but unable to transform these results into anchor tags.Kindly suggest me an idea to get each and every row in the form of anchor tag.Thanks in advance.
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("test");
$data = mysql_query("select * from blogtable");
while ($col = mysql_fetch_field($data)) {
echo $col->name;
}
while ($row = mysql_fetch_row($data)) {
for ($i = 0; $i < count($row); $i++) {
echo"<br/>";
echo"$row[$i]></a>";
echo"<br/>";
}
}
?>
Just add your link inside the for loop:
echo "<a href='http://example.com'>" . $row[$i] . "</a>";
Try this: echo "".$row[$i]."";
echo"$row[$i]></a>;
This is not the correct syntax for an anchor tag. Try something like:
echo "<a href='some site'>$row[$i]</a>";
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
New to php. Trying to create a list of images with text in a loop. The script works fine with just , but I want to add a class name to it so that I can control it with css, but I am getting an error. Seems not to flow cleanly with HTML. Any suggestions?
$rs = $conn->Execute("SELECT ProductName, ProductID FROM Products ");
//opens a recordset from the connection object
while (!$rs->EOF) {
$fv1=$rs->Fields("ProductID");
$fv2=$rs->Fields("ProductName");
print "<div class="product-img"> ";
print "<a href='moredetails.php?productid=$fv1'>";
print "<img src = \"thumbs/artifact-$fv1.jpg\" alt = \"Artifact-$fv1\"</a><br>";
print "<a href='moredetails.php?productid=$fv1'>";
print "$fv2</a><br>";
print "</div>";
$rs->MoveNext();
}
$rs->Close();
The double quotation will close the php function, try to use a single quotation for your class attr.
print "<div class='product-img'> ";
or escape it
print "<div class=\"product-img\"> ";
Change
print "<div class="product-img"> ";
to
print "<div class=\"product-img\"> ";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table called "profile" in mysql database
Table structure:
user | hasimg | imgurl
As you can see "hasimg" and "imgurl", normally if a user does not have an image, hasimg will be set to 0 and imgurl is set to none.
On the other hand, if a user has an image assigned then hasimg is set to 1 and imgurl will contain the link.
How can I display ones that have images (hasimg set to 1) with <img src="imgurl html tags?
How can I display ones that do not have images (hasimg set to 0) , without <img src?
I want to make a list which display users with images and user that does not have image.
Here's the code im using to echo out the data row by row
<?php
$sql = "SELECT * FROM profile ORDER BY user DSC";
$query = mysql_query($sql)or die(mysql_error());
$rows = array();
while($row = mysql_fetch_array( $query )){
$rows[] = $row;
echo "User:$row[user]<br>Image:$row[hasimg]\n";
}
?>
P/S mysql connection established.
If you want to keep your layout fancy way you can use default image.
if($hasimg == 0)
$imgurl = "default.jpg";
echo '<img src="' . $imgurl . '">';
EDIT:
Your PHP code could be like this.
$sql = "SELECT * FROM profile ORDER BY user DSC";
$result = mysql_query($sql)or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Image</th>
</tr>";
while($row = mysql_fetch_array($result))
{
$imgurl = $row['imgurl'];
if($row['hasimg'] == 0)
$imgurl = "default.jpg";
echo "<tr>";
echo "<td>" . $row['user'] . "</td>";
echo "<td><img src=\"" .$imgurl . "'\" /></td>";
echo "</tr>";
}
echo "</table>";
Two solutions:
Set a pathway to default img jpg file as default field content to field imgurl in your database.
Filter query results by adding this code just after $rows[] = $row;:
If ($row['hasimg'] == 0) $row['imgurl'] = 'default.jpg';