简体   繁体   English

使用数组创建动态MySQL查询-PHP

[英]Create a Dynamic MySQL Query using an Array - PHP

I am trying to run a MySQL query wherey piece is being lookedup using LIKE. 我正在尝试运行一个MySQL查询,其中正在使用LIKE查找其中的一块。

Table Structure: TableExample 表结构:TableExample

id    name     piece   
0     jon      piece0
1     james    piece3
2     sarah    piece6

The snipped I have so far: 到目前为止,我已经被抢断了:

$pieces  = "piece0 piece1";  //variable
$piecearrayexplode = explode(" ", $pieces);
$piece0 = $piecearrayexplode[0];
$piece1 = $piecearrayexplode[1];

$sql = "SELECT * FROM TableExample WHERE piece LIKE '%$piece0%' OR pieces LIKE '%$piece1%'";

The problem I have is that $pieces is a variable and I need $sql to be dynamic and automatically feature the correct number of LIKE statements. 我的问题是$ pieces是一个变量,我需要$ sql是动态的并自动具有正确数量的LIKE语句。

Eg if $pieces = "piece0 piece1 piece2", I want $sql to be: 例如,如果$ pieces =“ piece0 piece1 piece2”,我希望$ sql为:

$sql = "SELECT * FROM TableExample WHERE piece LIKE '%$piecearrayexplode[0]%' OR pieces LIKE '%$piecearrayexplode[1]%' OR pieces LIKE '%$piecearrayexplode[2]%'";

Note: $pieces is always separated by space. 注意:$ pieces总是用空格隔开。

I can do a word count. 我可以数字。

$count = str_word_count($pieces);

I don't know where to go from there. 我不知道从那里去哪里。

I did look at this Create a dynamic mysql query using php variables 我确实看过这个使用php变量创建动态mysql查询

It doesn't seem to be what I'm looking for because the LIKEs are successive and not 1 single statement like WHERE. 这似乎不是我要找的内容,因为LIKE是连续的,而不是像WHERE这样的单个语句。 Am I missing something here? 我在这里想念什么吗?

So build your query dynamically too: 因此,也可以动态构建查询:

$foo = '... list of pieces ...';
$parts = explode(' ', $foo);

$likes = array();
foreach($parts as $part) {
   $likes[] = "piece LIKE '%$part%'";
}

$sql = "SELECT ... WHERE " . implode(' or ', $likes);

But note that this is vulnerable to sql injection attacks . 但是请注意,这很容易受到sql注入攻击

You can itarerate throw array and add pieces one by one to query. 您可以itarerate抛出数组并一一添加查询。 (It isnt tested) (未经测试)

$pieces = "piece0 piece1 piece2";
$piecesArr = explode(" ", $pieces);

$sql = "SELECT * FROM TableExample WHERE piece LIKE"; 

$first = true;
foreach ( $piecesArr as $PA ) {
   if ( $first ) {
     $sql .= " '%$PA%'";
     $first = !$first;
   }
   else $sql .= " OR pieces LIKE '%$PA%'"`;
} 

What I basically did was dynamically generate my results from a view. 我基本上所做的是从视图动态生成结果。 I then populated the drop downs having values of the column names in the view and then pass those variables to the select statement from the view. 然后,我在视图中填充具有列名称值的下拉列表,然后将这些变量从视图传递给select语句。

<?php

error_reporting(0);
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_connection = "localhost";
$database_connection = "xxx";
$username_connection = "root";
$password_connection = "";
$connect = mysql_pconnect($hostname_connection, $username_connection, $password_connection) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_connection,$connect);

$col1=$_POST['col1'];
$col2=$_POST['col2'];
$col3=$_POST['col3'];

$result = mysql_query("SELECT $col1 as column1,$col2 as column2,$col3 as colmun3 FROM variety_view");

if(!$result){
echo "failed";
} else {
echo "Perfecto";

}

echo "<table>"; // start a table tag in the HTML

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo "<tr>
<td>" . $row['column1'] . "</td>
<td>" . $row['column2'] . "</td>
<td>" . $row['colmun3'] . "</td>
</tr>";  //$row['index'] the index here is a field name
}

echo "</table>"; //Close the table in HTML

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form method="Post" action="">
<select name="col1">
<option value="official_name">Variety Name</option>
<option value="localname">Local name</option>
<option value="country">Country</option>
<option value="pedigree">Pedigree</option>
</select> &nbsp;
<select name="col2">
<option value="official_name">Variety Name</option>
<option value="localname">Local name</option>
<option value="country">Country</option>
<option value="pedigree">Pedigree</option>
</select> &nbsp; &nbsp;
<select name="col3">
<option value="official_name">Variety Name</option>
<option value="localname">Local name</option>
<option value="country">Country</option>
<option value="pedigree">Pedigree</option>
</select> <br />

<input type="submit" value="Search" />

</form>
</body>
</html>

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

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