php连接数据库的类,但是选择数据库的时候总是出错,怎么回事呢?
<?phpclassregister{private$host;//Thehostaddressprivate$user;//Theuserprivate$pass;//...
<?php
class register{
private $host; //The host address
private $user; //The user
private $pass; //The password
private $database; //The database
//Connect with the database
function __construct(){
$host = $this -> host;
$user = $this -> user;
$pass = $this -> pass;
// $database = $this -> database;
// echo $db;
$conn = mysql_connect($host,$user,$pass);
$db = mysql_select_db($this -> database,$conn);
// if($conn){
// echo "连接成功";
// }else{
// echo "连接失败";
// }
if($db){
echo "数据库成功";
}else{
echo "数据库失败";
}
}
//Add a user
function addUser(){
$dataObj = $_POST["dataObj"];
$userInfo = json_decode($dataObj,true); //change the data from the type of json to array.
$username = $userInfo[0];
$password = $userInfo[1];
$repassword = $userInfo[2];
//The sql to add the new user
@$sql_addUser = <<<EOF
insert into users values("","$username","$password","$repassword");
EOF;
// var_dump($sql_addUser);break;
$result = mysql_query($sql_addUser);
// var_dump($result);break;
if($result){
echo 1;
}else{
echo 0;
}
}
}
// echo addUser();
$register = new register("localhost","root","","hotel");
$register -> addUser();
// break;
?> 展开
class register{
private $host; //The host address
private $user; //The user
private $pass; //The password
private $database; //The database
//Connect with the database
function __construct(){
$host = $this -> host;
$user = $this -> user;
$pass = $this -> pass;
// $database = $this -> database;
// echo $db;
$conn = mysql_connect($host,$user,$pass);
$db = mysql_select_db($this -> database,$conn);
// if($conn){
// echo "连接成功";
// }else{
// echo "连接失败";
// }
if($db){
echo "数据库成功";
}else{
echo "数据库失败";
}
}
//Add a user
function addUser(){
$dataObj = $_POST["dataObj"];
$userInfo = json_decode($dataObj,true); //change the data from the type of json to array.
$username = $userInfo[0];
$password = $userInfo[1];
$repassword = $userInfo[2];
//The sql to add the new user
@$sql_addUser = <<<EOF
insert into users values("","$username","$password","$repassword");
EOF;
// var_dump($sql_addUser);break;
$result = mysql_query($sql_addUser);
// var_dump($result);break;
if($result){
echo 1;
}else{
echo 0;
}
}
}
// echo addUser();
$register = new register("localhost","root","","hotel");
$register -> addUser();
// break;
?> 展开
2个回答
展开全部
构造函数错咯
function __construct($host,$user,$pass,$database){
$this -> host=$host;
$this -> user=$user;
$this -> pass=$pass;
$this -> database=$database;
// echo $db;
$conn = mysql_connect($host,$user,$pass);
$db = mysql_select_db($this -> database,$conn);
if($db){
echo "数据库成功";
}else{
echo "数据库失败";
}
}
//给你改咯哈这个类
class register{
private $host; //The host address
private $user; //The user
private $pass; //The password
private $database; //The database
private $conn;
//Connect with the database
function __construct($host,$user,$pass,$database){
$this -> host=$host;
$this -> user=$user;
$this -> pass=$pass;
$this -> database=$database;
$conn = mysql_connect($host,$user,$pass)or die("连接失败!");
mysql_select_db($this -> database,$conn) or die("选择数据库失败!");
$this->conn=$conn;
}
//Add a user
function addUser($data){
$dataObj = $data;//类里边不要把变量写得太死
$userInfo = json_decode($dataObj,true); //change the data from the type of json to array.
$username = $userInfo[0];
$password = $userInfo[1];
$repassword = $userInfo[2];
//The sql to add the new user
@$sql_addUser = <<<EOF
insert into users values("","$username","$password","$repassword");
EOF;
// var_dump($sql_addUser);break;
$result = mysql_query($sql_addUser,$this->conn);//指定连接,少些麻烦
// var_dump($result);break;
if($result){
echo 1;
}else{
echo 0;
}
}
}
展开全部
$register = new register("localhost","root","","hotel"); //对构造函数传入了4个参数
//定义构造函数时未定义接收传入参数的变量。(括号中是空的)
function __construct(){
$host = $this -> host;
$user = $this -> user;
$pass = $this -> pass;
// $database = $this -> database;
// echo $db;
...
}
//定义构造函数时未定义接收传入参数的变量。(括号中是空的)
function __construct(){
$host = $this -> host;
$user = $this -> user;
$pass = $this -> pass;
// $database = $this -> database;
// echo $db;
...
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询