Optimize while and SQL in foreach

前端 未结 1 1046
别跟我提以往
别跟我提以往 2021-01-29 15:01

My code :

$text = \'12-name-y-86\';
$array = explode(\'-\',$text);
foreach($array as $value) {
    $sql = mysql_query(\"SELECT * FROM `table` WHERE `pid`=\'$valu         


        
1条回答
  •  一生所求
    2021-01-29 15:11

    Try to select all records in just one query, like this:

    $text = '12-name-y-86';
    $array = explode('-', $text);
    $array = "'" . implode(',', $array) . "'";
    $sql = mysql_query("SELECT * FROM `table` WHERE `pid` IN (' . $array . ') ORDER BY `id` LIMIT 3");
    while($row = mysql_fetch_array($sql)) {
        echo $row['title'];
        echo '
    '; } echo '

    ';

    0 讨论(0)
提交回复
热议问题