mysql_* to MySQLi


mysql_* to MySQLi

我最近得知mysql_*已经贬值,我有一个关于如何重写的快速问题。

$db = mysql_connect("localhost","root","PASSWORD");
if(!$db) die("Error connecting to MySQL database.");
mysql_select_db("FormData" ,$db);

我试过这样重写。。。

$mysqli = new mysqli("localhost", "root", "PASSWORD", "FormData", $db);
if(!$db) die("Error connecting to MySQL database.");

但当它发布我的表单时,我收到了"连接MySQL数据库时出错"的错误。我可以通过使用这个来修复它,但我想知道如何添加错误连接。

$mysqli = new mysqli("localhost", "root", "PASSWORD", "FormData");

任何帮助都将是伟大的,因为我试图学习所有新的MySQLi的东西!

PHP网站

直接从php.net

<?php
$mysqli = new mysqli('localhost', 'fake_user', 'my_password', 'my_db');
// Works as of PHP 5.2.9 and 5.3.0.
if ($mysqli->connect_error) {
    die('Connect Error: ' . $mysqli->connect_error);
}
?>

编辑:

以下内容也将允许您按照自己的方式进行操作。

$mysqli = mysqli_connect('localhost', 'fake_user', 'my_password', 'my_db');

然后你可以:

if (!$mysqli) {
   //handle the error
}

如果可能的话,考虑PDO。他们有点像我。

$mysqli = new mysqli("localhost", "root", "PASSWORD", "FormData", $db);
if(!$db) die("Error connecting to MySQL database.");

应该是

$mysqli = new mysqli("localhost", "root", "PASSWORD", "FormData");
if($mysqli->connect_error) die("Error connecting to MySQL database.");

mysqli()的参数为:

[ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]]

不确定为什么要尝试使用$db变量来设置连接的端口,然后检查端口变量是否为true。。。

为了将来参考,最好先看一下文档

编辑

正如@Touch所指出的,您必须检查是否存在错误,而不仅仅是那个对象存在。已编辑代码以反映这一点。

使用mysqli:

define('DB_HOST', 'localhost');
define('DB_NAME', 'some_database_name');
define('DB_USER', 'some_user');
define('DB_PASS', 'some_password');

$Connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (!$Connection->connect_errno) 
{
  //do your prepared stuffs
}
else
{
  die("Database Connection error:".$Connection->connect_error);
}

或使用PDO

try 
{
  $PDOConnection = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.'', DB_USER, DB_PASS);
  $PDOConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  //do your prepared stuffs
  $PDOConnection = null;
} 
catch(PDOException $e) 
{
  die('ERROR: ' . $e->getMessage());
}

我写了一个名为better_mysqli的类,它扩展了mysqli,使其更易于使用。

以下示例显示了问题的答案,还显示了better_mysqli类的基本用法。您可以在这里查看一个带有大量注释的详细示例:better_mysqli 的详细用法

<?php
  include_once('better_mysqli.php');  // can be obtained from: http://pastebin.com/ATyzLUfK
  // THIS NEXT PART ANSWERS YOUR QUESTION 
  // THIS NEXT PART ANSWERS YOUR QUESTION 
  // THIS NEXT PART ANSWERS YOUR QUESTION 
  // THE ONLY DIFFERENCE IN THE CONNECTION WHEN USING better_mysqli AND mysqli
  // is $mysqli = new better_mysqli(...)  and $mysqli = new mysqli(...) 
  // == Instantiate the mysqli database object (aka open the database) ==
  $mysqli = new better_mysqli('your_server', 'your_user', 'your_pass', 'your_db_name');
  if (mysqli_connect_errno()) {
     error_log(sprintf("Can't connect to MySQL Server. Errorcode: %s'n", mysqli_connect_error()));
     exit;
  }

  // == select example ==
  unless( $sth = $mysqli->select('select * from table1 where col1=? and col2=?', $row, array('col1_placeholder_value', 'col2_placeholder_value'), $debug_level=0, $verbose_debug_output)){
        if($debug_level>0){ echo $verbose_debug_output;}
        // .. do your error handling here
  }
  while($sth->fetch()){
          echo $row['col1'] .', '. $row['col2'] .', and '. $row['col_etc'] .' are accessed like that.<br>';
  }

  // == insert example ==
  $statement = "insert into table1 (col1, col2, date_col, col_etc) values (?, ?, NOW(), ?)";
  unless( $mysqli->insert($statement, array('col1_insert_value', 'col2_insert_value', 'col_etc_value'), $debug_level=0, $verbose_debug_output, $id_of_new_record) ){     
          if($debug_level>0){ echo $verbose_debug_output;}
          // .. do your error handling here
  }

  // == update example ==
  unless($mysqli->update("update table1 set col1=? where col2=?", array('col1_value', 'col2_value'), $debug_level=0, $verbose_debug_output) ){
          if($debug_level>0){ echo $verbose_debug_output;}
          // .. do your error handling here      
  }

  // == delete example ==
  unless( $mysqli->delete("delete from table1 where col1=? where col2=?", array('col1_value', 'col2_value'), $debug_level=0, $verbose_debug_output) ){
          if($debug_level>0){ echo $verbose_debug_output;}
          // .. do your error handling here      
  }

  // in all cases statements are prepared just once and cached so if you reuse any statement the already prepared statement handle is automatically used

?>