views:

85

answers:

2

So I have this function that gets data from database and echoes it. The function takes from the database the article id, article title, and some other data..

When the user is not logged in, the function works good and shows all the data, but when a user is logged in, suddenly only the article title is fetched.. All the data is in the same database and even in the same table but only the article id is fetched!!

Notes:

  1. In my localhost this is not happening.
  2. My host is ipage.com and in order to get php sessions to work I need to add session_save_path(//path) before session_start() (I don't know if this has something to do with the problem)

Update:

This is the error: Warning: implode() [function.implode]: Invalid arguments passed in /path

This is the function:

function getNowPlaying($stmt) {

    $sql = 'SELECT movies.imdbID, movies.title FROM movies ORDER BY Rand() LIMIT 15';
    if ($stmt->prepare($sql)) {
        $stmt->bind_result($imdbID, $title);
        $stmt->execute();
        $i = 0;
        while ($stmt->fetch()) {
            $data[$i]["imdb"] = zeroFill($imdbID);
            $data[$i]["title"] = $title;
            $i++;
        }
    }

    for ($i = 0; $i < count($data); $i++) {

        $genres = getGenre($stmt, $data[$i]["imdb"]);
        $data[$i]["genre"] = implode(', ', $genres);

        $data[$i]["poster"] = getPoster($stmt, $data[$i]["imdb"]);
    }

    return $data;
}

function getGenre ($stmt, $id, $db = 'main') {

    if ($db === 'main') {
    $sql = 'SELECT sys_genres.genre FROM sys_genres, movie_genres WHERE sys_genres.genreID = movie_genres.genreID AND movie_genres.imdbID = ?
        ORDER BY movie_genres.genreORDER';
    }

    else if ($db === 'inp') {
    $sql = 'SELECT sys_genres.genre FROM sys_genres, inp_movie_genres WHERE sys_genres.genreID = inp_movie_genres.genreID AND inp_movie_genres.imdbID = ?
        ORDER BY inp_movie_genres.genreORDER';
    }

    if ($stmt->prepare($sql)) {
    $stmt->bind_param('i', $id);
    $stmt->bind_result($genres);
    $stmt->execute();

    while ($stmt->fetch()) {
        $data[] = $genres;
    }
    }

    if (!empty($data)) {
    return $data;
    }
}

The Session array when user is logged in:

Array
(
    [xsrf_token] => 13721578024c33e20b2940d3.39161731
    [username] => jonagoldman
    [userID] => 24
    [start] => 1278468629
)

Update 2:

This is the part that is causing problems:

In index.php I have this:

if (isset($_SESSION['userID'])) {
    $user_points = getUserPoints($stmt, $_SESSION['userID']);
}


function getUserPoints($stmt, $userid) {

    $sql = 'SELECT points FROM user_points WHERE userID = ? LIMIT 1';

    if ($stmt->prepare($sql)) {
        $stmt->bind_param('i', $userid);
        $stmt->bind_result($data);
        $stmt->execute();
        $stmt->fetch();
    }

    if (!empty($data)) {
        return $data;
    }

}

That part of code is causing the problem when the user log in.. Any ideas?

+1  A: 

Nothing in your code suggests anything session related, but note that getGenre may or may not return a result. It's unclear what a call to implode will do when getGenre returns nothing. The warning would suggest to me you're not getting any genres back.

Using a contrived example I can replicate your warning:

function foo(){ }
$bar = foo();
echo implode(', ', $bar);

Yields the warning

Warning: implode(): Invalid arguments passed in...

This would lead me to believe that your problem is occurring further up the chain...that is you're observing a symptom rather than a problem.

Evidently the core issue is that you're passing around $stmt rather than the connection itself. The problems you're seeing stem from code that only runs when a user is logged in. That doesn't mean there's a problem with your login scheme, and it's wholly unrelated to sessions. The core issue is that when a user is logged in you're doing something you don't normally do with $stmt, in effect corrupting it.

Everywhere you pass around $stmt you should be passing around your connection $conn (from your comments above I know this is $conn). Then anywhere you need a statement get one from the connection by running $stmt = $conn->stmt_init();.

Mark E
@Mark: I know where the problem is now, but not how to fix it... Take a look at the bottom of the updated post!!!
Jonathan
PROBLEM SOLVED... I moved the getUserPoints function call from index.php to header.php. It fixed the problem dont know why,,,
Jonathan
@Jonathan, stop passing around `$stmt`, it's getting modified everywhere you pass it and is in a sense corrupt. Instead pass around the connection and get a new "statement" anytime you need one. This has *nothing* to do with your use of sessions, and *everything* to do with logic executed when a user is logged in.
Mark E
@Mark: I think you are right and that is the problem. Can you explain me in more how to pass the database connection instead of $stmt??
Jonathan
@Jonathan, edited my answer.
Mark E
A: 

I can't see why this wouldn't work when a user is logged in, but there are some issues in the code. See my comments:

function getNowPlaying($stmt) {

    $sql = 'SELECT movies.imdbID, movies.title FROM movies ORDER BY Rand() LIMIT 15';
    if ($stmt->prepare($sql)) {
        $stmt->bind_result($imdbID, $title);
        $stmt->execute();
        $i = 0;
        while ($stmt->fetch()) {
            // BAD place to declare $data...what happens if this is **never** executed?
            // $data should be declared outside of the conditional/loop, since you go on to use it after the conditional/loop
            $data[$i]["imdb"] = zeroFill($imdbID);
            $data[$i]["title"] = $title;
            $i++;
        }
    }

    // you should just store count($data) in variable before the for loop, instead of calling count on every iteration of the loop, the size of the array shouldn't change
    for ($i = 0; $i < count($data); $i++) {

        $genres = getGenre($stmt, $data[$i]["imdb"]);
        $data[$i]["genre"] = implode(', ', $genres);
        $data[$i]["poster"] = getPoster($stmt, $data[$i]["imdb"]);
    }

    return $data;
}

function getGenre ($stmt, $id, $db = 'main') {

    // what happens if $db is neither 'main' nor 'inp'?
    if ($db === 'main') {
    $sql = 'SELECT sys_genres.genre FROM sys_genres, movie_genres WHERE sys_genres.genreID = movie_genres.genreID AND movie_genres.imdbID = ?
        ORDER BY movie_genres.genreORDER';
    }

    else if ($db === 'inp') {
    $sql = 'SELECT sys_genres.genre FROM sys_genres, inp_movie_genres WHERE sys_genres.genreID = inp_movie_genres.genreID AND inp_movie_genres.imdbID = ?
        ORDER BY inp_movie_genres.genreORDER';
    }

    if ($stmt->prepare($sql)) {
    $stmt->bind_param('i', $id);
    $stmt->bind_result($genres);
    $stmt->execute();

    while ($stmt->fetch()) {
        // again, declaring variables inside conditionals/loops is bad if you're using those variables outside of the conditional/loop
        $data[] = $genres;
    }
    }

    // and if $data is empty? then what?
    // specific to that error, if you don't return an array, implode() fails
    if (!empty($data)) {
    return $data;
    }
}
George Marian
Yes, I know that the error handling in those functions is not perfect. But I can't understand how user log in ($_SESSION) can affect this function at all...
Jonathan
@Jonathan At the very least, I would shore up the error handling in getGenre(), as that's where the issue is showing itself. As mentioned by Mark, this is a symptom of the problem, not necessarily the problem itself. You need to determine why you're not getting any results from the database in getGenre(). The function implode() is likely failing, because getGenre() is returning NULL (the default return value), since $data is created as an array inside that while loop or not at all. It's possible that $stmt->fetch() is failing, or maybe $stmt->prepare($sql). Continued...
George Marian
@Jonathan Basically, filling some of these holes should make it easier to troubleshoot the problem. Focus on getGenre() first. What other errors are you getting? I have a feeling that there should be some database related errors in getGenre().
George Marian
@George: I know where the problem is now, but not how to fix it... Take a look at the bottom of the updated post!!!
Jonathan
@everyone: PROBLEM SOLVED... I moved the getUserPoints function call from index.php to header.php. It fixed the problem dont know why,,,
Jonathan