So i am trying to make connect to a database. However, I am running into the issue of grabbing strings to connect to the database. First one fails, second one is successful.
PHP Code:
class dbedits {
private $pdo = null;
private $servername = "localhost";
private $dbusername = "root";
private $dbpassword = "";
public function dbconnect($dbname){
try{
$this->$pdo = new PDO("mysql:host=".$this->servername, $this->$dbusername, $this->$dbpassword);
$this->$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbname = "`".str_replace("`","``",$dbname)."`";
$this->$pdo->query("CREATE DATABASE IF NOT EXISTS $dbname");
$this->$pdo->query("use $dbname");
}catch(PDOException $e){
echo $e->getMessage();
}
}
public function dbcreate($dbname){
//$this->dbconnect($dbname);
try{
$pdo = new PDO("mysql:host=".$this->servername, "root","");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbname = "`".str_replace("`","``",$dbname)."`";
$pdo->query("CREATE DATABASE IF NOT EXISTS $dbname");
$pdo->query("use $dbname");
}catch(PDOException $e){
echo $e->getMessage();
}
}
}
I fixed it. I forgot to remove the '$' top ones.
PHP Code:
public function dbconnect($dbname){
try{
$this->pdo = new PDO("mysql:host=".$this->servername, $this->dbusername, $this->dbpassword);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbname = "`".str_replace("`","``",$dbname)."`";
$this->pdo->query("CREATE DATABASE IF NOT EXISTS $dbname");
$this->pdo->query("use $dbname");
}catch(PDOException $e){
echo $e->getMessage();
}
}
Bookmarks