php 类速成课程
我正在疯狂地尝试编写我的第一个 php 类; 它应该将我连接到服务器和数据库,但我总是收到此错误:
解析错误:语法错误,意外的',',期望 T_PAAMAYIM_NEKUDOTAYIM in /Applications/XAMPP/xamppfiles/htdocs/classTest/test .php on line 9
提前致谢,这里是代码:
<?php
// include 'DBConnect.php';
class DBConnect
{
function connection($hostname = "localhost", $myname = "root", $pass = ''){
mysql_connect(&hostname, &myname, &pass) or die("Could not connect!"); //Connect to mysql
}
function choose($dbnam){
mysql_select_db(&dbnam) or die("Couldn't find database!"); //fnd specific db
}
}
$a = new DBConnect();
$connect = $a->connection("localhost", "root");
$a->choose('mydb');
?>
I'm going crazy trying to write my first php class;
it is supposed to connect me to server and to a database but I'm always getting this error:
Parse error: syntax error, unexpected ',', expecting T_PAAMAYIM_NEKUDOTAYIM in /Applications/XAMPP/xamppfiles/htdocs/classTest/test.php on line 9
Thanks in advance, here is the code:
<?php
// include 'DBConnect.php';
class DBConnect
{
function connection($hostname = "localhost", $myname = "root", $pass = ''){
mysql_connect(&hostname, &myname, &pass) or die("Could not connect!"); //Connect to mysql
}
function choose($dbnam){
mysql_select_db(&dbnam) or die("Couldn't find database!"); //fnd specific db
}
}
$a = new DBConnect();
$connect = $a->connection("localhost", "root");
$a->choose('mydb');
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你在类中的所有变量上都缺少 $ 。第 9 行应该如下所示:
另外,我不知道为什么你在这里通过引用传递(使用 & 符号),这似乎没有必要。
your missing the $ on all of your variables in the class. line 9 should look like this:
also, I don't know why your passing by reference (using the & sign) here, it seems unnecessary.
要添加其他内容,请尝试使用 PDO。 mysql_* 函数在这一点上是老派的。此外,即使连接函数从不返回任何内容,您也会将对连接的调用分配给变量。
To add to the others, try and go with PDO. mysql_* functions are old school at this point. Also, you're assigning the call to connection to a variable even though the connection function never returns anything.
错误“T_PAAMAYIM_NEKUDOTAYIM”翻译为“::”,双冒号)。您只是缺少变量中的“$”,如上所述。
The error 'T_PAAMAYIM_NEKUDOTAYIM' translates to mean "::", double colon). You're just missing the '$' in your variables, mentioned above.
&
符号后面缺少$
,例如&$hostname
。顺便说一下,
mysql_select_db
行也有同样的问题。You are missing the
$
after the&
signs, e.g.&$hostname
.Same problem at the
mysql_select_db
line, by the way.