PHP-MySQL数据库连接

标签 php mysql database-connection

我正在通过 lynda.com 的视频学习 PHP。我在本地主机 phpmyadmin 面板中创建了一个名为 widget_corp 的数据库,并编写了这些代码块

    <?php
/* 1.Create a database connection */
$connection = mysql_connect("localhost", "root", "*");
if(!$connection){
die("Database connection failed: " .mysql_error());
}

/* 2. Select a database to use */
$db_select = mysql_select_db("widget_corp", $connection);
if(!$db_select)
{
die("Database selection failed: " . mysql_error());
}
?>

    <html>
    <head>
    <title> Connection To the Database </title>
    </head>
    <body>
    <?php

//3. Perform database query
$result = mysql_query("SELECT * FROM subjects",$connection);
if(!$result)
{
die("Database query failed: " .mysql_error());
}

//4. Use returned data

while($row = mysql_fetch_array($result));
{
echo $row["menu_name"]." ".$row["position"]."<br/>";
}
?>
</body>
</html>
<?php
//5. Close connection
mysql_close($connection);
?>

我总是得到这种错误类型:

Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

Error 404

localhost
Apache/2.4.4 (Unix) PHP/5.5.3 OpenSSL/1.0.1e mod_perl/2.0.8-dev Perl/v5.16.3

我该如何克服这个问题?谢谢大家

最佳答案

你犯了一个非常小的错误。您在 while 语句中使用了分号,因此只需将其删除即可。然后它将正常工作。

使用返回的数据:

while($row = mysql_fetch_array($result))

不是:

 while($row = mysql_fetch_array($result));

关于PHP-MySQL数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20172805/

相关文章:

android - 将 Android 应用程序连接到亚马逊 RDS

php - Laravelcollective html 表单错误不起作用

php - 如何替换文本 URL 并排除 HTML 标记中的 URL?

mysql - 有什么方法可以在 Propel (ORM) 中使用 match against statement 吗?

php - opencart - 如何在模板文件中手动显示模块?

php - 如何防止 PHP 中的 SQL 注入(inject)?

java - 将 jTable 与 MySQL 数据库连接时,java net beans IDE 8.0 中出现数组越界异常

java - 连接 MySQL 数据库时有关 SSL 连接的警告

php - codeigniter - 在 Controller 中使用助手不起作用

php - Elasticsearch和Symfony搜索单词的一部分