将PHP文件转换为PDO

时间:2014-09-18 22:45:12

标签: php android mysql json pdo

我开始使用php而且我需要你的帮助。我想让以下代码到PDO有一个json输出的Android应用程序我正在尝试bulit.I尝试了很多解决方案但没有任何正确的来了因为JSON回复。我也找不到好的例子和教程。我也是新的,因为我用php说,所以我害怕尝试复杂的场景

这是php代码

这是我的配置文件

db_config.php

<?php

define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "androidhive"); // database name
define('DB_SERVER', "localhost"); // db server
?>

这是连接文件

db_connect.php
<?php


class DB_CONNECT {

    // constructor
    function __construct() {
        // connecting to database
        $this->connect();
    }

    // destructor
    function __destruct() {
        // closing db connection
        $this->close();
    }

    /**
     * Function to connect with database
     */
    function connect() {
        // import database connection variables
        require_once __DIR__ . '/db_config.php';

        // Connecting to mysql database
        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

        // Selecing database
        $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

        // returing connection cursor
        return $con;
    }

    /**
     * Function to close db connection
     */
    function close() {
        // closing db connection
        mysql_close();
    }

}

?>

get_all_products.php

<?php

/*
 * Following code will list all the products
 */

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// get all products from products table
$result = mysql_query("SELECT *FROM products") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["products"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $product = array();
        $product["pid"] = $row["pid"];
        $product["name"] = $row["name"];
        $product["price"] = $row["price"];
        $product["created_at"] = $row["created_at"];
        $product["updated_at"] = $row["updated_at"];

        // push single product into final response array
        array_push($response["products"], $product);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);
}
?>

我知道对于有经验的人来说很容易,但我对它很苛刻。请帮忙,因为我有一个严格的截止日期,现在没有时间进行低价搜索。谢谢你

**如果我发布我所做的事情会不会有帮助?我没有张贴空间原因** s

EDIT 这就是我为什么做出了什么想法?

$db = new PDO("mysql:host=$dbhost;dbname=$dbname;", $dbuser, $dbpass);
$query = "Select * FROM products";


//execute query

try {

    $stmt   = $db->prepare($query);
    $result = $stmt->execute(HAVE NO IDEA!!!!);
}

catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Database Error!";
    die(json_encode($response));

}




$rows = $stmt->fetchAll();
if ($rows) {
    $response["success"] = 1;
    $response["message"] = "Post Available!";
    $response["posts"]   = array();


    foreach ($rows as $row) {
        $post             = array();
        $post["pid"] = $row["pid"];
        $post["name"]    = $row["name"];
        $post["price"]  = $row["price"];


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

    }



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

} else {

    $response["success"] = 0;
    $response["message"] = "No Post Available!";
    die(json_encode($response));

}


?>

1 个答案:

答案 0 :(得分:0)

尝试这种方法:

<?php
$response = array()
try {
    //connection
    $db = new PDO("mysql:host=$dbhost;dbname=$dbname;", $dbuser, $dbpass);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //prepare
    $query = "Select * FROM products";
    $stmt   = $db->prepare($query);
    //get resutlt
    $result = $stmt->execute();
    if ($stmt->rowCount > 0) {
        $response["success"] = true;
        $response["message"] = "Post Available!";

        //populate post array
        while($row = $stmt->fetch()){
            $response["posts"][] = $row;
        }
    }else{
        $response["success"] = false;
        $response["message"] = "No Post Available!";    
    }
}

catch (PDOException $ex) {
    $response["success"] = false;
    $response["message"] = "Database Error!";  
}

echo json_encode($response);

?>

Prepared statements and stored procedures

<小时/> 使用fetch all:

$rows = $stmt->fetchAll();
var_dump($rows);
$response["posts"] = $rows;