如何通过php回复或打印出select语句的结果?
<?php
$username = "root";
$password = "root";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
<?php
$query="SELECT * FROM mydb.product";
$result=mysql_query($query);
echo "$result";
echo $result;
?>
我现在已经走了这么远,但它仍然没有回报。问题出在我的mysql表中吗?或者它落在其他地方
<?php
$username = "root";
$password = "root";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$sth = $db->prepare("SELECT * FROM mydb.test;");
$sth->execute();
$result = $sth->fetchAll();
?>
<h1> Im here</h1>
<?php
foreach ($result as $row) {
echo "<tr>";
echo "<td>{$row['name']}</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
?>
<h1> Im still here</h1>
感谢所有帮助
答案 0 :(得分:0)
您需要查看结果。如果每行中有3列,那么您的代码将如下所示:
for($i=0;$i<mysql_num_rows($result);$i++) {
$row_array=mysql_fetch_row($result);
echo($row_array[0]." ".$row_array[1]." ".$row_array[2]."<br />");
}
答案 1 :(得分:0)
正如评论中所建议的那样,您必须遍历获取的结果:
$query="SELECT * FROM mydb.product";
$result=mysql_query($query);
// Add these lines:
while ($row = mysql_fetch_assoc($result)) {
var_dump($row);
// each item from the row could be used like this:
// echo $row['field'];
}
可在此处找到更多信息:http://php.net/manual/en/function.mysql-query.php
请注意,mysql_query已被删除,您应该考虑切换到mysqli(http://php.net/manual/en/book.mysqli.php)或PDO(http://php.net/manual/en/ref.pdo-mysql.php)