你好,谢谢你查看我的问题,我会讨论的。
我希望提取存储在MySQL数据库中两个不同表中的一堆数据,将每一行的值分配到数组中(如果这是最好的方法),然后使用这些数据填充html网格元素(这样每一行都用于填充网格中的一个框)。
这将最终成为php-mysql-webapp中线程的网格预览,用户将线程显示为"预览项",他们可以单击该项来完整打开线程。到目前为止,这就是我处理这项任务的方式,你有什么技巧或建议吗
idea_thread包含有关线程的信息,thumb包含用于缩略图和网格框侧的图像。
$result = $stmt = $mysqli->query("SELECT i.idea_id, i.user_id, i.time, i.title, i.Brief, t.location, t.width, t.height, t.colour
FROM idea_thread i, thumb t
WHERE t.idea_id = i.idea_id
ORDER BY i.idea_id DESC LIMIT 150,5");
while ($row = $stmt->fetch_array(MYSQLI_ASSOC)){
$id = $row[0];
$user = $row[1];
$date = $row[2];
$title = $row[3];
$briefdescrip = $row[4];
$photolocation = $row[5];
$width = $row[6];
$height = $row[7];
$color = $row[8];
};
?>
感谢您的阅读和提供的任何提示。
然而,对于某个赛季,这个代码只返回一个白色屏幕?有什么想法吗?
由于建议而应用的编辑,反映在我的代码**
请尝试以下操作:
$mysqli = 'do connect';
$stmt = $mysqli->prepare("
SELECT * i.idea_id,
i.user_id,
i.time,
i.title,
i.Brief,
t.location,
t.width,
t.height,
t.colour
FROM idea_thread i
LEFT OUTER JOIN thumb t ON t.idea_id = i.idea_id
ORDER BY i.idea_id DESC");
$stmt->execute();
while ($row = $stmt->fetch()) {
//do staff here
}
参考:http://www.php.net/manual/en/mysqli-stmt.fetch.php和http://dev.mysql.com/doc/refman/5.0/en/join.html
您同时使用mysql(mysql_fetch_array)和mysqli,您确定要外部联接吗?一种简单的连接方法就是用一个标识符指定多个表,就像你有(i和t)一样,然后指定它们在哪里连接,就像这样:
SELECT i.idea_id, i.user_id, i.time, i.title, i.Brief, t.location, t.width, t.height, t.colour
FROM idea_thread i, thumb t
WHERE t.idea_id = i.idea_id
ORDER BY i.idea_id DESC
左外部联接将包括所有线程,即使拇指中没有相关的id,也可能是您想要的:
SELECT i.idea_id, i.user_id, i.time, i.title, i.Brief, t.location, t.width, t.height, t.colour
FROM idea_thread i
LEFT OUTER JOIN thumb t ON t.idea_id = i.idea_id
ORDER BY i.idea_id DESC
由于您没有提供任何值,所以不需要使用prepare,只需在mysqli中使用query即可。
更新:注意到SELECT中也有*。这将选择所有列,然后选择您指定的列,从而进一步抛出查询。
所以对于mysqli,你会想要:
$stmt = $mysqli->query(...);
while ($row = $stmt->fetch_array(MYSQLI_NUM)) {
... logic here ...
}
我在fetch_array中使用了MYSQLI_NUM,因为您是按数字引用列的,所以您也可以使用MYSQLI_ASSOC:http://us2.php.net/manual/en/mysqli-result.fetch-array.php