致命错误:函数名称必须是.. PHP错误中的字符串

发布于 2024-09-04 15:15:19 字数 1120 浏览 2 评论 0原文

您好,我有一个名为 User 的类和一个名为 insertUser() 的方法。

function insertUser($first_name, $last_name, $user_name, $password, $email_address, $group_house_id)
  {
    $first_name = mysql_real_escape_string($first_name);
    $last_name = mysql_real_escape_string($last_name);
    $user_name = mysql_real_escape_string($user_name);
    $password = mysql_real_escape_string($password);
    $email_address = mysql_real_escape_string($email_address);

    $query = "INSERT INTO Users
              (FirstName,LastName,UserName,Password,EmailAddress, GroupHouseID) VALUES
              ('$first_name','$last_name','$user_name','$password','$email_address','$group_house_id')";
    $mysql_query($query);
  }

我这样称呼它:

$newUser = new User();
$newUser->insertUser($first_name, $last_name, $user_name, $email, $password,          $group_house_id);

当我运行代码时,我收到此错误:

Fatal error: Function name must be a string in /Library/WebServer/Documents/ORIOnline/includes/class_lib.php on line 33

有人知道我在做什么吗?另外,这是我第一次尝试 OO PHP。

干杯,

琼斯

Hi I have a class called User and a method called insertUser().

function insertUser($first_name, $last_name, $user_name, $password, $email_address, $group_house_id)
  {
    $first_name = mysql_real_escape_string($first_name);
    $last_name = mysql_real_escape_string($last_name);
    $user_name = mysql_real_escape_string($user_name);
    $password = mysql_real_escape_string($password);
    $email_address = mysql_real_escape_string($email_address);

    $query = "INSERT INTO Users
              (FirstName,LastName,UserName,Password,EmailAddress, GroupHouseID) VALUES
              ('$first_name','$last_name','$user_name','$password','$email_address','$group_house_id')";
    $mysql_query($query);
  }

And I call it like this:

$newUser = new User();
$newUser->insertUser($first_name, $last_name, $user_name, $email, $password,          $group_house_id);

When I run the code I get this error:

Fatal error: Function name must be a string in /Library/WebServer/Documents/ORIOnline/includes/class_lib.php on line 33

Anyone know what I am doing wronly? Also, this is my first attempt at OO PHP.

Cheers,

Jonesy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以为你会在 2024-09-11 15:15:19

$mysql_query($query); => mysql_query($query);。请注意缺少的美元。如果您尝试对变量使用函数调用语法,它会查找名称由变量值指定的函数。在这种情况下,您没有 mysql_query 变量,因此它什么也没有返回,这不是一个字符串,从而给您带来错误。

$mysql_query($query); => mysql_query($query);. Note the missing dollar. If you try to use function call syntax on a variable, it looks for a function with the name given by the value of the variable. In this case, you don't have a mysql_query variable, so it comes back with nothing, which isn't a string, and thus gives you the error.

夏尔 2024-09-11 15:15:19

您在 mysql_query 上有一个杂散的 $。删除它:

mysql_query($query);

You have a stray $ on mysql_query. Remove it:

mysql_query($query);
萌梦深 2024-09-11 15:15:19

对于下一个新手,如何触发/解决此错误

“PHP Fatal error: Function name must be a string in ...”

的另一个示例如下;

假设你有一个关联数组;

$someArray = array ( 
                 "Index1" => "Value1",
                 "Index2" => "Value2",
                 "Index3" => "Value3"
              );

echo $someArray('Index1'); // triggers a fatal  error as above

解决方案:

echo $someArray['Index1']; // <- square brackets - all good now

Just for the next newbie, another example of how this error

" PHP Fatal error: Function name must be a string in ..."

is triggered / and solved is as below;

Say you have an associative array;

$someArray = array ( 
                 "Index1" => "Value1",
                 "Index2" => "Value2",
                 "Index3" => "Value3"
              );

echo $someArray('Index1'); // triggers a fatal  error as above

Solution:

echo $someArray['Index1']; // <- square brackets - all good now
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文