PHP mysql_query无法使用变量

时间:2018-03-29 06:35:26

标签: php mysql

Database Structure显示数据库结构的图像
Database view 显示数据库视图的图像
Query Execution查询正常文本时查询运行完全正常 Failed query查询无法在搜索unicode文本时获取结果

我面临着一个特殊的问题。我将查询存储在一个变量中,它在运行时构建它,尽管从前一个表单传递了各种搜索参数(准确地说是13个参数)。构造的查询正在查找存储在数据库中的unicode数据。

构造的查询示例,存储在$ query变量中 SELECT *来自订单WHERE Court LIKE'%PBR%'和App_Name LIKE'%काशी%'

查询构造的代码如下:

$mysqli_link= new mysqli("localhost", "root", "", "revenue");
mysqli_set_charset( $mysqli_link, 'utf8');
if($_POST['id_sflag']=='1') 
{
// define the list of fields
$fields = array('CaseNO', 'Yr', 'CaseType', 'Court', 'Dt_Disposal', 'App_Name', 'Res_Name', 'Pet_Cou_Name', 'Res_Cou_Name', 'District', 'Pro_Off_Name', 'Division', 'Tehsil');
$conditions = array();
}

// builds the query
$query = "SELECT * from orders";

// loop through the defined fields
foreach($fields as $field){
// if the field is set and not empty
if(isset($_POST[$field]) && !empty($_POST[$field])) {
// create a new condition while escaping the value inputed by the user (SQL Injection)
 $conditions[] = "$field LIKE '%" . mysqli_real_escape_string($mysqli_link, $_POST[$field]) . "%'";
    }
}
// if there are conditions defined
if(count($conditions) > 0) {
// append the conditions
$query .= " WHERE " . implode (' AND ', $conditions); 
}

echo "$query";

$result = mysqli_query($mysqli_link, $query);
$num_rows=mysqli_num_rows($result);

mysqli_close($mysqli_link);

现在问题开始了:

当我运行上面满足PBR等搜索条件的查询时,它会被执行并找到结果。但是当我包含像app_name这样的通配符搜索参数时,它不会给出任何结果,而表中有数据。如果我复制/粘贴查询并在PHP Myadmin中运行它,我会得到结果。 显示结果的代码如下:

$i=1;
$tot=0;
while($row = mysqli_fetch_array($result)) 
{
$dis=$row['District'];
$div=$row['Division'];
$teh=$row['Tehsil'];
$cnt1=$row['Page_Cnt'];
$dyear=$row['Dis_year'];
$filen=$row['CCY'];
$newfile=$string = str_replace("/","_",$filen);
$newfile1="judgements/"."$dyear"."/"."$newfile".".pdf";
$Appname=$row['App_Name'];
$resname=$row['Res_Name'];
$disposal=$row['Dt_Disposal'];
$officer=$row['Pro_Off_Name'];
$pcnt=$row['Page_Cnt'];
printf("
<tr> 
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s / %s  / %s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\"><a href=\"%s\">View Order</a></td>
</tr>\n",$i,$filen,$Appname,$resname,$disposal,$officer,$dis,$div,$teh,$pcnt,$newfile1);
$tot=$tot+$cnt1;  
$i=$i+1;
}

1 个答案:

答案 0 :(得分:0)

现在使用mysqli查询mysql查询已过时或者可以使用PDO方法。我给了一个mysqli查询示例来从mysql服务器获取数据。

        $sql="SELECT * FROM commentview WHERE postid = $postid order by comid DESC LIMIT $count;";
        $result=mysqli_query($db,$sql);//$db is the connection string
        if(mysqli_num_rows($result)>0){

            while($row = mysqli_fetch_assoc($result))
            {   

                $variable = $row['coulmnname'];//column name as in database
            } 
        }
    else
    {
       echo 'Result not Found';
    }

希望,它会对你有所帮助。快乐的编码..