EdwardJ Posted November 12, 2008 Share Posted November 12, 2008 Hi, I'm trying to connect to a DB with the following: <?php define("host", "localhost"); define("user", "root"); define("password", "password"); define("database", "db"); ?> <?php include ("constants.php"); class DB_connect_select{ var $connection; function DB_access(){ $this->connection=mysql_connect(host, user, password); if($this->connection) echo "Connection successful! </BR>"; } } ?> <?php include("constants.php"); include("class.php");?> <HTML> <HEAD> <TITLE>Class DB connection test</TITLE> </HEAD> <BODY> <?php $db = new DB_connect_select(); ?> </BODY> </HTML> The problem is I am not getting the echo connection successful, which tells me the script is not connecting to the database. Also, if I put a wrong username or password, same thing happens, I don't even get an error message telling wrong user and/or password was input. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/132462-mysql-connection/ Share on other sites More sharing options...
plznty Posted November 12, 2008 Share Posted November 12, 2008 <?php //Usually localhost $host = "localhost"; //Database Username $username = "root"; //Database Password $dbpass = "password"; //Database Name $dbname = "db"; $db=mysql_connect ("$host", "$username", "$dbpass") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("$dbname"); ?> Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688704 Share on other sites More sharing options...
EdwardJ Posted November 12, 2008 Author Share Posted November 12, 2008 Yes it works, but I need to do it using classes and objects. thanks Quote Link to comment https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688717 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 You need a constructor, I am assuming you are not using PHP 5 for this: <?php include ("constants.php"); class DB_connect_select{ var $connection; function DB_connect_select(){ // name of the class denotes constructor in php < 5. $this->connection=mysql_connect(host, user, password); if($this->connection) echo "Connection successful! </BR>"; } } ?> For PHP 5 it would be: <?php include ("constants.php"); class DB_connect_select{ private $connection; public function __constructor(){ $this->connection=mysql_connect(host, user, password); if($this->connection) echo "Connection successful! </BR>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688726 Share on other sites More sharing options...
EdwardJ Posted November 12, 2008 Author Share Posted November 12, 2008 the block you posted with the _constructor function does not work, and I am using php 5. the first block does work. Quote Link to comment https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688732 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 the block you posted with the _constructor function does not work, and I am using php 5. the first block does work. Whoops here it is corrected: <?php include ("constants.php"); class DB_connect_select{ private $connection; public function __construct(){ $this->connection=mysql_connect(host, user, password); if($this->connection) echo "Connection successful! </BR>"; } } ?> Should be just construct, not constructor. Sorry about that. Quote Link to comment https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688734 Share on other sites More sharing options...
EdwardJ Posted November 12, 2008 Author Share Posted November 12, 2008 Im very sorry for insisting, but using: <?php include ("constants.php"); class DB_connect_select{ private $connection; private $selection; public function _construct(){ $this->connection=mysql_connect(host, user, password); if($this->connection) echo "Connection to DB successful! </BR>"; else die(mysql_error()); $this->selection=mysql_select_db(database, $this->connection); if($this->selection) echo "DB selection successful!"; else die(mysql_error()); } } ?> not working. Quote Link to comment https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688743 Share on other sites More sharing options...
EdwardJ Posted November 12, 2008 Author Share Posted November 12, 2008 it only works if i use protected or var to declare $connection and $selection. doesnt work with private though Quote Link to comment https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688751 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Where are you trying to initiate the connection? <?php include ("constants.php"); class DB_connect_select{ private $connection; public function _construct(){ if($this->connection=mysql_connect(host, user, password)) echo "Connection to DB successful! </BR>"; else die(mysql_error()); if(mysql_select_db(database, $this->connection)) echo "DB selection successful!"; else die(mysql_error()); } } ?> I think your if statements were just wrongly done, try the above and see if that works out. EDIT: Well then I would use protected. The connection string does not necessarily need to be private, but we also do not want to be easily changed. Oh and selection does not need to be tied to a string, like connection string does. Quote Link to comment https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688755 Share on other sites More sharing options...
EdwardJ Posted November 12, 2008 Author Share Posted November 12, 2008 I am very sorry once again. the following works: <?php include ("constants.php"); class DB_connect_select{ private $connection; private $selection; public function __construct(){ $this->connection=mysql_connect(host, user, password); if($this->connection) echo "Connection to DB successful! </BR>"; else die(mysql_error()); $this->selection=mysql_select_db(database, $this->connection); if($this->selection) echo "DB selection successful!"; else die(mysql_error()); } } ?> problem was, I was using a single underscore to name the function (as in _construct() instead of __construct()) Quote Link to comment https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.