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:
- In my localhost this is not happening.
- 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
getGenremay or may not return a result. It’s unclear what a call toimplodewill do whengetGenrereturns nothing. The warning would suggest to me you’re not getting any genres back.Using a contrived example I can replicate your warning:
Yields the warning
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
$stmtrather 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
$stmtyou 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();.