Urgent, write Class with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tiendan
Forum Newbie
Posts: 9
Joined: Tue Apr 01, 2003 11:39 pm
Location: Vn

Urgent, write Class with PHP

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

$host is a member variable, it should be referenced to as $this->host in your constructor.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

my brain is still off a bit today.. I completely missed that bit. :)

Well done tim. Here's a cookie.
Post Reply