Php 只有列名与变量匹配时才打印行列
我有一个从mysql结果生成表的脚本,我的目标是基于一个包含我想要显示的列的名称的变量使列完全动态显示。我已经用以下代码以这种方式生成了表标题:Php 只有列名与变量匹配时才打印行列,php,mysql,Php,Mysql,我有一个从mysql结果生成表的脚本,我的目标是基于一个包含我想要显示的列的名称的变量使列完全动态显示。我已经用以下代码以这种方式生成了表标题: $search_field_names=explode(',', $search_field_names) for($i=0, $count=count($search_field_names);$i<$count;$i++) { echo ('<th>'.$search_field_names[$i].'&l
$search_field_names=explode(',', $search_field_names)
for($i=0, $count=count($search_field_names);$i<$count;$i++) {
echo ('<th>'.$search_field_names[$i].'</th>');
}
然后,第一个代码段将生成一个包含这7个名称的表标题
然后是棘手的部分:存储所有记录的数据库表总共有22列。通过执行与第一个片段完全相同的操作来打印记录,结果将是它打印该表中的前7列,而这不一定是我想要的
下面是我获取记录的方式(显然是在脚本中的上述片段之后):
$query=mysqli\u query($mysqli,“从表中选择*,其中列\u name1='$search\u query'和列\u name2='$search\u query'等…);
while($row=mysqli\u fetch\u assoc($query)){
回声(“”);
对于($i=0,$count=count($search\u field\u names);$iHaider建议的两种解决方案,使用$search\u field\u names变量调整查询:
$query=mysqli_query($mysqli, "SELECT ".implode(",", $search_field_names)." FROM table");
while($row=mysqli_fetch_assoc($query)) {
echo('<tr>');
for($i=0, $count=count($search_field_names);$i<$count;$i++) {
echo ('<td>'.$row[$i].'</td>');
}
echo('</tr>');
}
$query=mysqli\u query($mysqli,“SELECT.”内爆(“,”,$search\u field\u name)。“FROM table”);
while($row=mysqli\u fetch\u assoc($query)){
回声(“”);
对于($i=0,$count=count($search\u field\u names);$i
为什么不更改Select查询,只获取$Search\u field\u names变量中可用的列?因为我的网页的每个用户都将数据存储在数据库的不同列中,所以查询会搜索所有列。但是,正如您所提到的,是否可以为()在搜索查询中循环,所以它只搜索存储在变量中的列?如果可能的话,它可能会解决我的问题。是的,这是一个可能的解决方案,但是我忘记了在我的原始问题中包括查询是SELECT.。从表中,列名称=“$search\u query”。我不理解这个查询。$search\u query
变量?您所说的column\u name
是什么意思?$search\u query是一个来自输入字段的POST变量column\u name只是一个例子。现实世界中的一个例子是:SELECT..from table,firstname像“$search\u query%”和lastname像“$search\u query$”等。我试图实现这个解决方案,但遇到了some错误,一个是它在SELECT中的最后一个内爆变量之后添加了一个额外的变量。绑定数组也存在一些不匹配的问题。非常感谢您的时间,但我的时间表很紧,无法启动并运行此程序,但我将在稍后继续处理此问题。谢谢!另外,我的表确实有问题TAN混合了varchar、datetime和integer列,所以我想我只能求助于我以前的工作解决方案,它将每个可搜索字段打印为自己的输入字段。
$query=mysqli_query($mysqli, "SELECT * FROM table WHERE column_name1='$search_query' AND column_name2='$search_query' etc...);
while($row=mysqli_fetch_assoc($query)) {
echo('<tr>');
for($i=0, $count=count($search_field_names);$i<$count;$i++) {
echo ('<td>'.$row[$i].'</td>');
}
echo('</tr>');
$query=mysqli_query($mysqli, "SELECT ".implode(",", $search_field_names)." FROM table");
while($row=mysqli_fetch_assoc($query)) {
echo('<tr>');
for($i=0, $count=count($search_field_names);$i<$count;$i++) {
echo ('<td>'.$row[$i].'</td>');
}
echo('</tr>');
}
$query=mysqli_query($mysqli, "SELECT * FROM table");
while($row=mysqli_fetch_assoc($query)) {
echo('<tr>');
for($i=0, $count=count($search_field_names);$i<$count;$i++) {
// Using header name as the key to the column
echo ('<td>'.$row[ $search_field_names[$i] ].'</td>');
}
echo('</tr>');
}
<?php
// Assuming you have a $post_array like so, where inputs have the same names as the column names in the database
// $post_array = array('username' => 'T', 'url' => 'http');
// Build constraints of the form column_name LIKE 'value%'
$constraints = array();
foreach ($post_array as $key => $value) {
$constraints[] = "$key LIKE ?";
$post_array[$key] = $value.'%';
}
// Build a query selecting the columns from $post_array, with corresponding constraints
// Note : replace " AND " by " OR " if you want the query to match ANY parameter.
$query_text = "SELECT ".implode(", ", array_keys($post_array))." FROM table WHERE ".implode(" AND ", $constraints);
// Prepare this query, we don't want SQL injections!
$query = $mysqli->prepare($query_text);
$bind = array();
foreach ($post_array as $key => $value) {
// We need to use our bind variables outside the scope of this loop, so we create them in $GLOBALS
$GLOBALS[$key] = $value;
// Array of references to our bind variables.
$bind[] = &$$key;
}
// Binding the parameters : we need as many 's' as there are inputs
$bindtypes = str_repeat('s', count($post_array))
call_user_func_array(array($query, 'bind_param'), array_merge( array($bindtypes), $bind ));
// Binding the retrieved columns
echo call_user_func_array(array($query, 'bind_result'), $bind);
$query->execute();
// Header row
echo '<tr>';
foreach ($post_array as $key => $value) {
echo '<th>'.$key.'</th>';
}
echo '</tr>';
// Result rows
while ($query->fetch()) {
echo('<tr>');
foreach ($post_array as $key => $value) {
echo '<td>', $$key, '</td>';
}
echo('</tr>');
}
?>