-1
Im正在努力处理以下代码。我的最终目标是创建一个简短的div,在我的数据库中显示表格中的信息。但它不工作。我是否也必须连接到特定的数据库?从数据库中选择语句
<html>
<head>
</head>
<body>
<?php
$dbName = 'localhost';
$userName = 'root';
$passWord = 'mysql';
$conn = mysql_connect($dbName, $userName, $passWord);
// Check connection
if (!$conn)
{
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
<div id="displayAuthor">
<?php
$sql_statement = "
SELECT ssn, lastname, firstname
FROM author
ORDER
BY lastname, firstname
";
$result = mysql_query($sql_statement);
$outputDisplay = "";
if(!$result)
{
$outputDisplay .= "Error";
} else
{
$outputDisplay = "<h3> Table author data </h3>";
$outputDisplay .= "<tr><th>SSN</th> <th>Last name </th> <th> First name</th> </tr>";
$numberResults = mysql_num_rows($result);
for ($i=0; $i<$numberResults; $i++)
{
//Dit is een counter van hoeveel rijen het uiteindelijk was.
$row = mysql_fetch_array($result);
$ssn = $row['ssn'];
$lastname = $row['lastname'];
$firstname = $row['firstname'];
$outputDisplay .="<td>".$ssn."</td>";
$outputDisplay .="<td>".$lastname."</td>";
$outputDisplay .="<td>".$firstname."</td>";
$outputDisplay .= "</tr>";
}
$outputDisplay .="</table>";
}
print $outputDisplay;
?>
</div>
</body>
</html>
需要使用mysql_select_db来选择你的数据库。但不建议使用mysql_ *函数。改用pdo或mysqli_ *函数。 – fortune 2014-12-02 11:10:38
扔掉这个并使用[PDO](http://php.net/pdo)或[mysqli](http://php.net/mysqli);然后再问一次。 – 2014-12-02 11:10:40
您可以连接到特定数据库,也可以在指定表格时指定数据库,即查询中的FROM FROM MyDatabase.MyTable – Eilidh 2014-12-02 11:11:06