Jump to content

help me!!!!


Wife

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Recently Browsing   0 members

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