Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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:

<?php

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

//check connection
if(mysqli_connect_errno())
{
    echo "Failed to connect to MySQL" . mysqli_connect_error();
}

ini_set('display_errors',1);
error_reporting(E_ALL);


$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!";
      die(json_encode($response));
     }

 // 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!";
            die(json_encode($response));
       }

   ?>

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.

share|improve this question
    
I generally don't use PDO, I prefer mysqli but although I'm not sure about your problem I can see the construct call to PDO is a little different from the one in the documentation here php.net/manual/en/pdo.construct.php where the code is something like this $db=new PDO("'mysql:dbname=testdb;host=127.0.0.1", "username","password"); and I think you won't need your first line where you call mysqli_connect() –  Mark E Aug 3 at 22:54

1 Answer 1

up vote 2 down vote accepted

First of all this part of your code is not necessary

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

//check connection
if(mysqli_connect_errno())
{
    echo "Failed to connect to MySQL" . mysqli_connect_error();
}

Also, the PDO connection made should be this way

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

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

You can read more about PDO connections here

share|improve this answer
    
This has started making the server send back the data i need however it is now also throwing a notice of: "Undefined variable: query_params in 'url to php page' on line 18" I know what the error is, would it be best to remove that or where would I look to declare it? –  geolaw Aug 3 at 23:19
1  
change this $stmt = $db->prepare($query); to $stmt = $db->query($query); and remove $result = $stmt->execute($query_params); from your code –  Kombian Aug 3 at 23:25
    
That's sorted it!!! Thanks a lot man, you have helped me in a major way with this. –  geolaw Aug 3 at 23:31
    
Just wanna state that if you don't "prepare" you statement and just go direct with the query and you use user obtained variables, you'll be exposed to a SQLinjection attack. If you don't use user obtained variables just the query is cool –  Mark E Aug 6 at 17:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.