MYSQL query optimisation - php

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.

Related

Call an image address that is stored in a MySQL database

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%">

image display from database in php

i want to display image from database and below is my code where empid is the value of textbox, it's showing the image name stored in database but i am not able to echo this picture. anyone 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 $rows['image'];
}
?>
Change this line.
$result = $db->prepare("SELECT image FROM info WHERE empid= '". $empid ."'");
its only show you the filename that you stored in your db . if you want to show image then first add folder location with image name and pass into the <img src="excatImageLocation">
Example -
$yourExactPath = "YourImageUploadFolderLocation".$rows['image']; // img/yourFilename.jpg;
echo "<img src='$yourExactPath' />
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');
$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)){
echo $row['image'];
}
?>

retrieve image from database using mysqli php 7

I am trying to retrieve a png image file from the database
Here is the call from within the <img> tag inside body:
<img src="..\BankLogin\man.php?id=2" style="width:128px;height:150px">
Here's the man.php file:
<?php
$link = mysqli_connect("localhost","root","","images");
$imgId = $_GET['id'];
if (!empty($imgId)) {
$sqliCommand = "SELECT image FROM images WHERE id = $imgId";
$result = mysqli_query($sqliCommand,$link);
$row = mysqli_fetch_assoc($result);
mysqli_close($link);
header("Content-type: image/png");
echo $row['image'];
}
?>
On running the code i just get an image frame with an 'unloaded image'(am i saying it correct?).I am pretty sure something is wrong in the man.php file, maybe in echo $row['image']. I am not sure how to go about making it right. Any help with this would be great.
The function mysqli_close should be called after the image data is echoed. This is because it destroys the result sets.
Also please fix the SQL Injection vulnerability:
$imgId = (int)$_GET['id'];
If you want to retrieve the image which is stored as BLOB type in phpmyadmin you have to echo it as follows.
echo '<img src="data:image/jpeg;base64,'.base64_encode( $rows['image'] ).'"/>'
Example:
To Retrieve the BLOB image from the DB you have to do like this.
<?php
$db = mysqli_connect("localhost","root","","dbname"); //keep your db name
$query = "SELECT * FROM image WHERE id = $id";
$sth = $db->query($query);
$fetch=$sth->fetch_assoc();
echo '<img src="data:image/jpeg;base64,'.base64_encode( $fetch['image'] ).'"/>';
?>
For Inserting the image you need to follow the procedure like this So that if you encode it as base 64 you can retrieve the image perfectly without any error.
<?php
$conn = mysqli_connect("localhost","root","","DbName"); //keep your db name
$single_image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
//U have to keep your DB table column name for insertion. I keep image type Blob.
$query = "INSERT INTO image (image) VALUES('".$single_image."')";
$SQL = mysqli_query($conn, $query);
?>

how to retrive php query from db and set on textarea with executed?

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! :)

Displaying Image from image uploader

im trying to display image from database by calling filename of the image.
here's my code.
$fname=$row['user'];
$query1=mysql_query("select * from user where username = '$fname'");
$q_pix =mysql_fetch_array($query1);
$id=$row['id'];
$date=$row['date'];
$message=$row['message'];
$lugar=$row['location'];
<img src=".$row['location']." height=\"120\" width=\"120\">
Undefined index: location.
First of all print both the variables $row and $q_pix and check in which you have your location field.
Try to use $q_pix variable in img src if it has -
<img src=".$q_pix['location']." height=\"120\" width=\"120\">
<?php
$fname = $row['user'];
$query1 = mysql_query("select * from user where username = '$fname'");
$q_pix = mysql_fetch_array($query1);
?>
Here you are getting values from database to the variable $q_pix, not to $row.
So retrieve values from $q_pix instead of $row. (Print both $q_pix and $row to make sure they are getting values).
<?php
$id = $q_pix['id'];
$date = $q_pix['date'];
$message = $q_pix['message'];
$lugar = $q_pix['location'];
?>
<img src="<?php echo $q_pix['location']; ?>" height=\"120\" width=\"120\">
NOTE : Make sure your $q_pix['location'] value includes the directory stucture too with the image name.

Categories