0
我正在使用slim-framework v2和php來獲取json outputs.With這段代碼,我得到了關於該主題描述的錯誤。 MySQL表格格式不正確,現在無法更改,所以我以這種方式製作這些功能,因爲我不是很有經驗。 解決這個問題的最好方法是什麼?如果可以用sql命令完成,會更好,所以我可以從主循環中刪除這些函數嗎?循環中的函數是否壞? ps:讓我知道如果主題看起來不對。錯誤:PDO類的對象無法轉換爲字符串
測試()是客戶ID
function test($server, $user, $pass, $db) {
$result = array();
$sql = "SELECT cid from tasks where total!=0";
$db = newDB($server, $user, $pass, $db);
$stmt = $db->prepare($sql);
$stmt->execute();
foreach ($stmt as $v) {
$result[] = ['cid'=> $v['cid'], 'test'=> toGet($server, $user, $pass, $db, $v['cid']), 'test2'=> toGet2($server, $user, $pass, $db, $v['cid'])];
}
echo json_encode($result, JSON_FORCE_OBJECT);
}
和其他功能
function toGet($server, $user, $pass, $db, $id) {
$sql = "SELECT sum(`poso`) as posoe from `economics` where `custid`=:id and flag=0";
$db = newDB($server, $user, $pass, $db);
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_NUM);
return $row[0];
}
function toGet2($server, $user, $pass, $db, $id) {
$sql = "SELECT sum(`poso`) as posoe from `economics` where `custid`=:id and flag=1";
$db = newDB($server, $user, $pass, $db);
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_NUM);
return $row[0];
}
非常感謝你的主循環!
UPDATE: 打開調試$ APP->配置( '調試',真正的),終於得到了錯誤 類型:ErrorException 代碼:4096 消息:類PDO的對象無法轉換爲字符串 文件:/var/www/api/newdb.php 線:6
function newDB($host, $user, $pass, $db) {
$dbhost= $host;
$dbuser= $user;
$dbpass= $pass;
$dbname= $db;
$dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbConnection->query('set names utf8');
return $dbConnection;
}
UPDATE2:修身的conf
include 'newdb.php';
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->config('debug', true);
$app->response->setStatus(200);
$app->contentType('application/json; charset=utf-8');
$app->get('/test/:server/:user/:pass/:db', 'test');
$app->get('/', function() use($app) {
$app->response->setStatus(200);
echo "Welcome!";
});
$app->run();
嗯......什麼行生成該錯誤獲取數據? – AbraCadaver
這是第6行,我的問題已更新 – UrL