为了解释我的问题,首先有一个简单的代码:
public function sql($data) {
if (is_array($data)) {
$cells = $data['cells'];
$from = $data['from'];
$where = $data['where'];
$joins = $data['joins'];
$order_by = $data['order_by'];
$o_type = $data['order_by_type'];
$limit = $data['limit'];
/*****************************/
if ($cells == '') { $cells = "*"; }
if ($where != '') { $where = "where ".$where; }
if ($oredr_by != '') { $order_by = "order by ".$order_by." ".$o_type; }
if ($limit != '') { $limit = "limit ".$limit; }
//
$sql = "select ".$cells." from ".$from." ".$joins." ".$where." ".$order_by." ".$limit;
$run = mysqli_query($_SESSION['con'], $sql);
}else{
$run = mysqli_query($_SESSION['con'], $data);
}
}
当我开始使用这个方法时,我会传递一个多维数组作为参数,如下所示:
$sql = $class->sql([ "from" => "table", "order_by" => "id", "order_by_type" => "asc" ]);
/* This will generate and run this query: select * from table order by id asc */
// Notice that I've only used 3 keys, not the all above.
在Apache server
中,当我只使用数组的一些keys
时,它可以很好地工作,但在XAMPP
中则不行,因为它说我必须通过所有的parameters
(cells, from, where, joins, ...)
,即使它们是empty
。
请帮我解决这个问题,谢谢。
您可以使用isset
来检查是否存在数组键,然后获得它的值,如下所示。
public function sql($data) {
if (is_array($data)) {
$cells = '';
if(isset($data['cells']) {
$cells = $data['cells'];
}
....
/*****************************/
if ($cells == '') { $cells = "*"; }
if ($where != '') { $where = "where ".$where; }
if ($oredrby != '') { $orderby = "order by ".$orderby." ".$od_type; }
if ($limit != '') { $limit = "limit ".$limit; }
$sql = "select ".$cells." from ".$table." ".$joins." ".$where." ".$orderby." ".$limit;
$run = mysqli_query($_SESSION['con'], $sql);
}else{
$run = mysqli_query($_SESSION['con'], $data);
}
}
或者只需在调用此函数之前或在index.php.
error_reporting(1)
问题在于此。
$arr = ["a"];
echo $arr["b"];
您将收到错误通知。
Notice: Undefined index: b
如果你想避免这种情况,就用这种方式吧。
$arr = ["a"];
$arr = ["b"] = "";
echo $arr["b"];
将$from
更改为$table
,您没有$table
变量
$from = $data['from'];
至
$table = $data['from'];
此外,你还有拼写错误最大的拼写错误,很难找到。orderby
和oredrby