Disaster Faster
Disaster Faster

Reputation: 107

PHP mysqli function return value failing

In this chunk of code it was previously designed to use the session_id. I am trying to convert from using the session_id to using a User ID that is retrieved from the database. I'm not sure what I did wrong but the function is not returning the variable. Any suggestions would be appreciated.

 protected function get_user_id() {
        //previous code used the session id
        //@session_start();
        //return session_id();

        // New code to use User ID instead of session_id
        // Connecting to the database
        include ("../../../admin/includes/connect.php");

        // Let's get the user ID from the database for use with the widget
        $user_id_query = "SELECT nonadmin_user_id FROM `nonadmin_user_login` WHERE email = '$_SESSION[email]'";
        $run_query = mysqli_query($conn, $user_id_query);
        while($row=mysqli_fetch_array($run_query)){

        // Create variable for the user's id
        $nonadmin_user_id = $row['nonadmin_user_id']; }
        return $nonadmin_user_id;
    }
    // This function needs to use the variable $nonadmin_user_id
    protected function get_user_path() {
        if ($this->options['user_dirs']) {
            return $this->get_user_id().'/';
        }
        return '';
    }

Upvotes: 1

Views: 203

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

"Fred you're the man! It was the session. I removed the comment out from in front of the session start and now it works perfect. What baffles me on this is I was under the impression that if you start a session in a file and then include other files the included files did not require the session to be started."

The session needs to be started in order for the session array to be recognized and passed successfully in your query.

Plus, session_start(); is required to be resident inside all files using sessions.


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Upvotes: 1

Related Questions