Pulling data from MySQL Server using PHP to display in JSON format


I have seen a lot of similar questions and answers for this issue but nothing I try is working. So I thought I would ask as this is day 2 of my fight with this code to no avail.

Here is my code:


$con=mysqli_connect("localhost", "username", "password", "database");

//check connection
    echo "Failed to connect to MySQL" . mysqli_connect_error();


$db=new PDO("mysqli:host=localhost;dbname=table", "username","password");

//initial query
$query = "Select * FROM table";

//execute query
try {
      $stmt   = $db->prepare($query);
      $result = $stmt->execute($query_params);
catch (PDOException $ex) {
      $response["success"] = 0;
      $response["message"] = "Database Error!";

 // Finally, we can retrieve all of the found rows into an array using fetchAll 
 $rows = $stmt->fetchAll();

 if ($rows) {
     $response["success"] = 1;
     $response["message"] = "Details Available!";
     $response["details"]   = array();

 foreach ($rows as $row) {
     $post             = array();
$post["ID"]  = $row["ID"];
     $post["cohort_name"] = $row["cohort_name"];
     $post["pin"]    = $row["pin"];
     $post["start_date"]  = $row["start_date"];

     //update our repsonse JSON data
     array_push($response["details"], $post);

     // echoing JSON response
     echo json_encode($response);

   } else {
            $response["success"] = 0;
            $response["message"] = "No Details Available!";


My PHP is god awful and I got most of this if not all from reading what other people use. the error code I'm getting here is:

Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in >'url to php page':15 Stack trace: #0 >'url to php page'(15): PDO->__construct('mysqli:host=loc...', >'username', 'password') #1 {main} thrown in >'url to php page' on line 15

Im using a MySQL server that is running wordpress, that I want to kinda work around and connect straight to the database because I'm setting up a separate user system for an Angular app (hence why I'm using JSON) I am already writing directly to the same sql table from the wordpress site using a .php page however the php page in question either throws some kind of error like the one above or doesn't throw anything up at all, just a blank page. Any insight would be greatly appreciated.




First of all this part of your code is not necessary

$con=mysqli_connect("localhost", "username", "password", "database");

//check connection
    echo "Failed to connect to MySQL" . mysqli_connect_error();

Also, thePDO connection made should be this way

$db = new PDO('mysql:host=localhost;dbname=table', $user, $pass);

make sure correct values are provided for thetable,$user and$pass

You can read more about PDO connections here

People are also looking for solutions to the problem: php cURL bot redirect issue


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.