/*connection file*/
function connect(){
$server = "localhost";
$user = "xxxx";
$password = "xxxx";
$db = "xxxx";
$connetion = mysqli_connect($server,$user,$password,$db);
}
这是我的连接文件。我使用MVC连接到数据库。
/*function declaration and insert query*/
function insert($table,$value){
$fld = "";
$val = "";
$i = 0;
foreach ($value as $k => $v) {
if($i == 0){
$fld .= $k;
$val .= "'" . $v ."'";
}
else{
$fld .= "," . $k;
$val .= ",'" .$v . "'";
}
$i++;
}
global $conn;
return mysqli_query($conn,"INSERT INTO $table($fld) VALUES($val)") or die(mysqli_error($conn));
}
当我尝试将数据插入数据库时会发出警告。
请帮我解决此警告。
答案 0 :(得分:0)
您需要从函数connect
返回连接对象。
function connect(){
$server = "localhost";
$user = "xxxx";
$password = "xxxx";
$db = "xxxx";
$connection = mysqli_connect($server,$user,$password,$db);
return $connection; // return connection object
}
答案 1 :(得分:0)
您需要返回$connection
,以便global $connection;
中不会定义它:
function connect(){
$server = "localhost";
$user = "xxxx";
$password = "xxxx";
$db = "xxxx";
$connection = mysqli_connect($server,$user,$password,$db);
return $connection; // return $connection
}
注意:您错误地拼写了$connection
,不应该是$connetion
。