PHP-MySQL数据库连接

use*_*894 1 php mysql database-connection

我正在用lynda.com的视频学习PHP.我在localhost 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);
?>
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我怎样才能克服这个问题?谢谢大家

小智 5

你犯了一个很小的错误.你在while语句中使用了分号,所以只需将其删除即可.然后它会工作正常.

使用返回的数据:

while($row = mysql_fetch_array($result))
Run Code Online (Sandbox Code Playgroud)

不:

 while($row = mysql_fetch_array($result));
Run Code Online (Sandbox Code Playgroud)