如果查询在php中成功,如何提醒用户?

时间:2013-08-22 02:16:41

标签: php mysql sql

我想提醒用户是否成功查询或更新数据库..

adduser.php

<?php
include('sqlconnection.php');

$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$middlename = $_POST['mname'];
$password = $_POST['pword'];
$username = $_POST['uname'];
$gender = $_POST['gender'];
$utype = $_POST['utype'];

$query = "INSERT INTO user (firstname,lastname,middlename,gender) VALUES ('$firstname','$lastname','$middlename','$gender')";   

mysql_query($query);

$result = mysql_query("SELECT id FROM user WHERE firstname = '$firstname'");

$row = mysql_fetch_assoc($result);

mysql_query("INSERT INTO accounts (u_id,username,password,utype) VALUES ('$row["id"]','$username',md5('$password'),$utype);")
?>

我如何提醒用户?或者我应该使用什么来向用户提供一条弹出消息说明其成功?

3 个答案:

答案 0 :(得分:1)

检查受影响的行数,参考http://php.net/manual/en/function.mysql-affected-rows.php

也弃用mysql_*个函数,尝试使用mysqli_*PDO

答案 1 :(得分:0)

将变量分配给mysql_query,例如:

 $result = mysql_query("your query");

 if($result){
 $xxx = "query successful";
 }else{
 $xxx = "query failed";
 }

在hmtl:

 <script>
 alert(<?php echo $xxx;?>);
 </script>

这是一种方式,从这里你可以玩你想做的任何事情


另请注意:自PHP 5.5.0起,不推荐使用Mysql_ *扩展,将来也会删除。相反,应该使用MySQLi或PDO_MySQL扩展。

有用的链接Why shouldn't I use mysql_* functions in PHP

答案 2 :(得分:0)

对于无效查询,

mysql_query()返回false。 mysql_num_rows()返回为select等查询返回的记录数。 mysql_affected_rows()检查受影响的行。

<强> EG:

$result = mysql_query("your query");

if($result){ 
  //query successful but this doesn't mean it returned anything
  //you can now use mysql_num_rows (for select query) to check if something is returned
  if( mysql_num_rows($result) > 0 ){
   //some results were returned
  }

  //for insert, update or delete query you need to check affected rows like this
  if( mysql_affected_rows() > 0 ){
   //some records were updated or deleted
  }
}else {
  echo "There was a problem";
}

Sidenote:从PHP 5.5.0开始,不推荐使用MySQL_*扩展名,将来会被删除。请改用MySQLiPDO