简单的SQL查询不起作用,但它显示连接成功?

时间:2015-10-28 15:01:27

标签: php mysql

好的 - 我一定是大脑失灵了。以下代码显示"成功连接"但数据未显示。我在俯瞰什么?

BTW - 我对数据库名称,表格和字段进行了三重检查 - 所以它们是正确的。

<?php 

// - - - - - - - - - - - - - - - - *
// include("config.php"); 
// - - - - - - - - - - - - - - - - *

$servername = "localhost";
$username = "****";
$password = "****";
$dbname = "****";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
    echo "Connection Successful - ";
}

$sql="SELECT * FROM teams ORDER BY team";
$result=mysql_query($sql);                      // Puts result in a variable 
$count=mysql_num_rows($result);                 // Counts number of rows (records)

while($rows=mysql_fetch_array($result)){
    echo $rows['team']; 
    echo '<br />';   
}
echo "<p>hell yes</p>";


?>

希望这对我来说是一个简单的忽视。

1 个答案:

答案 0 :(得分:1)

您使用mysqli连接和mysql函数。不要混淆了!

http://php.net/manual/en/book.mysql.php

http://php.net/manual/en/book.mysqli.php

使用mysql的示例:

// - - - - - - - - - - - - - - - - *
// include("config.php"); 
// - - - - - - - - - - - - - - - - *

$servername = "localhost";
$username = "****";
$password = "****";
$dbname = "****";

// Create connection
mysql_connect($servername, $username, $password);
mysql_select_db($dbname);

$sql="SELECT * FROM teams ORDER BY team";
$result=mysql_query($sql);                      // Puts result in a variable 
$count=mysql_num_rows($result);                 // Counts number of rows (records)

while($rows=mysql_fetch_array($result)){
    echo $rows['team']; 
    echo '<br />';   
}
echo "<p>hell yes</p>";

但是像安德鲁斯提到的那样,你应该使用mysqli。