• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

动态while循环MySql查询

php 来源:M-n 4次浏览

我将如何缩短这个MySQL查询在PHP中的使用?动态while循环MySql查询

<?php 
    $sql = "SELECT * FROM $this->usernameid where 
    name LIKE '%$term%' OR 
    manufacture1 LIKE '%$term%' OR 
    manufacture2 LIKE '%$term%' OR 
    manufacture3 LIKE '%$term%' OR 
    manufacture4 LIKE '%$term%' OR 
    manufacture5 LIKE '%$term%' OR 
    manufacture6 LIKE '%$term%' OR 
    manufacture7 LIKE '%$term%' OR 
    manufacture8 LIKE '%$term%' OR 
    manufacture9 LIKE '%$term%' OR 
    manufacture10 LIKE '%$term%' 
     ORDER BY $order1"; 
    ?> 

希望做一个while循环,作为一个例子这里是该计划的另一部分我的$ _ POST。

<?php 

    $i = 1; 
    while ($i < 10) { 
     $manufacture[$i] = strtoupper(filterinput($_POST['manufacture' . $i])); 
     $i++; 
    }; 
    ?> 


===========解决方案如下:

// Base query 
$baseQuery = "SELECT * FROM $this->usernameid WHERE name LIKE '%$term%' "; 

// Get all columns from the table with manufacturers 
$manufacturers = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA " 
       . "WHERE TABLE_NAME = '{YOUR_TABLE}' " 
       . "AND COLUMN_NAME LIKE 'manufacturer%';" 
// Execute query 
$getManufacturers = mysql_query($manufacturers); 
$addWhereClause = ''; // Additional WHERE clauses 
// Loop over all the columns 'LIKE manufacturer%' 
while ($manu = mysql_fetch_rows($getManufacturers)) { 
    // Add an additional clause for every manufacturer 
    $addWhereClause .= "OR $manu LIKE '%$term% "; 
} 

// Append everything together and you have your dynamic query. 
$query = $baseQuery.$addWhereClause."ORDER BY $order1;"; 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)