简体   繁体   中英

Create a Dynamic MySQL Query using an Array - PHP

I am trying to run a MySQL query wherey piece is being lookedup using LIKE.

Table Structure: 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.

Eg if $pieces = "piece0 piece1 piece2", I want $sql to be:

$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.

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

It doesn't seem to be what I'm looking for because the LIKEs are successive and not 1 single statement 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 .

You can itarerate throw array and add pieces one by one to query. (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.

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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