简体   繁体   中英

PHP output array into SELECT mysql

Need to find array, and then run a MYSQL SELECT where array values are present (or not present).

$symbol = "abc";
$sql = "SELECT * FROM around";
$results = $conn->query($sql);
foreach($results as $row) {
$stop = preg_replace("/[0-9]/", "", $row['tip']);
if ($stop ==  $symbol) 
{$sword = $row['tip'];
 }}

So we need $sword to serve as an array in the event that there are multiple outputs. After we have that array, we need to run a mysql query that shows only those that have $sword array.

$query = "
 SELECT * FROM ms WHERE `big` = '$sword'";
 $result = mysql_query( $query );

So then we can do something like:

while ( $row = mysql_fetch_assoc( $result ) ) {
  echo '"time": "' . $row['time'] . '",'; }

Here's the code, converting $sword to an array and using it in your 2nd query:

$symbol = array("abc", "def", "ghi");
$sword = array();
$sql = "SELECT * FROM around";
$results = $conn->query($sql);
foreach($results as $row) {
    $stop = preg_replace("/[0-9]/", "", $row['tip']);
    if (in_array($stop, $symbol)) {
        $sword[] = $row['tip'];
    }
}
if ($sword) {
    $in = '';
    $sep = '';
    foreach ($sword as $s) {
        $in .= "$sep '$s'";
        $sep = ',';
    }
    /* $in now contains a string like "'123abc123', '12abc12', '1abc1'" */
    $query = "
    SELECT * FROM ms WHERE `big` IN ($in)";
    $result = mysql_query( $query );
    while ( $row = mysql_fetch_assoc( $result ) ) {
        echo '"time": "' . $row['time'] . '",'; 
    }
}

Now I'm going to go wash my hands to get all that mysql_query off me... :-)

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