Php 搜索脚本-列、表名和条件作为参数/变量
抱歉,标题不准确 我想做的是为搜索做一个动态查询。准备好的语句将接收列和表名作为参数,以及可选的条件(其中,和)作为php变量: 我在找这样的工作Php 搜索脚本-列、表名和条件作为参数/变量,php,mysqli,prepared-statement,Php,Mysqli,Prepared Statement,抱歉,标题不准确 我想做的是为搜索做一个动态查询。准备好的语句将接收列和表名作为参数,以及可选的条件(其中,和)作为php变量: 我在找这样的工作 $optional_cond = ' WHERE city = ' . $city . 'AND zone = ' . $zone; // int if ($stmt = $mysqli->prepare("SELECT COUNT(?) as count, ? FROM ?" . $optional_cond) { $stmt-&
$optional_cond = ' WHERE city = ' . $city . 'AND zone = ' . $zone; // int
if ($stmt = $mysqli->prepare("SELECT COUNT(?) as count, ? FROM ?" . $optional_cond) {
$stmt->bind_param('sss', $column, $column, $table);
$stmt->execute();
$row = $stmt->fetch();
}
$total_results = $row['count'] // here i need the count
现在我需要抓取数据:
if (!isset($page_number))
$page_number = (int)$_GET['page'] <= 0 ? 1 : (int)$_GET['page']; // grab the page number
$perpage = 4; // number of elements perpage
if ($page_number > ceil($total_results/$perpage))
$page_number = ceil($total_results/$perpage);
$start = ($page_number - 1) * $perpage;
// here i need the id's one by one
while ($row = $stmt->fetch()) {
$id = $row[$column]; // i think bind_param does not accept arrays
if ($stmt = $mysqli->prepare("SELECT * FROM ? Where id = ? LIMIT ? ?") {
$stmt->bind_param('siii', $table2, $id, $start, $perpage);
$stmt->execute();
}
while ($row = $stmt->fetch()) {
// print the data
}
}
if(!isset($page_number))
$page\u number=(int)$\u GET['page']ceil($total\u results/$perpage))
$page\u number=ceil($total\u results/$perpage);
$start=($page_number-1)*$perpage;
//我需要一个接一个的身份证
而($row=$stmt->fetch()){
$id=$row[$column];//我认为bind_参数不接受数组
如果($stmt=$mysqli->prepare($SELECT*FROM?Where id=?LIMIT??){
$stmt->bind_param('siii',$table2,$id,$start,$perpage);
$stmt->execute();
}
而($row=$stmt->fetch()){
//打印数据
}
}
我不确定这是否是最好的方法,如果有任何建议和改进,我将不胜感激
我将使用ajax发送变量
$city
和$zone
,我是否需要执行其他操作才能工作?(除了检查post是否已设置)。不幸的是,您不能以这种方式在准备好的语句中使用表或列标识符。因此,可能重复,我可以从{$mytable}何处选择*吗
或从“$mytable.”中选择*,其中…
。这是唯一的方法吗?