Page 1 of 1

Urgent, write Class with PHP

Posted: Tue Jan 25, 2005 7:34 am
by tiendan
Now I have write 1 class in PHP 5

file db_connect.inc

<?
class db_connect {

var $host="localhost";
var $user="";
var $pass="";
var $dbname="test";

function db_connect() {

$conn = mysql_connect($host,$user,$pass);

if (!$conn) {
echo "Could connect to database".mysql_error();
return 0;
}
echo "Connected";
return $conn;
}
}
?>

file test.php

<?
require_once 'db_connect.inc';

$db = new db_connect();

echo $db->host;
echo $db;
?>

But error was output

Error was output at line mysql_connect($host,$user,$pass); . Can not connect. I don't know this error.

And have a different between PHP4 Class and PHP5 Class.

Thanks

Posted: Tue Jan 25, 2005 8:10 am
by feyd
did you turn on mysql support in you php5 config? php5 doesn't come with mysql support by default. It does come with mysqli support, but it much be enabled, I believe..


a note to everyone else: placing urgent, help!, and the like does not make us rush in to see what the problem is any faster than posting like normal. So please, don't waste your time typing these things in your topic. :roll:

Posted: Tue Jan 25, 2005 9:25 am
by timvw
$host is a member variable, it should be referenced to as $this->host in your constructor.

Posted: Tue Jan 25, 2005 9:29 am
by feyd
my brain is still off a bit today.. I completely missed that bit. :)

Well done tim. Here's a cookie.