0
我想使切換到PDO。儘管我很快遇到了一些問題,但我無法解決的問題是使用隨機圖像腳本。我有一個關於這個不工作的以前的帖子,它必須與SELECT MAX(id) needed to be SELECT MAX(id-6)
。在問題出現之前是我得到隨機圖像的隨機數,從未6.試圖使開關PDO,隨機圖像腳本並不總是火
現在的問題是,有時我得到的六個隨機圖像,有的時候我沒有。我希望有人可以看看我的腳本,看看他們是否可以發現任何問題。
由於有時第二雙眼睛,是所有需要:
<?php
$passed_cat = mysql_real_escape_string($_GET['cat']);
$passed_sub_cat = mysql_real_escape_string($_GET['sub_cat']);
include ('../../connection.php');
$related_images = "SELECT *,r1.id as main_id
FROM images AS r1
JOIN
(
SELECT ROUND(RAND() * (SELECT MAX(id-6)
FROM images)) AS id
) AS r2
WHERE r1.id >= r2.id
AND public_approved='Approved'
AND category = :category
AND sub_category = :sub_category
ORDER BY r1.id ASC
LIMIT 6
OFFSET 20";
$q = $conn->prepare($related_images);
$q->execute(array(
':category' => $passed_cat,
':sub_category' => $passed_sub_cat));
if(!$q)
{
die("Query failed due to: ". $conn->errorInfo());
}
$q->setFetchMode(PDO::FETCH_BOTH);
while($row = $q->fetch())
{
echo "<div style='float:left; margin:2px;' >";
echo '<a href="sub_page.php?cat='. $passed_cat .'&sub_cat='. $passed_sub_cat .'&id='. $row['main_id'] .'"><img src="../../files/small/thumb0_'. $row['item_name'] . '.' . $row['file_extension'] . '" style="width:96px;" border="0"></a>';
echo "</div>";
}
?>
編輯:Connection.php
<?php
$host = "localhost";
$dbname = "all_sites";
$db_username = "root";
$db_password = "password";
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",$db_username,$db_password);
}
catch (PDOException $pe)
{
die('Connection error, due to: ' .$pe->GetMessage());
}
?>
久經考驗你說這裏有什麼,但是仍然存在錯誤 – Owen
有你在phpMyAdmin總是檢查您的查詢,並將其作品,也沒有關係,但localhost更改爲127.0.0.1,它會加快PDO –
謝謝你的提示,當在phpMyAdmin它並不總是工作。有時,它返回一個空集,我會試着重新開始,看看我是否能得到它的工作。謝謝 – Owen