当表有10行时,我的PHP查询返回零结果。实际显示的是PHP表格式代码的一部分。
实际输出:
0) { // output data of each row while($row = mysql_fetch_assoc($result)) { echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "
"; } } else { echo "0 re
sults"; } mysql_close($conn);?
HTML / PHP代码:
<div class="about-text">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$conn = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysql_connect_error());
}
$sql = "SELECT * FROM tablename";
$result = mysql_query($conn, $sql);
if (mysql_num_rows($result) > 0) {
// output data of each row
while($row = mysql_fetch_assoc($result)) {
echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>";
}
} else {
echo "0 results";
}
mysql_close($conn);
?>
</div>
答案 0 :(得分:1)
http://php.net/manual/en/function.mysql-query.php
替换
$result = mysql_query($conn, $sql);
与
$result = mysql_query($sql, $conn);
答案 1 :(得分:0)
将连接资源/实例作为第一个参数传递是您应该使用的mysqli extension的样式,而不是将资源作为第二个参数传递(可选) - 这是旧的和现在的{ {3}}做到了。 mysql_ *将deprecated mysql_* extension。
<?php
$mysqli = mysqli_connect('localhost', 'root', '', 'db');
if ($mysqli->connect_errno) {
die("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
}
$sql = "SELECT `in`,`Name`,`Email` FROM tablename";
$result = mysqli_query($mysqli, $sql);
if ( !$result ) {
die('query failed: '.$mysqli->error);
}
$row = mysqli_fetch_array($result);
if ( !$row ) {
echo '0 results';
}
else {
do {
echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>\r\n";
}
while( NULL!=($row=mysqli_fetch_array($result)) );
}
答案 2 :(得分:0)
您可以使用mysqli extension
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "your_db";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM mytable";
$result = mysqli_query( $conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "in: " . $row["id"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
答案 3 :(得分:0)
试试这段代码。这是你的confic.php文件:
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "database_name";
$connection = mysql_connect($hostname, $username, $password, $dbname);
if (!$connection) {
die('Could not connect: ' . mysql_error());
}
制作新的php文件并包含你的配置文件:
<?php
include('config.php');
$sql = "SELECT * FROM table_name";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
?>
<div class="about-text">
<?php
echo "In: ".$row["in"]."Name: ". $row['Name'].","."Email: ".$row['Email']."</br>";
?>
</div>
<?php
}
}else{
echo "There is no Result!!!!";
}
mysql_close($connection);
我认为这会对你有帮助。