此查询和显示信息是否都正确?语法,明智的。
<?php
mysql_connect("HOST", "USERNAME", "PASSWORD") or die (mysql_error ());
mysql_select_db("DATABASENAME?!?!") or die(mysql_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysql_query($strSQL) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['COLUMNNAME'] . "<br />";
}
mysql_close()
?>
答案 0 :(得分:0)
将语句用作
<?php
$conn=mysqli_connect("HOST", "USERNAME", "PASSWORD") or die (mysqli_error ());
mysqli_select_db("DATABASENAME",$conn) or die(mysqli_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysqli_query($strSQL) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
echo $row['COLUMNNAME'] . "<br />";
}
mysqli_close()
?>
另外,我建议您使用Mysqli或Pdo而不是Mysql驱动程序,因为它已被弃用。
答案 1 :(得分:0)
您的问题或代码中没有问题。语法明智它很好。但建议不要使用mysql_*
函数,因为它已被弃用。
根据您的条件使用代码的一般方法是:
<?php
mysqli_connect("HOST", "USERNAME", "PASSWORD") or die (mysqli_error());
mysqli_select_db("DATABASENAME") or die(mysqli_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysqli_query($strSQL) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
foreach ($row as $column => $value)
echo $column . " = " . $value . "<br />";
echo "<br/>";
}
mysqli_close();
?>
所以,这个迭代并假设你有四列和两行,它给出了如下输出:
Column 1 Name = Value
Column 2 Name = Value
Column 3 Name = Value
Column 4 Name = Value
Column 1 Name = Value
Column 2 Name = Value
Column 3 Name = Value
Column 4 Name = Value