簡體   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