0

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();
}   
}
Community
  • 1
  • 1
  • Have you defined DB_DSN, DB_USERNAME and DB_PASSWORD ? – Indra Kumar S Jan 02 '15 at 18:00
  • yes of course, the first function (saveEditQuestions) works fine, when i call the second i get that exception – Gustavo Benito Silva Jan 02 '15 at 18:01
  • 1
    The fact that the stacktrace mentions `'DB_DSN'` (a string) indicates that there is no such constant (if you check the PHP error log, you will see a PHP notice complaining about this). Review the code that defines those constants, it is most likely not being included when you call the second function. – DCoder Jan 02 '15 at 18:07
  • So `var_dump` values when you call your function and check values. – u_mulder Jan 02 '15 at 18:07
  • What @DCoder says... thats likely the problem. Also why do you recreate the connection in each method why not store it as a property on the class during construction??? – prodigitalson Jan 02 '15 at 18:08
  • it's really strange... why it doesn't find that constant on that particular function and it does on the other 7 ? .... will check it though and perform all sugestions here..... – Gustavo Benito Silva Jan 02 '15 at 18:12
  • possible duplicate http://stackoverflow.com/questions/19740829/uncaught-exception-pdoexception-message-invalid-data-source-name – fortune Jan 02 '15 at 18:12

1 Answers1

1

The problem seems to be with your connections. Use it like this....

connect.php

<?php 
$db_host = "localhost";
$db_name = "blogdata";
$db_user = "username";
$db_pass = "password";
$db = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

And in your functions,

require 'connect.php';

And use this connection wherever you want

Indra Kumar S
  • 2,818
  • 2
  • 16
  • 27