Checking if variable is empty - php

First I will paste my code than explain.
while($row = mysqli_fetch_assoc($result)) {
$id=$row['id'];
$username=$row['username'];
$email=$row['email'];
$firstname=$row['firstname'];
$lastname=$row['lastname'];
$motto=$row['motto'];
$bio=$row['bio'];
$result4 = mysqli_query($link, "SELECT * FROM photo where id='$id'");
while($row4 = mysqli_fetch_assoc($result4))
{
$image=$row4['filename'];
$src = (empty($image)) ? "upload/your-photo.jpg" : "site_images/$id/$image";
}
As you can tell the problem has to do with the image. I'm thinking the problem is where it SELECTS * FROM photo where id='$id' because this is in a while loop and if it is empty like it says in $src than it just moves on to the next person in line.
I was wondering how it would be possible to make this work? Right now it is just displaying the same image for every id when it should show the correct image.

You should just have one query with a join in it, to join the photo table.

I would try breaking out the conditional where you have the inner loop:
$result4 = mysqli_query($link, "SELECT * FROM photo where id='$id'");
while($row4 = mysqli_fetch_assoc($result4))
{
$image=$row4['filename'];
$src = "upload/your-photo.jpg";
if(!empty($image)) {
$src = "site_images/$id/$image";
}
}
If that doesn't work, try using:
strlen
and checking if it's equal to 0
Also, in your database, does your photo table have rows with with IDs but no photo file specified? Perhaps you are querying an empty table..

Related

Delete file from folder and mysql joined table

I have a file upload system where I can delete from MySQL DB and from the folder.
The only problem is that all the files in the folder are deleted instead of just the single one. Each folder is a custom folder with a person's name.
I have a joined table where the id's of two tables are equal. I need the joined tabled to receive the name of the person to know the file location.
This is my working code so far where multiple files are deleted instead of the one I selected:
$analyse = mysqli_query($conn, "SELECT *
FROM analyse
INNER JOIN persons ON
analyse.id_person = persons.id");
while ($row = mysqli_fetch_array($analyse)) {
$img = $row['img_name'];
$name= $row['name'];
$frontname= $row['frontname'];
$image_url = "../persons/{$name} {$frontname}/analyse/{$img}";
$result = mysqli_query($conn, "DELETE FROM analyse WHERE id=$id");
unlink($image_url);
if($result){
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
echo "Failed to delete";
}
}
For example: Person X with id 15 has three images with different ids but each time with the ID(15) of the person. I want to be able to delete the single selected file instead of all the files with id 15.
UPDATE
I found the solution, the ID that came through for deleting was always the first in my DB row. The fetching of my array was wrong and my ID consequently too.
Thanks for the answers.
Try using Limit 1 with your delete Query.
$analyse = mysqli_query($conn, "SELECT *
FROM analyse
INNER JOIN persons ON analyse.id_person = persons.id");
while ($row = mysqli_fetch_array($analyse)) {
$img = $row['img_name'];
$name= $row['name'];
$frontname= $row['frontname'];
$image_url = "../persons/{$name} {$frontname}/analyse/{$img}";
$result = mysqli_query($conn, "DELETE FROM analyse WHERE id=$id LIMIT 1");
unlink($image_url);
if($result){
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
echo "Failed to delete";
}
}

How do i navigate through images stored in mysql rather than displaying all of them?

This is my code but it displays all of the images saved in the database, i just want one to be displayed and the rest by using next and previous buttons. any help would be appreciated. Thanks!
<?php
$sql = "SELECT * FROM images";
$result = mysqli_query($conn, $sql) or die("bad request: $sql");
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
if($i%3 == 0)
{
echo "<tr>";
}
echo"<td><img src='user_data/{$row['FILE_NAME']}' width=200 height=200></td>";
if($i%3 == 2) {
echo"</tr>";
}
}
?>
You should use LIMIT and OFFSET in your query. It would look like this:
$sql = "SELECT file_name FROM images LIMIT 0,1";
This will get you 1 record, starting at record 0. So from that query you can build your pagination script. Just keep adding one to the offset (the first number in the LIMIT clause). Remember to filter the page number (make sure it’s an int).

How to run query until one record is found?

This is what i am trying right now but no luck
$bid = $next - 2;//This subtracts 2 from the number, this number is also auto generated
$preid = $bid;
$query = "SELECT * FROM images where imageid = '$preid'";
$sql = mysqli_query($conn,$query) or die(mysqli_error($conn));
while(mysqli_num_rows($sql) !=0) {
$select_query = "SELECT * FROM images where imageid = '$preid'";
$sql = mysqli_query($conn,$select_query) or die(mysqli_error($conn));
--$preid;
}
whats suppose to happen is that if a record does not exist it subtracts 1 from preid and runs the query again with the new preid and keeps happening until a record it found but cant figure out how to do it.
I am assuming that you are constantly checking database for new values. However, on a large scale application thi is an highly inefficient way to constantly ping the database.
You have made a variable $preid but you are not using it anywhere.
This is how i would do it if i were to go according to your way
$bid = $next - 2;//This subtracts 2 from the number, this number is also auto generated
$preid = $bid;
$query = "SELECT * FROM images where imageid = '$preid'";
$sql = mysqli_query($conn,$query) or die(mysqli_error($conn));
while(mysqli_num_rows($sql) !=0 || !$preid) { //notice here i added the condition for preid.
$select_query = "SELECT * FROM images where imageid = '$preid'";
$sql = mysqli_query($conn,$select_query) or die(mysqli_error($conn));
--$preid;
}
now what happens is that the loop will run as long as either of the two condition stays true ie either a row is returned from the database or it will keep searching until preid is not 0.
If you want to test for an empty set, your while should run while mysqli_num_rows == 0
while(mysqli_num_rows($sql) == 0) {
$select_query = "SELECT * FROM images where imageid = '$preid'";
$sql = mysqli_query($conn,$select_query) or die(mysqli_error($conn));
$preid--;
}
As #DarkBee has mentionend in his comment, this code is highly vulnerable for an infinite loop which will take down your script, as soon as there are no entries for anything.

obtaining comments for each post

hi I have to create a system of comments within various trhead I performed before and all the while the trhead 'while inside the comment and it works but is really slow and with a large number of threads often gives me timeout error how can I fix the problem?
function commenti($id) {
$query2 = "SELECT * FROM table2 WHERE numid='$id' ORDER BY id ASC";
$result2 = mysqli_query($conn,$query2);
if($result2->num_rows >0)
{
while($row2 = $result2->fetch_array(MYSQLI_ASSOC))
{
$idt2 = $row2['id'];
$testot2 = $row2['testo'];
return $testot2;
}
} else {
echo "No comment";
}
}
$query = "SELECT * FROM table1 where visualizza='1' ORDER BY id DESC";
$result = mysqli_query($conn,$query);
if($result->num_rows >0)
{
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
$id = $row['id'];
$titolo = $row['titolo'];
$testo = commenti($id);
echo "$titolo $testo <br>";
}
}
mysqli_close($conn);
?>
I thought to use the join but if there are more duplicates also post comments
$query = "SELECT * FROM table1 left JOIN table2 ON table1.id = table2.numid where visualizza='1' ORDER BY id DESC";
I'm going to assume that you are trying to pull a ton of records. The best way to approach this is to add pagination and only load ~10-20 comments per page. depending on your server
Update:
#OP Basically on first load of the page you load ~10 comments, once they click view more then you load in the next few using ajax. Rinse and repeat.

Simple mysql Query to check if row exist

I want to show user if he liked a image or not..
for that I am creating php code
$userid=$_COOKIE['userid'];
$sql = "SELECT * FROM likes WHERE `user_id`='{$userid}'";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($query);
if($row){
echo "unlike";
}
else{
echo "like";
}
I can not do this for everything like 'tags', 'shares', 'comments', 'favourites' ...many
Isn't there anything simpler than this...?
Like say $row_check=mysqli_check_exist($table,$column_name,$userid);
use mysql fetch row method
$num_row = mysqli_num_rows($query);
if($num_row>0)
{
//add your code
}
else
{
//add your code
}
There are a lot of ways of doing this really but if you arnt going to use any more information then weither or not the user has liked it doing select * is a bad idea. The reason why is that you are asking the database to return the value of every column in that table.
Assuming its a small database its probably not a problem no but as your database gets bigger you are puting more load on it then you need you should try and only select the columns you need and intend to use. Ok in this case the userid is probably indexed and its only one row, but if you get in the habit of doing it here you may do it else where as well.
try this instead.
$userid=$_COOKIE['userid'];
$sql = "SELECT count(user_id) as total FROM likes WHERE `user_id`='{$userid}'";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($query);
if( $row ['total'] > 0){
echo "unlike";
}
else{
echo "like";
}
This way we are just getting the total. simple and elegant
Use mysqli_num_rows($query) if > 0 exist
You simply need to count the available records using
mysqli_num_rows($query);
This will return a number (count) of available records
So simple put a check like this :
$userid=$_COOKIE['userid'];
$sql = "SELECT * FROM likes WHERE `user_id`='{$userid}'";
$query = mysqli_query($conn, $sql);
$count = mysqli_num_rows($query);
if($count>0){
echo "unlike";
}
else{
echo "like";
}

Categories