我想循环一个表,但我给它一个对象而不是资源。资源是什么意思?数组?我应该拿什么?
<?php
$db = new Db();
$select = $db->conn->query("SELECT * FROM tbl_tweets");
$rows = $select->num_rows;
$result = mysql_query("SELECT * FROM tbl_tweets");
$fetch = mysql_fetch_array($result);
echo "<table>";
for ($i=0; $i<$rows; $i++)
{
echo "<table><tr><td>".($fetch['tweet'])."</td></tr>";
}
echo "</table>";
?>
我已经看了很长时间了,尝试了fetch_assoc,但我并没有完全相同。欢迎解释差异。
答案 0 :(得分:0)
代码的前3行不正确。如果您打算使用php mysql函数(我建议使用PDO),首先需要使用以下代码建立与数据库的连接:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
}
然后你的代码中的这一行:
$result = mysql_query("SELECT * FROM tbl_tweets");
然后循环搜索结果:
echo "<table>";
while($fetch= mysql_fetch_assoc($result)) {
echo "<tr><td>".$fetch['tweet']."</td></tr>";
}
echo "</table>";
答案 1 :(得分:0)
像这样解决了。数据库连接在Db类中。
$db = new Db();
$sql = "SELECT * FROM tbl_tweets";
$allTweets = $db->conn->query($sql);
while ($tweet = $allTweets->fetch_assoc())
{
echo $tweet['tweet'] . "</br>";
}
?>