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/ impostazio ni.php");
require_once("../libs/db_o bj_class.p hp");
$table_name = "banner";
$db = new db_obj($table_name);
$db->search();
$db->assign_first_record() ;
echo "banner1=".UPLOAD_DIR."/ba nner/".$db ->banner1. "&banner2= ".UPLOAD_D IR."/banne r/".$db->b anner2;
?>
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_con nection=NU LL)
{
$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]->_exis ts = 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/h ttpdocs/si to_2006/li bs/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
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/
require_once("../libs/db_o
$table_name = "banner";
$db = new db_obj($table_name);
$db->search();
$db->assign_first_record()
echo "banner1=".UPLOAD_DIR."/ba
?>
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_con
{
$this->_exists=false;
$this->_records=array();
$this->_table_name=$table_
if ($db_connection)
$this->_db_connection=$db_
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=
{
$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]->_exis
$this->_records[$i]->fetch
}
}
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
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
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.
$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).
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
ASKER
This is the output:
Notice: Undefined property: db_obj::$_table_name in /usr/local/psa/home/vhosts /naba.it/h ttpdocs/si to_2006/li bs/db_obj_ class.php on line 68
Notice: Undefined property: db_obj::$_db_connection in /usr/local/psa/home/vhosts /naba.it/h ttpdocs/si to_2006/li bs/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/h ttpdocs/si to_2006/li bs/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/h ttpdocs/si to_2006/li bs/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/h ttpdocs/si to_2006/si to/_carica _banner.ph p 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.
Notice: Undefined property: db_obj::$_table_name in /usr/local/psa/home/vhosts
Notice: Undefined property: db_obj::$_db_connection in /usr/local/psa/home/vhosts
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /usr/local/psa/home/vhosts
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /usr/local/psa/home/vhosts
Notice: Use of undefined constant UPLOAD_DIR - assumed 'UPLOAD_DIR' in /usr/local/psa/home/vhosts
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
An additional thought, if you are using php mysqli commands, ensure that php.ini has a section
[Extensions]
extension = mysqli.so
[Extensions]
extension = mysqli.so
$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.