开发者

About php classes and my sql problem

Thanks. I would like to let user login开发者_StackOverflow , And i try to do it in oop way

Here is the loginPage.php

<html>
<body>
<?php require_once("connection.php");
$instance=new connection("527442_1","chi1234a","foodil_zxq_1");
if (isset($_POST['s'])) {
    $SecIns=new login($_POST['u'],$_POST['p']);
    echo $SecIns->enter();
}
?>
<form name="fm" method="POST" action="show.php">
User Name :<input type="text" name="u">
<br>
Password: <input type="password" name="p">
<br>
<input type="submit" name="s">
</form>
</body>
</html>

Here is the oop code which i fail Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource

<?php

class connection
{var $user;
var $pass;
var $db;

function __construct($user,$pass,$db){
$this->user=$user;
$this->pass=$pass;
$this->db=$db;
}

function conn(){
$conn=mysql_connect('localhost' , $this->user , $this->pass) or die ("Can't connect server");
mysql_select_db($this->db,$conn) or die ("DB error");
}
}

class login extends connection
{

function __construct($id,$pwd)
{$this->id=$id;
$this->pwd=$pwd;
}

function enter()
{$this->query="SELECT * FROM test WHERE id='".$this->id."' AND pwd='". $this->pwd."';";
$result=mysql_query($this->query,$this->conn) or die ("Error:" . mysql_error());
if (mysql_num_rows($this->result)<1)
return "Not correct user";
else
return "Login success";
}
}
?>

The problem is at the function enter() .Should be the format problem?? But i spend a long time on it . Thanks.


While it's commendable that you want to add organization to your code, adding the methods into a class doesn't automatically make it OOP. I recommend you read up on the basics and then reorganize your classes.

As for your problem,

$result=mysql_query('$this->query','$this->conn') or die( ..
--------------------------^

Those variables you passed are treated as literal strings because you've quoted them. you need to pass it an actual query and a connection object. Just remove the quotes.

Also, I recommend you look into parametrized queries : http://php.net/manual/en/book.pdo.php

EDIT

Looking at your code further, these tips might improve your code

  • Use upper case names for your classes. This makes them easier to read.
  • You're correct in moving your connection to a different class, but your usage is wrong.
  • Even though you've extended your Login class from your Connection class, you don't instantiate a connection object for use inside your Login class even though you refer to it in your code (e.g. - $this->conn)
  • Instantiate your connection and pass it as a parameter for your other classes. Don't extend them from the connection.

    $conn = new Connection($username, $pass, $host, $db); $login = new Login($conn); $login->doLogin($username, $password);

And have a query() method in your connection, so you can call that method from your other classes.


$result=mysql_query('$this->query','$this->conn') or die ("Error:" . mysql_error());

This piece of code does not evaluate to PHP stored variables. If you single-quote something, PHP won't attempt to parse it as if it was a variable. Remove the quotes.

After you do that, your code will fail at connection::conn() since there is no function mysql_conn. There is mysql_connect. And it takes arguments in different order (server is first for example, not 3rd argument).


Try:

{$query="SELECT * FROM test WHERE id='".$this->id."' AND '".$this->pwd".';";


you have missed something ; i write collectively from all answers

1 - mysql_conn() is not a function you have written in function conn() {}
2 - remove ' from $result = mysql_query('$this->query','$this->conn')
3 - you have missed one parameter in mysql statement

    $query="SELECT * FROM test WHERE id='$this->id' AND        '$this->pwd';";
                                                         ^ (here)

4 - assign the query in $this->query instead of just $query


you did not passed the connection object to the login object , thats why you have this error : Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource . from where the login object is calling the $db , you are using resources in the login class without initiating them ... they are not exist until you set them .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜