1

Thanks. I would like to let user login , 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.

mary_lee
  • 27
  • 4
  • 1. `var` and omitting the the visibility modificators (`public`, `protected`, `private`) is PHP4-style. You should learn how to write code in PHP5. 2. `mysql` is outdated for a while and will be marked as deprecated soon. You should use `MYSQLi`, or `PDO` instead. 3. You should _never_ access any `$_POST`-value directly without validating (same for `$_GET` and such). – KingCrunch Jul 27 '11 at 09:49
  • it still not work, pls look at my code after modified – mary_lee Jul 27 '11 at 09:58
  • This wasn't an answer, that was just a comment about what you should clean up. Did you read the answers below? – KingCrunch Jul 27 '11 at 10:04

5 Answers5

3

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.

JohnP
  • 49,507
  • 13
  • 108
  • 140
0
$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).

Michael J.V.
  • 5,499
  • 1
  • 20
  • 16
0

Try:

{$query="SELECT * FROM test WHERE id='".$this->id."' AND '".$this->pwd".';";
Tim Post
  • 33,371
  • 15
  • 110
  • 174
Marcin Zaluski
  • 687
  • 5
  • 10
0

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

xkeshav
  • 53,360
  • 44
  • 177
  • 245
0

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 .

iskandarm
  • 119
  • 5