怎么判断mysql_query("delete from test where id='$rand'");删除成功?

loswing 2003-12-04 03:20:08
$query="delete from test where id='$rand'";//$rand随便是什么
$query=mysql_query($query);//这里什么也没删除
if($query)
{
echo "ok";
}
else
{
echo "no";
}
//只要 table test存在不论$rand是什么都输出'ok'
要怎么才可以判断
$query真正执行了?
...全文
189 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
扬帆 2003-12-07
  • 打赏
  • 举报
回复
用$query的结果判断SQL语句是否被执行。
用mysql_affected_rows() 判断被执行,受影响的记录有几条如果大于0就说明检索到,或修改数据库中的信息,否则SQL语句没有找到相关记录。
Jatic 2003-12-06
  • 打赏
  • 举报
回复
$sql="delete from test where id='$rand'";
$rs=mysql_query($sql) or die(mysql_error());
if($rs) echo "ok";
else echo "no";

不要把sql语句与查询返回值用同一个变量

hsboy 2003-12-04
  • 打赏
  • 举报
回复
int mysql_affected_rows ( [resource link_identifier])


mysql_affected_rows() returns the number of rows affected by the last INSERT, UPDATE or DELETE query associated with link_identifier. If the link identifier isn't specified, the last link opened by mysql_connect() is assumed.

mysql_affected_rows() 返回使用link_identifier进行的最后一次Insert, UPDATE或者DELETE查询受影响的行数。如果没有指定link identifier参数,那么默认是最后一个被mysql_connect()打开的连接。这样以来问题就简单了:

if(mysql_affected_rows()>0)
{
//有被删除的记录
}
else
{
//没有被删除的记录
}
id(rand())); SetCookie("session", "$s", time() + 14400); } /* 检查是否有 seesion, 如果没有产生一个 MD5 的唯一 id, 并利用 cookie 存入 $s 中。 并且设置其存在时间为 14400 sec 也就是 4 小时 */ $mysql_link = mysql_connect("127.0.0.1", "root", "test"); if (!($mysql_link)) { echo "连接数据库失败
"; exit; } $mysql_select=mysql_select_db("shopper", $mysql_link); if (!($mysql_select)) { echo "打开数据库失败
"; exit; } /* 购物车 Class */ class Cart { function check_item($table, $session, $product) { $query = "SELECT * FROM $table WHERE session='$session' AND product='$product' "; $result = mysql_query($query); if(!$result) { return 0; } $numRows = mysql_num_rows($result); if($numRows == 0) { return 0; } else { $row = mysql_fetch_object($result); return $row->quantity; } } function add_item($table, $session, $product, $quantity) { $qty = $this->check_item($table, $session, $product); if($qty == 0) { $query = "INSERT INTO $table (session, product, quantity) VALUES "; $query .= "('$session', '$product', '$quantity') "; mysql_query($query); } else { $quantity += $qty; $query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' AND "; $query .= "product='$product' "; mysql_query($query); } } function delete_item($table, $session, $product) { $query = "DELETE FROM $table WHERE session='$session' AND product='$product' "; mysql_query($query); } function modify_quantity($table, $session, $product, $quantity) { $query = "UPDATE $table SET quantity='$quantity' WHERE session='$session' "; $query .= "AND product='$product' "; mysql_query($query); } function clear_cart($table, $session) { $query = "DELETE FROM $table WHERE session='$session' "; mysql_query($query); } function cart_total($table, $session) { $query = "SELECT * FROM $table WHERE session='$session' "; $result = mysql_query($query); if(mysql_num_rows($result) > 0) { while($row = mysql_fetch_object($result)) { $query = "SELECT price FROM inventory WHERE product='$row->product' "; $invResult = mysql_query($query); $row_price = mysql_fetch_object($invResult); $total += ($row_price->price * $row->quantity); } } return $total; } function display_contents($table, $session) { $count = 0; $query = "SELECT * FROM $table WHERE session='$session' ORDER BY id "; $result = mysql_query($query); echo ""; echo ""; echo ""; while($row = mysql_fetch_object($result)) { $query = "SELECT * FROM inventory WHERE product='$row->product' "; $result_inv = mysql_query($query); $row_inventory = mysql_fetch_object($result_inv); $contents["product"][$count] = $row_inventory->product; $contents["price"][$count] = $row_inventory->price; $contents["quantity"][$count] = $row->quantity; $contents["total"][$count] = ($row_inventory->price * $row->quantity); $contents["description"][$count] = $row_inventory->description; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; $count++; } echo "
产品编号产品名称单价购买数量单项小计产品描述
".$row_inventory->id."".$row_inventory->product."".$row_inventory->price."".$row->quantity."".$contents["total"][$count]."".$row_inventory->description."
"; $total = $this->cart_total($table, $session); $contents["final"] = $total; return $contents; } function num_items($table, $session) { $query = "SELECT * FROM $table WHERE session='$session' "; $result = mysql_query($query); $num_rows = mysql_num_rows($result); return $num_rows; } function quant_items($table, $session) { $quant = 0;

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧