在mysql数据库上注册后的PHP echo ID

时间:2015-06-30 17:56:24

标签: php mysql

大家好我有这个PHP代码在我正在开发的游戏中注册建筑数据。这段代码工作正常。

我想知道的是,当寄存器功能成功时,如何回显我使用此代码注册的对象的自动增加ID。

<?php


$db = "database";//Your database name
$dbu = "username";//Your database username
$dbp = "password";//Your database users' password
$host = "localhost";//MySQL server - usually localhost

$dblink = mysql_connect($host,$dbu,$dbp);
$seldb = mysql_select_db($db);

if(isset($_GET['oid']) ){

     //Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks
     $name = strip_tags(mysql_real_escape_string($_GET['oid']));
     $sql = mysql_query("INSERT INTO `$db`.`building` (`id`,`oid`) VALUES ('','$oid');");

     if($sql){

          //The query returned true echo the newly registered id
          echo '????'; 

     }else{

          //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
          echo 'Fail to save data';

     }

}else{
     echo 'Fail-No object owner ID';
}

mysql_close($dblink);//Close off the MySQL connection to save resources.
?>

请帮帮我。

1 个答案:

答案 0 :(得分:0)

使用mysql_insert_id()

if($sql){

          //The query returned true echo the newly registered id
          echo mysql_insert_id($dblink); 

     }else{

          //The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
          echo 'Fail to save data';

     }

说明:

以下是为什么你不应该使用Mysql而是mysqli扩展为php的原因 MySQL vs MySQLi when using PHP

来自PHP开发人员:

  

建议使用mysqli或PDO_MySQL扩展。建议不要将旧的mysql扩展用于新开发。下面提供了详细的特征比较矩阵。所有三个扩展的整体性能被认为是大致相同的。虽然扩展的性能只占PHP Web请求总运行时间的一小部分。通常,影响低至0.1%。