繁体   English   中英

PHP mysql_query无法使用变量

[英]PHP mysql_query not working with variable

数据库结构该图显示了数据库结构
数据库视图显示数据库视图的图像
搜索普通文本时, 查询执行查询运行得很好
查询失败搜索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;
}

Mysql查询现在已经过时,可以使用mysqli查询,也可以使用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';
    }

希望会对您有所帮助。 快乐编码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM