运行sql DELETE查询但不在DB中执行的php命令

时间:2012-09-08 14:32:39

标签: php mysql html

我在php中编写了这个MYsql DELETE函数

function DeleteProduct($thisCatalog) {

    $connB = new ProductDAO();
    $connB->Connect();
    $pro_query = "DELETE * FROM Ikea WHERE `CatalogNumber` = $thisCatalog";
    $db_result = $connB->ExecSQL($pro_query);

    $html_result = 'Your Product Has Been Deleted! ';

    $connB->Disconnect();
    return $html_result;
}

这是执行推荐

DeleteProduct($CatalogNumber);

1 个答案:

答案 0 :(得分:1)

删除语法与select语法不同(您不选择列名或在其中使用*):

"DELETE FROM Ikea WHERE `CatalogNumber` = $thisCatalog";

应该做的伎俩。

我的测试数据库中的一些例子:

mysql> select * from first;
+------+-------+
| id   | title |
+------+-------+
|    1 | aaaa  |
|    2 | bbbb  |
|    3 | cccc  |
|    4 | NULL  |
|    6 | gggg  |
+------+-------+
5 rows in set (0.00 sec)

mysql> insert into first values (7, 'cccc');
Query OK, 1 row affected (0.01 sec)

mysql> select * from first;
+------+-------+
| id   | title |
+------+-------+
|    1 | aaaa  |
|    2 | bbbb  |
|    3 | cccc  |
|    4 | NULL  |
|    6 | gggg  |
|    7 | cccc  |
+------+-------+
6 rows in set (0.00 sec)

mysql> delete from first where id=7;
Query OK, 1 row affected (0.01 sec)

mysql> select * from first;
+------+-------+
| id   | title |
+------+-------+
|    1 | aaaa  |
|    2 | bbbb  |
|    3 | cccc  |
|    4 | NULL  |
|    6 | gggg  |
+------+-------+
5 rows in set (0.00 sec)