mysql_ *到MySQLi

时间:2013-08-21 20:38:57

标签: php mysql 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时,任何帮助都会很棒!

4 个答案:

答案 0 :(得分:3)

PHP website

直接来自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。他们和我很相似。

答案 1 :(得分:2)

$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变量来设置连接的端口,然后检查端口变量是否为真......

将来参考的最佳位置是docs

修改

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

答案 2 :(得分:0)

使用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());
}

答案 3 :(得分:0)

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

以下示例显示了您的问题的答案,并显示了better_mysqli类的基本用法。您可以在此处查看包含大量注释的详细示例:detailed usage of 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


?>