مصطفى سعدي مصطفى سعدي - 5 years ago 258
SQL Question

Method name must be a string fatal error class

How can I fix this code? When I run check.php I get this error:


Erorr : Fatal error: Method name must be a string in C:\AppServ\www\Weboo\cms\check.php on line 46


<?php
ob_start();
session_start();
class mysql {


private $localhost = "localhost";
private $db_user = "root";
private $db_pass = "root";
private $db_name ="webocms";


function __construct(){

mysql_connect($this-> localhost,$this->db_user,$this->db_pass);
mysql_select_db($this->db_name);
}

}

function sql(){

$username = $_POST ['username'];
$password = $_POST['password'];

$sql = "SELECT * from users WHERE username='$username'
AND password='$password'";

$query = mysql_query($sql);

$num = mysql_num_rows($query);

if ($num > 1){

$_SESSION['username'] = "username";
$_SESSION['password'] = "password";

header("Location: admin/admin.php");
}else {

echo "<h2><b> No Users </h2></b>";
}
}

$use=new mysql;
$use->$sql();
ob_end_flush();
?>

Answer Source

I think you've made a mistake with the closing brace of your mysql class. sql() is a function and not a method of the mysql class. You're also using a variable which in my opinion appears unintentional.

Instead it should be $use->sql(); but again that won't work until you put sql() inside the class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download