我目前正在学习MySQL和php5。我目前在使用php脚本显示数据时遇到问题。我正在学习使用数据库路径并努力提出数据。
这是我的mysql表
+----+------+------------------------------+
| id | name | path |
+----+------+------------------------------+
| 1 | test | test |
| 2 | 1 | /masonry-practice/flag/1.gif |
| 3 | 2 | /masonry-practice/flag/2.gif |
+----+------+------------------------------+
这是我的PHP代码
<?php
$con=mysqli_connect("localhost","root","password","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM img");
while($row = mysqli_fetch_array($result)) {
echo $row['name'] . " " . $row['path'];
echo "<br>";
}
mysqli_close($con);
?>
答案 0 :(得分:0)
您必须使用它来获取值
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
执行任务的更好方法是
<?php
$con=new mysqli(("localhost","root","password","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = $con->query("SELECT * FROM img");
while($row = $result->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
echo $row['name'] . " " . $row['path'];
echo "<br>";
}
/* free result set */
$result->close();
/* close connection */
$con->close();
?>
答案 1 :(得分:0)
试试这个 并在shell上执行脚本以获得问题
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$conn = New mysqli(SERVER, USER, PASS, NAME);
if (!$conn)
{
die("Database connection failed: " . mysqli_error());
}
$result = $conn->query("SELECT * FROM `img`");
while($row = mysqli_fetch_assoc($result))
{
echo $row['name'] . " " . $row['path'];
echo "<br>";
}
?>