0
當在phpMyAdmin中運行此查詢時,我得到正確的值, 但在PHP腳本中使用同一行時,它總是給出x = 0 y = 0。錯誤的值從MYSQL使用PHP
所有其他值是正確的只有X和Y 由於某種原因,返回0。
EDITED沒有得到正確的價值觀
代碼:
$sql = "select a.image_id as id,
i.image_url as url,
i.image_x as x,
i.image_y as y
from album a
join images i
where a.album_id = 1
and
i.image_id = a.image_id";
echo getFunc($sql);
function getFunc($sql) {
try {
$db = getConnection();
$stmt = $db->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$db = null;
return json_encode($result);
} catch(PDOException $e) {
echo $e->getMessage();
}
};
function getConnection() {
$dbhost="127.0.0.1";
$dbuser="root";
$dbpass="";
$dbname="efrattest";
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec("set names utf8");
return $dbh;
}
MySQL表:
image_id int(11)
image_url varchar(250)
image_x int(9)
image_y int(9)
是$ sql的定義?在這個例子中我們不能說因爲有一個雜散的select查詢 – Ray
正如Ray指出的那樣,是否定義了$ sql? $ album_id是否正確定義? – Daniel
對不起,我編輯了這個問題。 還有什麼不對嗎? –