i'm doing a project and using PDO inside a class, all functions works like a charm, but i have this one that is returning an invalid data source error, here is the textual copy:
Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name' in /home/decoded/public_html/studiobug/xxxx/class.php:194
Stack trace:
#0 /home/decoded/public_html/studiobug/xxxx/class.php(194): PDO->__construct('DB_DSN', 'DB_USERNAME', 'DB_PASSWORD') #1 /home/decoded/public_html/studiobug/xxxx/deleteQuiz.php(5): Admin->goodByeQuiz('2') #2 {main} thrown in /home/decoded/public_html/studiobug/xxxx/class.php on line 194
And here are 2 public functions inside the class, first one works fine, and the second one is the one returning me that error. I can't find what's going on since the code is almost the same, only queries change.
public function saveEditQuestions($quizno,$todo) {
$table = "quiz".$quizno."Questions";
$sql = "";
$sql .= "TRUNCATE $table;";
foreach ($todo as $c => $v){
$sql .= "INSERT INTO $table SET ";
$sql .= "option1 = '".$v['Answer1']."', ";
$sql .= "option2 = '".$v['Answer2']."'; ";
}
$con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
try {
$stmt = $con->prepare($sql);
$success = $stmt->execute();
return $success;
}
catch (PDOException $e)
{
echo $e->getMessage();
die();
}
}
public function goodByeQuiz($del) {
$table1 = "quiz".$del."Questions";
$table2 = "quiz".$del."Results";
$sql = "UPDATE formStatus set active = 0 WHERE formNumber = $del;";
$sql .= "TRUNCATE $table1;";
$sql .= "TRUNCATE $table2;";
$con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
try {
$stmt = $con->prepare($sql);
$success = $stmt->execute();
return $success;
}
catch (PDOException $e)
{
echo $e->getMessage();
die();
}
}