Jump to content

A very strange problem


kayes

Recommended Posts

Hi. This is my first post to this forum and I'm also new to PHP. Now I'm trying to view some movie information from database. I know this can be done very straight forward way. But the way I'm trying to do it, I think, should have worked. Ok, the problem is the information is viewed the first time I enter the page. But if I refresh the page, everything's gone. But once I clear the browser's cache, authenticated sessions, the information is viewed again. And then after refreshing, it's again gone.

Here's how I'm doing it:
I have a class called 'Movie'. I have another class called 'GetMovies' which has a function called 'fetchMovies()' that gets the information about movies from database, creates a Movie object for each movie and stores these Movie objects in an array and returns this array.
Now the main page which is 'index.php' is first checking if the Movie objects array is saved in Session. If it exists in the session it just gets the Movie objects from the array and uses them to view the info. But if it doesn't, it creates an object of 'GetMovies' class and calls the 'fetchMovies()' function on this object which, like I said above, returns the array containing the Movie objects. And then this array is saved to Session so it can be accessed from anywhere in the application.

But what I'm experiencing is it works only the first time I run the app. But after refreshing the page it just doesn't work (shouldn't the array be accessed from Session after refreshing?). I can see that the array exists in Session and it contains the Movie objects. But there are not properties remaining set in these objects. So no information is viewed. I just can't understand why this is happening. Can anyone give me some lights here? Here are my codes:

[b]DBConnection.php[/b] (located at D:\wamp\www\test\classes)
[code]<?php
class DBConnection
{
var $mysqlLinkIdentifier=false;
var $dbSelected=false;

function DBConnection()
{
$this->mysqlLinkIdentifier=mysql_connect(DATABASE_SERVER, DATABASE_USER, DATABASE_PASS) or die("Could not connect to MySQL!");
$this->dbSelected=mysql_select_db(DATABASE_NAME) or die("Could not connect to database!");
}

function getConnectionObject()
{
return $this->mysqlLinkIdentifier;
}

function isDBSelected()
{
return $this->dbSelected;
}
}
?>[/code]

[b]GetMovies.php[/b] (located at D:\wamp\www\test\classes)
[code]<?php
class GetMovies
{
function GetMovies()
{
new DBConnection();
}

function fetchMovies()
{
$movies=array();
$resource=mysql_query("select str.movieid, str.movienameabbr, str.moviename, str.downloadlink, strcat.cat_name, strinfo.hits from movies str, moviecats strcat, movieinfo strinfo where strinfo.movieid=str.movieid and strcat.cat_id=str.category");
while($row=mysql_fetch_array($resource, MYSQL_ASSOC))
{
$movie=new Movie();
$movie->movieId=$row["movieid"];
$movie->movieName=$row["moviename"];
$movie->downloadLink=$row["downloadlink"];
$movie->category=$row["cat_name"];
$movie->hits=$row["hits"];
$movies[]=$movie;
}
return $movies;
}
}
?>[/code]

[b]Movie.php[/b] (located at D:\wamp\www\test\classes)
[code]<?php
class Movie
{
var $movieId;
var $movieName;
var $downloadLink;
var $category;
var $hits;

function Movie()
{

}
}
?>[/code]

[b]config.php[/b] (located at D:\wamp\www\test)
[code]<?php
session_start();

define('DATABASE_SERVER','localhost');
define('DATABASE_PORT',3306);
define('DATABASE_NAME','mydb');
define('DATABASE_USER','root');
define('DATABASE_PASS','');

require_once("classes/DBConnection.php");
require_once("classes/Movie.php");
require_once("classes/GetMovies.php");
?>[/code]

[b]index.php[/b] (located at D:\wamp\www\test)
[code]<?php
require_once("config.php");
if(!isset($_SESSION["movies"]))
{
$gm=new GetMovies();
$movies=$gm->fetchMovies();
$_SESSION["movies"]=$movies;
}
$movies=$_SESSION["movies"];
foreach($movies as $movie)
{
echo $movie->movieName."<br />";
echo $movie->downloadLink."<br />";
echo $movie->category."<br />";
echo $movie->hits."<br />";
echo "-------------------------------------------------------<br />";
}
?>[/code]
Link to comment
Share on other sites

Out of intrest why are you storing all of your data in a session?  MySQL can should be able to cope with regular re-polls of the database unless you have a seriously high number of conections, inwhich case it may be worth thinking of a diffrend db like postgres?

I ask because it seems to me you are storing all the infomation in the session, when it is unlikly that you will need to call on all of it.  Leaving it all in the database untill you actualy need it would also make your coding much simpler.

For example:

To connect to the database needs only be:
[code]
$user = "USERNAME";
$pass = "PASSWORD";
$host = "IP ADDRESS";
$db = "DATABASE";
$link = mysql_connect( $host, $user, $pass );

if (!$mysql_connection)
          {
          die("Could not open connection to database server $host");
          }[/code]

In reality your preforming a lot of checks, every time, that are not realy nessesary.  Your either connected or not.  They can be usefull for troubleshooting, but just pad out your real code.

Then to query the database:

[code]
include "FILE WITH CONNECTION DETAILS.php";
global $mysql_connection;

$query = "SELECT author,movie_length FROM move_table_name WHERE title LIKE 'saw1'";

$result = mysql_query($mysql_connection, $query);
$data = mysql_fetch_array($result);
       
[/code]

Then you can use the array as you wish:
[code]$text = $data["WHICH_EVER_COLOUB_YOU_POLED_FOR"];

or

$text2 = $data["THE_OTHER_COLOUB_YOU_POLED_FOR"];[/code]

Compare, print, whatever you please!

Perhaps i have missed the point? but it seems to me that this would be dramaticly simpler, require drematicaly less code, and thus be much easyer to find out were things are going wrong.

Link to comment
Share on other sites

[quote author=btherl link=topic=117640.msg480286#msg480286 date=1165466316]
Try including your class definitions before session_start().  This is a shot in the dark but it may work.  Also see http://sg.php.net/manual/en/language.oop.serialization.php
[/quote]

btherl, the 'shot in the dark' hit the target. Thanks very much. As I've said that I'm a PHP newbie, I was not aware of this issue. This was a great help. Thanks a lot. Have good days.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.