简体   繁体   中英

PHP/MySQL strange problem affecting fetch from database only when user logged in

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?

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(); .

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM