I'm trying to complete an img tag src using a filename stored in a mysql database.
At the moment I have to load two images from this one MySQL query (They are chosen at random) moving forward however I will likely need to do more.
This is my current query:
// Get random 2
$query="SELECT * FROM images ORDER BY RAND() LIMIT 0,2";
$result = $conn->query($query);
while($row = mysqli_fetch_object($result)) {
$images[] = (object) $row;
}
My connection documentation is stored in a separate file and called earlier in the process (This has worked for other functions so don't think the issue is there)
I then try to insert the image address later on like this:
<img src="images/<?=$images[0]->filename?>" style="width:100%">
<img src="images/<?=$images[1]->filename?>" style="width:100%">
I've been staring at this and trying stuff for the best part of an hour so it's likely it's either an obvious and stupid problem or I've gone completely off track!
Thanks in advance!
It might be easier to use mysqli_fetch_assoc() instead of mysqli_fetch_object().
$query = "SELECT * FROM images ORDER BY RAND() LIMIT 0,2";
$result = $conn->query($query);
while ($row = mysqli_fetch_assoc($result)) {
$images[] = $row;
}
But, that inline PHP does not look reliable at all. You will want to do it like this.
<img src="images/<?php echo $images[0]["filename"]; ?>" style="width:100%" />
<img src="images/<?php echo $images[1]["filename"]; ?>" style="width:100%" />
Hope this helps.
Try this:
<img src="/images/<?php echo $images[0]->filename; ?>" style="width:100%">
<img src="/images/<?php echo $images[1]->filename; ?>" style="width:100%">
Related
I wanted to retrieve machine image pass by id. But issue is that i would be getting all machine images instead of particular one. So please guide me what's wrong i have done.
<?php
$query1=mysqli_query($con,"select * from photo where m_id = $m_id");
while($row=mysqli_fetch_array($query1)){
?>
<img src="<?php echo $row['location']; ?>" height="150px;" width="150px;">
<?php
}
?>
You should filter data using WHERE to fetch only certain rows.
Just Add:
$m_id = $_POST['m_id'];
$query1=mysqli_query($con,"select * from photo where m_id = $m_id ");
hope this help
This is the code which selects from DB and sets the image tag.
<div>
<?php $query = mysql_query("SELECT * FROM company where sn='1'");
while($rows = mysql_fetch_assoc($query)){
$logo = $rows['logo'];
$password = $rows['password'];
$phone = $rows['phone'];
}
?>
<img src="<?php echo $logo ?>"/>
</div>
When we get this and set on textarea then we want this query{which save in db} executed. and output show only Logo name.
But this time this show full query which save in db.
we want get this output on textarea:
<div><img src="logoname"/></div>
You are using mysql extension, which is deprecated. You should use mysqli instead.
The loop overwrites your variables ($logo, $password, $phone) in every iteration, so it makes no sense until you're fetching single row.
But if you're fetching single row, then you don't need a loop:
<?php
if ($r = mysqli_query($connection, "SELECT * FROM company WHERE sn = 1")) {
$company = mysqli_num_rows($r) ? mysqli_fetch_row($result)[0] : null;
mysqli_free_result($r);
}
?>
<img src="<?php echo empty($company) ? 'nophoto.png' : $company['logo']; ?>" />
Replace
SELECT * FROM company where sn='1'
With
SELECT * FROM company WHERE sn=1
If you take out the apostrophes, that might solve your problem since the value stored in your database is most likely not a string. Also you should have WHERE in capital letters.
Let me know if that answered your question! :)
I have this:
<a href="">
<img class="img" src="
<?php $query = mysql_query("
SELECT * FROM posts WHERE ID = 49");
while($row = mysql_fetch_array( $query ))
{ echo $row['Image1(170x170)']; }
?>" width="180px" height="130px">
</a>
I want to echo an image path where I have the echo that I have stored the path into a row in a database... In generally I have stored the image path like this: ../folder/folder/file.jpg for another reason that I cant changed it and now I want to pull that from the database and echo it here but I want to change and done like this from
../folder/folder/file.jpg to folder/folder/folder/file.jpg
Is there a way to do that?
Because I have searched for a lot of time and I have only find the REPLACE() that I don't want to use because I don't want to change my records in my database. Any help would be appreciated! Thanks in advance!
You can do it in the PHP code:Remove the .. from the beginning of the string using substr and add the "folder" instead:
echo "folder" . substr($row['Image1(170x170)'],2);
I want to change and done like this from ../folder/folder/file.jpg to folder/folder/folder/file.jpg
The basic idea here is to get your image src link, and trim the first two .. characters. This can be done in many ways. I've just str_replace() in the below code:
$query = mysql_query("SELECT * FROM posts WHERE ID = 49");
while($row = mysql_fetch_array( $query ))
{
$src = $row['Image1(170x170)'];
$src = str_replace('..', '', $src);
?>
<a href=""><img class="img" src="<?php echo $src; ?>" width="180px" height="130px"/>
<?php
}
Basically I have a query called using PHP:
<?php $result = mssql_query("SELECT * FROM Colours WHERE Type = 'type1' ");
while ($row = mssql_fetch_array($result)) {
if ($row['ColourID'] == "1") {
$sayclass1="imgactive";
}else{
$sayclass1="imginactive"; }
?>
As you can see once I execute the query I then loop it, the problem is that it returns an array, now in some instances I need to use the full array, but I would like to be able to select one entry from it for if statements and such. For example:
<img id="h" src="<?php echo $row['thumbimg']; ?>
now thumbimg is a column in my DB, and it just holds a url. However due to the fact its an array the picture doesn't display because its echoing all the values, so instead of images/image1.png for example it is echoing images/image1.png images/image2.png images/image3.png etc etc...
I hope that makes sense, and can anyone tell me how to manipulate the query/code slight to still return all the entries but to select certain values from the arrays please?
You need to use the img tag with in for each if you want to show all the images
foreach($row[thumbimg] as $img):
<img id="h" src="<?php echo $img; ?>
end foreach;
<?php $result = mssql_query("SELECT * FROM Colours WHERE Type = 'type1' ");
while ($row = mssql_fetch_array($result)) {
if ($row['ColourID'] == "1") {
$sayclass1="imgactive";
}else{
$sayclass1="imginactive";
}
echo '<img id="h" src="'.$row['thumbimg'].'">';
}
?>
Using this above code, you will get one image per line from the database. The part of code you have copy-pasted is not enough to determine where your mistake is and why $row['thumbimg'] contains a concatenated value of the result.
the problem I am having I will describe it as best as I can....The user chooses three selections and the image appears on the screen, and the selections they made are sent and stored in the database, the PHP function below then finds the image for the selection and returns them as variables so the latest selection they made and the images corresponding to the selection is still displayed in the placeholders after the page is refreshed, the SQL code I have below I want to incorporate into one Query but I need it so where the data for $driver1 is found it is placed into the $image1 variable and the same for $driver2 into $image2, and $driver3 to $img3 so I can return them to the page and echo the correct images in the correct placeholders. The images for the selections they made need to appear in the placeholders that they originally appeared in. It works fine as it is now, but I am sure that the SQL querys can be shortened to one? to save lines of code.
The code I have posted below is the main important bits so you can hopefully understand what I am asking.
Any help would be great, thank you.
//HTML CODE
<?php
list($img1, $img2, $img3) = checkteam();
?>
<img onclick="return removedriver1(this)" id="advert" src="images/delete.gif" border="0"/>
<img id="placeholder1" src="<?php echo "$img1";?>" alt="" />
<img onclick="return removedriver2(this)" id="advert src="images/delete.gif" border="0"/>
<img id="placeholder2" src="<?php echo "$img2";?>" alt="" />
<img onclick="return removedriver3(this)" id="advert" src="images/delete.gif" border="0"/>
<img id="placeholder3" src="<?php echo "$img3";?>" alt="" />
//PHP CODE
function checkteam(){
$sql="SELECT image FROM drivers WHERE drivers_id = '$driver1'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$image1=$row[image];
$sql="SELECT image FROM drivers WHERE drivers_id = '$driver2'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$image2=$row[image];
$sql="SELECT image FROM drivers WHERE drivers_id = '$driver3'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$image3=$row[image];
return array ($image1, $image2, $image3);
}
The best way to do this in my opinion is to stick them all into an associative array:
$sql = "SELECT drivers_id, image FROM drivers WHERE drivers_id IN ($driver1, $driver2, $driver3)";
$result = mysql_query($sql);
$images = array();
while (($row = mysql_fetch_assoc($result)) {
$images[$row["drivers_id"]] = $row["image"];
}
You can then print out the appropriate image with $images[$driver1] etc.
You could do one query only:
$sql = "SELECT drivers_id id, image FROM drivers WHERE drivers_id in ($driver1, $driver2, $driver3)";
You would need to myqsl_fetch_array three times, of course.
while ($row = mysql_fetch){
if ($driver1 == $row['id'])
$image1 = $row['image'];
else if ($driver2 == $row['id'])
$image2 = $row['image'];
else if ($driver3 == $row['id'])
$image3 = $row['image'];
}
select drivers_id, image from drivers where drivers_id in ($driver1, $driver2, $driver3)
or
select drivers_id, image from drivers where drivers_id = $driver1 or drivers_id = $driver2 or drivers_id = $driver3
loop through the results and match drivers_id to echo the correct image in the correct placeholder.