1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Char blank but has db value

Discussion in 'PHP' started by Jeremy Benson, Aug 7, 2015.

  1. #1
    I'm trying to echo out a char to see if a user has uploaded a profile image yet. If there is not profile image yet `image` is set to 'n'. Right now it's echoing empty even through there's an 'n' in the db field. I set the char value or length to 1 when creating the table.

    I haul the info out inside my user class. Strangely other data is coming out from that db call, because I can echo out the username.

    pUser.php

    class pUser{
       
            var $ip;
            var $isGuest;
            var $user = array();
            var $profile = array();
    
    // Set profile info by ID
           
            function set_profile_id($idSet)
            {
               
                global $dsn, $dbUserName, $dbPassword;
               
                $db = new PDO($dsn, $dbUserName, $dbPassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
                    // Set User Table
                $profileStm = $db->prepare('SELECT `ID`, `username`, `displayName`, `about`, `company`, `country`, `status`, `profileViews`,
                `image`, `useDisplayName` FROM `profiles` WHERE `ID` = ?');
               
                try{
                   
                    $profileStm->execute(array($idSet));
                    $this->profile = $profileStm->fetchAll();
                   
                }catch(\PDOException $e){}
               
               
            }
    
    //return profile image name
           
            function return_profile_image()
            {
               
                return $this->profile[0]['image'];
               
            }
    
    //return profile username
           
            function return_profile_username()
            {
               
                return $this->profile[0]['username'];
               
            }
    
    }
    PHP:
    update_profile.php

    require('../../data/sqldata.php');
        require('../classes/pUser.php');
        require('../../lib/chip_upload/pChip.php');
        require('../classes/pUploader.php');
        require('../../lib/zeebra/pZeebra.php');
        session_start();
        // This script will change all the settings of the user.
        // Take out current settings keep them the same if they are blank.
       
        if(isset($_SESSION['ID']))
        {
           
            $user = new pUser;
            $user->set_profile_id($_SESSION['ID']);
            $user->set_user_id($_SESSION['ID']);
           
            $dbImage = $user->return_profile_image();
           
            // Get the new image name.
           
            if(isset($_FILES['images']['name']))
            {
           
                $newImage = $_FILES['images']['name'][0];
               
            }else{
               
                $newImage = '';
            }
           
            echo 'db profile name: ' . $user->return_profile_username();
            echo 'db profile image: ' . $dbImage;
    
    }
    PHP:
    The top echo has a value, but the bottom one doesn't.
     
    Solved! View solution.
    Jeremy Benson, Aug 7, 2015 IP
  2. #2
    Okay, first of all, why are you using fetchAll? Why not just fetch()? Reduces the complexity a little bit, and since you should only ever get one row from the database on this check...?
    Second - var_dump() or print_r() the returned info from the database. Maybe there's an error somewhere.
    Do you have error-reporting in PHP on? You can also try var_dump()-ing the return_profile_image() to see what it is. But most likely there's something wrong somewhere, or something you haven't thought of. You're looking for "n" - does that mean you're using "y" when there IS an image? That's not really wise... just use a boolean value (1 or 0) instead. No need wasting a varchar(1) on that nonsense. I don't really understand why you have that check at all - I'm assuming you have another table for the actual link to the image to be stored in? If so, just join them in the first query, and if there isn't a match, you'll just get an empty value - if so, just check for that.
     
    PoPSiCLe, Aug 7, 2015 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Okay, I gotta rephrase this a bit. I'm not sure why I said I made a char. I guess because I did elsewhere. Actually `image` is a tinytext. It holds 'n' on creation just to remind me there's no image in there yet, so use stock profile image. When the user uploads an image this is going to change to their image name and extension.

    I'm going to update my code in this case, cuz I didn't know fetch would work. I'll come back and let you know what the dumps say... I can't see where an error would be though, since the other data is coming out.

    Just wondering will fetch return one array or something? I thought fetch was for if you were hauling info from just one place, like, say I was just pulling out `username`..

    Thanks.
     
    Jeremy Benson, Aug 7, 2015 IP
  4. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #4
    I think you're right. Since changing everything to fetch it seems to be putting out data for all the fields. Got everything going good.

    hmm, thanks for the new tip. I'll be using fetch from now on. I didn't know it worked like that, haha.
     
    Jeremy Benson, Aug 7, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Fetch() fetches everything, but if you want to loop through several rows, you'll need to loop it. fetchAll() fetches everything into an array, which can work, by all means, but can also result in a very huge array, if there's lots of results. Basically, if you have many rows, a fetch()-loop would be something like this:
    
    if ($stmt->execute()) {
      while ($row =  $stmt->fetch()) {
        //do stuff 
      }
    } else { // do error-handling }
    
    PHP:
     
    PoPSiCLe, Aug 7, 2015 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Uhm... you do know VAR is PHP 4 and shouldn't be used anymore, right? (even threw E_STRICT errors for a while). You should be saying "public", not VAR. (or even better PRIVATE!)

    When it comes to properties you probably also shouldn't be wasting time repeating that over and over again either.

    That you then have the DB connection info global in scope (security? what's that?!?), and as mentioned fetch_all would return multiple rows for something that shouldn't have multiple rows. (particularly if the ID is unique)

    ...But even more confusing is I'm not seeing any code that seems to have ANYTHING to do with what you are talking about with some "char" somewhere.
     
    deathshadow, Aug 7, 2015 IP
  7. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #7
    I'm used to this from other languages, but wasn't aware it was implemented into PHP. I'll definitely change this, thanks.

    I'm not sure how else to do this. This stems back to a problem I was having a long time ago. I wasn't sure how to use my db connection inside a class. The solution ended up being what you see there, to access the connection variables. What's the proper way?

    It wasn't necessary. I explained exactly what was happening. I was pulling `image` out of the db and it should have a value of 'n' or whatever I said, but it was echoing out blank. All the code is there...

    Pulling it out here

    
    $profileStm = $db->prepare('SELECT `ID`, `username`, `displayName`, `about`, `company`, `country`, `status`, `profileViews`,
    `image`, `useDisplayName` FROM `profiles` WHERE `ID` = ?');
    
    PHP:
    ecoing it here

    
    echo 'db profile name: ' . $user->return_profile_username();
    echo 'db profile image: ' . $dbImage;
    
    PHP:
    I've solved this problem anyway, script works fine now. I would be interesting in hearing more about the db issue though.
     
    Jeremy Benson, Aug 9, 2015 IP