dongmeijian1716 2014-08-03 22:44
浏览 50
已采纳

使用PHP从MySQL服务器中提取数据以JSON格式显示

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.

  • 写回答

1条回答 默认 最新

  • dsfds2353 2014-08-03 23:11
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错