2

最近、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

4 に答える 4

0

mysqliを拡張して使いやすくする、 better_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


?>
于 2013-08-21T21:03:26.560 に答える