使用PHP连接到MySQL [英] Connecting to mysql using php
问题描述
这是我下面的教程代码,来自OReilly的学习PHP MYSQL和Javascript".
Here is my following tutorial code from "Learning PHP MYSQL and Javascript" by OReilly.
<?php
//require_once = 'login.php';
$db_database = 'publications';
$db_hostname = 'localhost:8888';
$db_username = 'root';
$db_password = 'root';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if(!$db_server) die("Unable to connect to MYSQL: ". mysql_error());
mysql_select_db($db_database)
or die("Unable to connect to database: " . mysql_error());
$query = "SELECT * FROM classics";
$result = $mysql_query($query);
if(!$result) die ("Could not query: " . mysql_error());
$rows = mysql_num_rows($result);
for($j = 0; $j < $rows; ++$j)
{
echo 'Author: ' . mysql_result($result, $j, 'author') . '</br>';
echo 'Title: ' . mysql_result($result, $j, 'title'). '</br>';
echo 'ISBN: '.mysql_result($result, $j, 'isbn').'</br>';
}
?>
但是,尝试连接时出现错误.浏览器说这是500错误(实际上并没有太大帮助).当我删除行$db_username = 'root'; $db_password = 'root';
时,我收到另一个错误消息:无法连接到数据库:用户'@'localhost'对数据库'publications'的访问被拒绝."
However, I'm getting an error when trying to connect. The browser says it's a 500 error (which really isn't that helpful). When I take out the lines $db_username = 'root'; $db_password = 'root';
I get another error saying: "Unable to connect to database: Access denied for user ''@'localhost' to database 'publications'".
任何人都可以帮助诊断问题吗?
Can anyone help diagnose the problem?
谢谢!
推荐答案
更改此内容
$result = $mysql_query($query);
对此
$result = mysql_query($query);
函数名称是mysql_query
而不是$mysql_query
,$
用于变量名.
Function name is mysql_query
not $mysql_query
, $
is used in variable names.
此外,与其尝试立即在浏览器窗口中查看"您的代码,还可以使用PHP命令行.如果您的文件名为foo.php
,则可以执行以下操作:
Also, instead of trying to "see" your code immediately in a browser window you could use a PHP command line instead. If your file is called foo.php
you coudl do:
>> php foo.php
PHP Notice: Undefined variable: mysql_query in /var/www/foo.php on line 17
PHP Fatal error: Function name must be a string in /var/www/foo.php on line 17
如您所见,错误消息非常清楚.由于使用$mysql_query
,它认为mysql_query
是一个变量,但是没有在某处定义这样的变量.
As you see, the error message is pretty clear. Because of using $mysql_query
it thinks that mysql_query
is a variable, but such a variable is not defined somewhere.
这篇关于使用PHP连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!