Jump to content

Returning a value if blank in mysql with php


Go to solution Solved by cyberRobot,

Recommended Posts

Hello all. I think this one will be very simple for most of you (if not all) but yet I'm missing something here and I can't tell what it is. Any insight will be great as I am ready to wave the white flag on this.
I have tried switching the if and else and that didn't work. I tried to make it an echo statement to test and that didn't work. 

PROBLEM: No matter what I put in the IF first part, it gets ignored and just moves to the ELSE statement. The column "pageimg" in the database is set to NULL, however I tried to make it not null to see if that made a difference and I got the same bad results.

Goal: If the user has an image title in the database column "pageimg" (ex: myimage.jpg) then the background will show that (this part works)
if database column "pageimg" is blank (no entry at all) then it SHOULD show "nouserback.jpg" as the image (this does not work)

Any thoughts?

NOTE: I didn't include below the connection and stuff because that is working fine to get the image and the other functions of the page. This is the only part that is messing with me (below)

Example of what is happening now...

User "Jane" uploads an image for her profile background. 
Jane now has the background image showing ("pageimg")

User "John" doesn't upload an image for his background.
John has a blank white background instead of showing the image "nouserback.jpg"

.... john should have "nouserback.jpg" as a background


 

if (!empty($pageimg)) {
$imagePageURL = 'img/nouserback.jpg';
} else{
$imagePageURL = 'img/'.$row["pageimg"];
}

 

@ginerjm I tried the following (code added below) and what happens is that everyone has "nouserback.jpg" as a background, regardless if they uploaded an image or not
(when they upload an image, then the file name gets entered in the column "pageimg". For some reason this is going to the else statement if there is an image file name in that column or not

So another words

Jane uploads an image, and her image is showing

john does not upload an image so the image file named "nouserback.jpg" should be showing. But it doesn't

 

if (!empty($pageimg)) {
$imagePageURL = 'img/'.$row["pageimg"];
} else{
$imagePageURL = 'img/nouserback.jpg';
}

 

Do you have error checking turned on?

Have you tried using a different image in place of the nouser one?  Just to see what happens.

And if that works, then what does that tell you?

Edited by ginerjm

@ginerjm Yes turned on and no errors. Also, I did try a different image. It's going to the else image url for each time. Also checked the URL of the image on it's own and it's good

@cyberRobot yes it's intentional. One is looking to see if the row is empty. The other is using the row for the file name

 

the column (pageimg) is NULL in the database. Could that be the issue? I DID change it to Not Null once but that didn't make a difference so I put it back to Null
 

Edited by PNewCode
28 minutes ago, PNewCode said:

yes it's intentional. One is looking to see if the row is empty. The other is using the row for the file name

Where do you define the variable $pageimg then?  Why are you using two variables instead of just the row result?

30 minutes ago, PNewCode said:

yes it's intentional. One is looking to see if the row is empty. The other is using the row for the file name

If I'm understanding correctly, $pageimg indicates if the row is empty or not. Then $row["pageimg"] contains the file name, if available. Is that correct?

If so, shouldn't you be testing whether or not $row["pageimg"] contains a value? If it does, use the image name...else, use nouserback.jpg.

@kickenThats not the part that doesn't work. If there is something in the pageimg row like in the screen shot, then it appears in the page. It's definition is in the part of the script that I'm having trouble with.
The part that isn't working, is if it's blank. But it works if something is in that column of the database. Below is the script that defines and uses it.

 

if (!empty($pageimg)) {
$imagePageURL = 'nouserback.jpg';
} else{
$imagePageURL = '/img/'.$row["pageimg"];
}

 

  • Solution

To me it seems like the code should be this. Note that I changed $pageimg to $row["pageimg"].

//IF IMAGE NAME IS PROVIDED, SHOW CUSTOM IMAGE
if (!empty($row["pageimg"])) {
    $imagePageURL = '/img/'.$row["pageimg"];

//ELSE...SHOW FALLBACK IMAGE
} else{
    $imagePageURL = 'nouserback.jpg';    
}

Maybe this will help

The background of the page is getting called from the PHP. Below is that code and the results. Notice that user 2 has no background. It should be using "nouserback.jpg" however if there is one entered, as you can see it works

 

<style>
body {

background-image: url('<?= $imagePageURL; ?>');

background-position: center center;

background-repeat: no-repeat;

background-attachment: fixed;

background-size: cover;

background-color: #000000;
}
</style>

 

dbss2.jpg

@cyberRobot You almost got it. Except with that, it's showing a blank background if the column isn't blank instead of what is entered in the db column :)

 

EDIT... CYBERROOT YOU DID IT!!!

I forgot to put the root back in the path when I used yours (I deleted it for posting in here).... YOU NAILED IT THANK YOU!!!


For my education, can you please tell me the difference of what I had and what you did so I understand it? MANY THANKS!!!

Edited by PNewCode

I believe that's what kicken was trying to help us figure out. Seeing how $pageimg is defined should let us know what the variable contains.

You could add the following debug lines to see what the different variables contain:

echo '<pre>' . print_r($pageimg, true) . '</pre>';
echo '<pre>' . print_r($row["pageimg"], true) . '</pre>';

 

Oh I see. Because they are both looking at the actual row instead of just the name that wasn't defined separately? Am I understanding that right? Sorry @kicken I thought that what I had was defining it all in it's own. Thank you again @cyberRobot and @kicken very much!

3 minutes ago, PNewCode said:

Because they are both looking at the actual row instead of just the name that wasn't defined separately? Am I understanding that right?

Too be honest, I'm still not sure what $pageimg contains. My guess is that it contains the result object that's returned when you run the query. It's difficult to tell without seeing the code.

$row["pageimg"] on the other hand will contain just the image name for whichever row (i.e., $row) is being processed by the loop extracting data from the result object.

@cyberRobot $pageimage is also containing the same as $row["pageimg"]. That row and it's name are serving 2 purposes. Your code got it on point what the goal was to get it done. AND I can use that now to know what to do with other pages now. Thank you again. I appreciate it.

17 minutes ago, PNewCode said:

$pageimage is also containing the same as $row["pageimg"].

Unless you have code somewhere above that conditional that is doing

$pageimg = $row['pageimg'];

then the variable $pageimg is not the same as $row['pageimg'].  That's why I asked to see how it's being defined.  It seems to me like maybe you are expecting that $pageimg just magically exists because it's the name of a column in your DB table, but that's not how things work.  If you don't assign a variable a value at some point, then it doesn't exist. And if it doesn't exist, then using empty() on it will always return true, which is the problem you seemed to be having.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.