如何在MySQL插入语句上用字符串连接PHP变量

时间:2018-07-02 16:33:44

标签: php mysql mysqli

如何将字符串与声明的变量连接起来?

block_id列是外键, 我想插入room_id = Block 2/R1block_id = Block 2

require 'db.php';

$block = 2;

for($i=1;$i <= 50; $i++){

$sql = $mysqli->query("INSERT INTO `room` (`room_id`, `room_type`, `blck_id`) "
        . "VALUES ('Block '.$block.'/R'.$i, 'Double', 'Block '.$block)");

table room image

1 个答案:

答案 0 :(得分:1)

您不需要在查询字符串中添加.

$mysqli->query("INSERT INTO `room` (`room_id`, `room_type`, `blck_id`) "
    . "VALUES ('Block $block/R$i', 'Double', 'Block $block')");

如果要查询$block = 1$i = 2

INSERT INTO `room` (`room_id`, `room_type`, `blck_id`) VALUES ('Block 1/R2', 'Double', 'Block 1')