Jump to content

help me!!!!


Recommended Posts

Posted

ok im trying to learn some PHP and i has problem

if i change the db password to something else than it is it will give me an error, but it still wont connect with the right details pls help plox

my connection class

class connection {
  protected $db_connect;
  public $db_username = 'root';
  public $db_hostname = 'localhost';
  public $db_database = 'test';
  public $db_password = '';

  function db_connection() {
    try {
      $this->connection = new PDO("mysql:host=$this->db_hostname;$this->db_database", $this->db_username, $this->db_password);
      $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      return $this->connection;
    } catch(PDOException $error) {
      echo $error->getMessage();
    }
  }
  function debug_Connection() {
    if($this->db_connect == true) {
      echo 'Connection debugged.';
    }
  }
}
$test = new connection;
$test->db_connection();

pls help im stuck

Posted (edited)
8 minutes ago, Facial said:

ok im trying to learn some PHP and i has problem

if i change the db password to something else than it is it will give me an error, but it still wont connect with the right details pls help plox

my connection class


class connection {
  protected $db_connect;
  public $db_username = 'root';
  public $db_hostname = 'localhost';
  public $db_database = 'test';
  public $db_password = '';

  function db_connection() {
    try {
      $this->connection = new PDO("mysql:host=$this->db_hostname;$this->db_database", $this->db_username, $this->db_password);
      $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      return $this->connection;
    } catch(PDOException $error) {
      echo $error->getMessage();
    }
  }
  function debug_Connection() {
    if($this->db_connect == true) {
      echo 'Connection debugged.';
    }
  }
}
$test = new connection;
$test->db_connection();

pls help im stuck

Try changing 

class connection {
  protected $db_connect;
  public $db_username = 'root';
  public $db_hostname = 'localhost';
  public $db_database = 'test';
  public $db_password = '';

to


class connection {
  protected $db_connect;
  public $db_username = "root";
  public $db_hostname = "localhost";
  public $db_database = "test";
  public $db_password = "";

Also, I usually work via DAO

class DAO {
	private static $dbHost = "localhost";
	private static $dbName = "kcart";
	private static $dbUser = "kcartTest";
	private static $dbPass = "";
	private static $sharedPDO;

	protected $pdo;

	function __construct() {
		if(empty(self::$sharedPDO)) {
			self::$sharedPDO = new PDO("mysql:host=" . self::$dbHost . ";dbname=" . self::$dbName, self::$dbUser, self::$dbPass);
			self::$sharedPDO->exec("SET CHARACTER SET utf8");
			self::$sharedPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			self::$sharedPDO->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
		}
		$this->pdo =& self::$sharedPDO;
	}

 

Edited by Howest
Posted (edited)

Also I believe this:

$this->connection = new PDO("mysql:host=$this->db_hostname;$this->db_database", $this->db_username, $this->db_password);

Is invalid, you need to specify that 

$this->db_datebase

is the database name:

$this->connection = new PDO("mysql:host=$this->db_hostname;dbname=$this->db_database", $this->db_username, $this->db_password);

Might be wrong though

Edited by harrypotter
  • Like 2
Posted
13 minutes ago, Facial said:

Yes, but it still wont print the debug_Connection() function..

I missed the dbname= in the PDO(), now it also gives me error IF the DB wont exists, but doesnt give me a connection succeed message when I try to print it.

 

I'm not really sure what you mean, if should return an error if the database table doesn't exist. Why would it give a connection success message when it didn't successfully connect?

If you're getting an error stating that the db table doesn't exist then double check your table name is correct

Posted
6 minutes ago, harrypotter said:

I'm not really sure what you mean, if should return an error if the database table doesn't exist. Why would it give a connection success message when it didn't successfully connect?

If you're getting an error stating that the db table doesn't exist then double check your table name is correct

No, no and no.

First I was unable to get the error even with false details, now it gives me the error I want when false details.

But still wont print my debug function.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...