我有这个类可以在php中建立与MySQL数据库的连接。但它似乎有些问题,显然我犯了一个错误。
这是班级:
class dbConnection {
public $CONN;
public function __construct() {
$dbservername = "localhost";
$dbusername = "test";
$dbpassword = "pass";
$dbname = "dbtest";
// Create connection
$this->CONN = new mysqli($dbservername, $dbusername, $dbpassword, $dbname);
// Check connection
if ($this->CONN->connect_error) {
die("Connection failed: " . $CONN->connect_error);
}
return $this->CONN;
}
public function close() {
$this->CONN->close();
}
}
这是要求班级执行INSERT
:
$conn = new dbConnection();
$stmt = $conn->prepare("INSERT INTO tablename (stuff1, stuff2, stuff3) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $val1, $val2, $val3);
// set parameters and execute
$val1 = "val1";
$val2 = "foo";
$val3 = "bar";
$stmt->execute();
$stmt->close();
显示的错误是:
Fatal error: Uncaught Error: Call to undefined method dbConnection::prepare() in /var/www/index.php:7 Stack trace: #0 {main} thrown in /var/www/index.php on line 7
如果我在类中设置一个函数并调用它而不是构造函数,这是有效的,但我想用构造函数本身完成它。 我如何建立这个?
答案 0 :(得分:1)
无法将自定义和系统函数应用于同一对象,因此您必须镜像自定义对象上的系统函数:
class connect{
public $CONN;
function __construct(){
$this->CONN=new mysqli();
}
function prepare($statement){
$this->CONN->prepare($statement);
}
答案 1 :(得分:1)
您可以为public class SecondActivity extends Activity
{
private String name,email,phone,county;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.main);
//this is first activity data
//you can get firstactivity data in second activity
//and store data into varialbles
name=getIntent().getStringExtra("name");
email=getIntent().getStringExtra("email");
phone=getIntent().getStringExtra("phone");
country=getIntent().getStringExtra("country");
}
public void onResume()
{
super.onResume();
//put this code as per your requirement
// i am just giving idea
Intent i=new Intent(this,ThirdActivity.class);
//this is first activity data put in intent
i.putExtra("name",name);
i.putExtra("email",email);
i.putExtra("phone",phone);
i.putExtra("country",country);
//this is second activity data you also put on this intent
i.putExtra("sex","male");
i.putExtra("age","24");
i.putExtra("city","jaipur");
startActivity(i);
}
}
类创建public class FinalActivity extends Activity
{
private String name,email,phone,county,sex,age,city;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.main);
//this is first activity data and second activity data
//you can get firstactivity and secondactivity data in Final activity
//and store data into varialbles
name=getIntent().getStringExtra("name");
email=getIntent().getStringExtra("email");
phone=getIntent().getStringExtra("phone");
country=getIntent().getStringExtra("country");
sex=getIntent().getStringExtra("sex");
age=getIntent().getStringExtra("age");
city=getIntent().getStringExtra("city");
//all data are in instance variable please use this data whenever
}
public void onResume()
{
super.onResume();
//if you need to send data another activity
//please repeat again previous steps as per your requirement
}
}
方法:
prepare
并且您当前的代码可以使用。在我的机器上测试它确实有效。