mysql_ *到MySQLi [英] mysql_* to MySQLi
问题描述
我最近了解到mysql_ *已被贬值,我对如何重写某些东西有一个快速的疑问.
I recently learned that mysql_* has been depreciated and i have a quick question on how to rewrite something.
$db = mysql_connect("localhost","root","PASSWORD");
if(!$db) die("Error connecting to MySQL database.");
mysql_select_db("FormData" ,$db);
我尝试过这样重写它...
I have tried rewriting it like this...
$mysqli = new mysqli("localhost", "root", "PASSWORD", "FormData", $db);
if(!$db) die("Error connecting to MySQL database.");
但是当它发布我的表单时,出现连接到MySQL数据库时出错".错误.我只用这个就可以修复它,但是我想知道如何在Error连接中添加它.
But when it posts my form i get the "Error connecting to MySQL database." error. I was able to fix it by just using this but i wanted to know how to add in the Error connecting.
$mysqli = new mysqli("localhost", "root", "PASSWORD", "FormData");
当我尝试学习所有新的MySQLi知识时,任何帮助都是很棒的!
Any help would be great as i try to learn all of the new MySQLi stuff!
推荐答案
直接从php.net
Straight from 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);
}
?>
下面的内容也将允许您按照自己的方式进行操作.
The below will also allow you to do it your way.
$mysqli = mysqli_connect('localhost', 'fake_user', 'my_password', 'my_db');
然后您可以:
if (!$mysqli) {
//handle the error
}
请尽可能考虑PDO.他们和我很相似.
Consider PDO if possible. They are kind similar to me.
这篇关于mysql_ *到MySQLi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!