Forum Moderators: coopster

Message Too Old, No Replies

Retrieving data from PHP include file

Error is generated when using include.

         

ladams02

9:15 pm on Aug 22, 2006 (gmt 0)

10+ Year Member



I'm trying to use an external database.php file to store all the functions for retrieving my data from a MySQL DB. I have a MySQLDB class, that I create an object of called $database. If I include everything in one PHP file, it works, here is the complete file:

<?php
include("http://localhost/ladams/includes/constants.php");

class MySQLDB {
var $connection; //The MySQL database connection

/* Class constructor */
function MySQLDB() {
/* Make connection to database */
$this->connection = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('ladams') or die(mysql_error());

}

function getSiteNews ($x) {
$q = "SELECT * FROM site_news order by id desc limit 0,".$x;
$res = mysql_query($q);

return $res;
}

};

/* Create database connection */
$database = new MySQLDB;

global $database;
$news = $database->getSiteNews(1);
while ($row = mysql_fetch_row($news)) {
echo $row[0];
}
?>

However, if I take those last 5 lines of code out, and place them in my HTML file, with the database.php file as an include, I receive this error: Fatal error: Call to a member function getSiteNews() on a non-object in C:\wamp\www\ladams\index.html on line 56

What am I missing here? Thanks!

siMKin

1:33 pm on Aug 23, 2006 (gmt 0)

10+ Year Member



you can only include php code that runs on the same server, and in most cases that belongs to your domain only.
you should therefor never use http://

eternal, as in from another server, is not going to work

[edited by: siMKin at 1:33 pm (utc) on Aug. 23, 2006]

ladams02

1:51 pm on Aug 23, 2006 (gmt 0)

10+ Year Member



Took the http:// out and it worked like a charm. Many thanks! I was using the PHP include function to include HTML files with the full URL and it worked, so I didn't think twice about it.