Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/132462-mysql-connection/
Share on other sites

<?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

Link to comment
https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688704
Share on other sites

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>";
   }
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688726
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688734
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688743
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688755
Share on other sites

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())

Link to comment
https://forums.phpfreaks.com/topic/132462-mysql-connection/#findComment-688758
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.