gpt4 book ai didi

PHP类问题

转载 作者:行者123 更新时间:2023-11-29 03:55:30 25 4
gpt4 key购买 nike

我正在使用以下代码,运行时屏幕上没有显示任何内容。

    class ModelBase
{
public $db_server;
public $db_user;
public $db_password;
static $db_conn;

public static $DBSERVER="localhost";
public static $DBUSER="user";
public static $DBPASSWORD="password";
public static $DBNAME="test";

function ModelBase()
{
if(!isset(self::$db_conn))
{
$this->db_server = ModelBase::$DBSERVER;
$this->db_user = ModelBase::$DBUSER;
$this->db_password = ModelBase::$DBPASSWORD;
self::$db_conn = mysql_connect($this->db_server, $this->db_user, $this->db_password) or die(mysql_error(0)." Error handling database connection. ");
mysql_select_db(ModelBase::$DBNAME) or die("Couldn't select database.");

return self::$db_conn;
}
}

static function getConnection()
{
if (!isset(self::$db_conn))
{
self::$db_conn = mysql_connect($this->db_server, $this->db_user, $this->db_password) or die(mysql_error(0)." Error handling database connection. ");
mysql_select_db(ModelBase::$DBNAME) or die("Couldn't select database.");
}
return self::$db_conn;
}
}

我有一个继承了 ModelBase 的类。

<?php
include("ModelBase.php");

?>

<?php
class Lead extends ModelBase
{
public $id;
public $firstname;
public $lastname;
public $phone;
public $email;
public $notes;

public Lead()
{

}

public insert()
{
$con = ModelBase::getConnection();
$query = "insert into test (id, firstname, lastname, phone, email, notes) values('','".$this->firstname."','".$this->lastname."','".$this->phone."','".$this->email."','".$this->notes."')";
$res = mysql_query($query, $con) or die(mysql_error(0)." Error inserting ".$query);

return($res);
}
}
?>

最后我有一个测试文件:

include("Lead.php");

echo("Creating new lead");

$L = new Lead;

echo("Inserting info");

$L->firstname = "Dev";
$L->lastname = "Test";
$L->phone = "8885552222";
$L->email = "dev@gmail.com";
$L->notes = "Test this screen.";

echo($L->insert());

echo("Done.");

我收到以下错误:

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE in /var/www/html/joshk/test/Lead.php on line 15

第 15 行是 public Lead() 函数,我看不出有什么问题。

最佳答案

您缺少 function 关键字。必须是

class Lead extends ModelBase
{
public function Lead()
{

}

public function insert()
{
//....
}
}

关于PHP类问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2134176/

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