PHP 类问题
我正在尝试构建一个 PHP 类来检查 MySql 中的用户名和密码。 我收到“mysql_query():提供的参数不是 D:\2010Portfolio\football\main.php 第 38 行中的有效 MySQL-Link 资源”数据库有错误:“ 消息。
当我移动时我的 userQuery 代码在我的类之外,仅当它在我的类内时才正常。 我不确定问题是否是我没有在我的类中建立与 mysql 的连接。感谢您的帮助! 这是我的代码
<?php
$userName=$_POST['userName'];
$userPw=$_POST['password'];
class checkUsers {
public $userName;
public $userPw;
function checkUsers($userName='', $userPw=''){
$this->userName=$userName;
$this->userPw=$userPw;
}
function check1(){
if (empty($this->userName) || empty($this->userPw)){
$message="<strong>Please Enter Username and Password</strong>";
}else{
//line 38 is next line
$userQuery=mysql_query("SELECT userName, userPw FROM user WHERE
userName='$this->userName' and userPw='$this->userPw'", $connection);
if (!$userQuery){
die("database has errors: ". mysql_error());
}
if(mysql_num_rows($userQuery)==0){
$message="Please enter valid username and password";
}
}//end empty check
return $message;
}//end check1 method
}//end Class
$checkUser=new checkUsers($userName, $userPw);
echo $checkUser->check1();
?>
,感谢您的帮助!!!!
I am trying to build a PHP class to check the username and password in MySql.
I am getting "mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\2010Portfolio\football\main.php on line 38" database has errors:" message.
When I move my userQuery code out of my Class, it works fine. Only if it is inside my Class.
I am not sure if the problem is that I don't build the connection to mysql inside my Class or not. Thanks for any helps!!
Here is my code
<?php
$userName=$_POST['userName'];
$userPw=$_POST['password'];
class checkUsers {
public $userName;
public $userPw;
function checkUsers($userName='', $userPw=''){
$this->userName=$userName;
$this->userPw=$userPw;
}
function check1(){
if (empty($this->userName) || empty($this->userPw)){
$message="<strong>Please Enter Username and Password</strong>";
}else{
//line 38 is next line
$userQuery=mysql_query("SELECT userName, userPw FROM user WHERE
userName='$this->userName' and userPw='$this->userPw'", $connection);
if (!$userQuery){
die("database has errors: ". mysql_error());
}
if(mysql_num_rows($userQuery)==0){
$message="Please enter valid username and password";
}
}//end empty check
return $message;
}//end check1 method
}//end Class
$checkUser=new checkUsers($userName, $userPw);
echo $checkUser->check1();
?>
I appreciate any helps!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的方法无法看到连接对象。要么在方法定义中使用全局 (
(一般来说是个坏主意),要么尝试使用单例数据库连接(代码示例可以在整个网络上找到)。
The problem is that your method cannot see the connection object. Either use global (
in your method definition (bad idea in general) or try going with a singleton database connection (code samples can be found all over the web).
它将
$connection
解释为check1()
函数中的局部变量。查看变量范围的文档。您可能必须在函数中定义
global $connection
。It's interpreting
$connection
as a local variable in yourcheck1()
function.Review the docs for variable scope. You probably have to define
global $connection
in your function.