2 回答

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超22個(gè)贊
你可以這樣做:
<?php
$pdo = new PDO("mysql:dbname=database;host=127.0.0.1", "user", "password");
$statement = $pdo->prepare("SELECT * FROM table");
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
編輯:假設(shè)您已連接到數(shù)據(jù)庫(kù):
if ($_SERVER['REQUEST_METHOD'] == 'GET'){
$pdo = new PDO("mysql:host={$db['host']};dbname={$db['db']}", $db['username'], $db['password']);
$sql = $pdo->prepare("CALL consulta administratdor()";
$sql->execute();
$results = $statement->fetchALL(PDO::FETCH_ASSOC);
$json = json_encode($results);
}

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
我得到了問(wèn)題的解決方案,問(wèn)題是在我的數(shù)據(jù)庫(kù)中我有json_encode函數(shù)無(wú)法理解的特殊字符,為了解決它,請(qǐng)將此行聚合到我的代碼中$dbConn->query("SET NAMES 'UTF8'");:
$dbConn = connect($db);
$dbConn->query("SET NAMES 'UTF8'");
if ($_SERVER['REQUEST_METHOD'] == 'GET'){
$sql = "CALL consulta_administrador()";
$q = $dbConn->query($sql);
$data = $q->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data, JSON_UNESCAPED_UNICODE);
}
- 2 回答
- 0 關(guān)注
- 170 瀏覽
添加回答
舉報(bào)