gpt4 book ai didi

php - 方法名必须是字符串 fatal error 类

转载 作者:行者123 更新时间:2023-11-30 22:28:24 25 4
gpt4 key购买 nike

如何修复此代码?当我运行 check.php 时出现此错误:

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();
?>

最佳答案

正如Mark在评论中所说的,我觉得更应该是这样的:

<?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();
?>

注意,sql() 函数现在在您的类中,倒数第二行是 $use->sql() 而不是 $use->$sql().

关于php - 方法名必须是字符串 fatal error 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34580366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com