Link to home
Start Free TrialLog in
Avatar of bobo1979a
bobo1979a

asked on

OOP issues migrating to php5

Hi, my provider upgraded server's OS (FreeBSD from 5.4 to 6.2), mySQL (5.0.x) and PHP (from 4 to 5.2.x).
Now my application don't work anymore and I'm becoming crazy finding why.

I have a page, that loads data from the DB using classes:
<?
require_once("../settings/impostazioni.php");
require_once("../libs/db_obj_class.php");

$table_name = "banner";
$db = new db_obj($table_name);
$db->search();
$db->assign_first_record();
echo "banner1=".UPLOAD_DIR."/banner/".$db->banner1."&banner2=".UPLOAD_DIR."/banner/".$db->banner2;
?>

Impostazioni.php contains the DB informations and db_obj_class.php contains the Classes and Functions.

When I build the reference of the class
     $db = new db_obj($table_name);
it should pass the table name to the function

public function db_obj($table_name,$db_connection=NULL)
{
    $this->_exists=false;
    $this->_records=array();
    $this->_table_name=$table_name;
   
     if ($db_connection)
         $this->_db_connection=$db_connection;
     else
         $this->db_connect();
         $this->table_list_fields();
}

But that value isn't passed and when I call the Search function
     $db->search();

function search($where=null,$order=null)
{
     $query = "select * from ".$this->_table_name;
     if ($where)
         $query.=" where ".$where;
     if ($order)
         $query.=" order by ".$order;
     if ( ! $res = $this->query($query) )
         return false;

     $n_res = mysql_num_rows( $res );
     for ( $i=0; $i < $n_res; $i++ )
          {
          $this->_records[$i] = new db_obj( $this->_table_name, $this->_db_connection );
          $this->_records[$i]->_exists = true;
          $this->_records[$i]->fetch( mysql_fetch_row( $res ) );
          }
}

function query( $query )
{
     echo $query."<BR>";
     $res = mysql_query( $query, $this->_db_connection );
                  
     $this->debug_notice( $query );
     $this->debug_error( mysql_error() );
                  
     if ( mysql_num_rows($res) ) {
         return $res;
     } else {
         return false;
     }
}

I notice that the table name is empty in the $query (select * from) and it gives me the error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /usr/local/psa/home/vhosts/naba.it/httpdocs/sito_2006/libs/db_obj_class.php on line 53 (that's the row $res = mysql_query($query, $this->_db_connection); )

The error is the same even if I write the table name directly in the query ($query = "select * from banner";

I just can't understand why the table name isn't passed in the class and why the query doesn't work even if I write it directly.

Thanks in advance
Roberto
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

After this

$table_name = "banner";
$db = new db_obj($table_name);

put this

echo "<pre>"; print_r($db); echo "</pre>";

What is in $db?

Do the same thing just before your query is run.
Avatar of bobo1979a
bobo1979a

ASKER

After this

$table_name = "banner";
$db = new db_obj($table_name);

Outputs:

db_obj Object
(
)

then:

echo $query."<BR>";
echo "<pre>"; print_r($db); echo "</pre>";
$res = mysql_query( $query, $this->_db_connection );

Outputs nothing.
Sounds like the object is not being created. As the very, very, very first line of your code after the first <?php add

error_reporting(E_ALL);

Do any error messages show up? If not can you look in the apache error log usually held at

/var/log/httpd/error_log   or /var/log/apache2/error_log

depending on version. Look at the last entries, use tail -n 20 /var/log/apache2/error_log (or the other one).
This is the output:

Notice: Undefined property: db_obj::$_table_name in /usr/local/psa/home/vhosts/naba.it/httpdocs/sito_2006/libs/db_obj_class.php on line 68

Notice: Undefined property: db_obj::$_db_connection in /usr/local/psa/home/vhosts/naba.it/httpdocs/sito_2006/libs/db_obj_class.php on line 54

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /usr/local/psa/home/vhosts/naba.it/httpdocs/sito_2006/libs/db_obj_class.php on line 54

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /usr/local/psa/home/vhosts/naba.it/httpdocs/sito_2006/libs/db_obj_class.php on line 59

Notice: Use of undefined constant UPLOAD_DIR - assumed 'UPLOAD_DIR' in /usr/local/psa/home/vhosts/naba.it/httpdocs/sito_2006/sito/_carica_banner.php on line 12

and so on errors...

I understand that the object if is created it's empty. I can't understand why, and why I can't read the constants from inside the class.
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
An additional thought, if you are using php mysqli commands, ensure that php.ini has a section

[Extensions]
extension = mysqli.so