我有鳕鱼的这一部分:
我如何能够阻止这一部分
for($i=0; $i<$n-1 ; $i++) {
mysqli_query($con,"INSERT INTO text (ID, TXT)
VALUES ('$id', '$sir[$i]')");
$id++;
}
如果表格不空?
答案 0 :(得分:3)
做一个
SELECT COUNT(*) AS cnt FROM text
并检查您有多少条目。如果cnt> 0,则在循环开始时放置一个中断。
答案 1 :(得分:1)
您需要在特定区块之前添加:
// count the rows
$res = mysqli_query($con, "SELECT COUNT(ID) as numrows FROM text");
// fetch the result
$data=mysql_fetch_assoc($res);
// check if count = 0
if($data['numrows'] == 0) {
// execute the codeblock
for($i=0; $i<$n-1 ; $i++) {
mysqli_query($con,"INSERT INTO text (ID, TXT)
VALUES ('$id', '$sir[$i]')");
$id++;
}
}
答案 2 :(得分:1)
如果表格为空,如果插入如下?
INSERT INTO my_table (colname)
SELECT 'foo'
WHERE NOT EXISTS (SELECT * FROM my_table)
答案 3 :(得分:1)
只需选择一行,看看会发生什么......
if (!($result = mysqli_query($con, "SELECT ID FROM text LIMIT 1")) || $result->num_rows < 1) {
if($result) {
$result->close();
}
for ($i = 0; $i < $n - 1; $i++) {
mysqli_query($con, "INSERT INTO text (ID, TXT)
VALUES ('$id', '$sir[$i]')");
$id++;
}
}