vs mysql连接配置文件,mysql vs mysqli配置文件和查询

作者在尝试使用mysqli扩展时遇到困扰,对比了不同连接方式(mysql_pconnect vs mysqli_new)。文章建议更新到mysqli对象模型并提供学习资源,同时指出混合使用不同风格代码的问题。
摘要由CSDN通过智能技术生成

I need start using the mysqli extension but I'm finding all kinds of conflicting info depending on how all the info is that I'm trying to use.

For example, my header connects to a 'config.php' file that currently looks like this:

$hostname_em = "localhost";

$database_em = "test";

$username_em = "user";

$password_em = "pass";

$em = mysql_pconnect($hostname_em, $username_em, $password_em) or trigger_error(mysql_error(),E_USER_ERROR);

?>

But when I go to php.net I see that I should be using this but after updating everything I get no database.

$mysqli = new mysqli("localhost", "user", "password", "database");

if ($mysqli->connect_errno) {

echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;

}

echo $mysqli->host_info . "\n";

$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);

if ($mysqli->connect_errno) {

echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;

}

echo $mysqli->host_info . "\n";

?>

I also went through and added an "i" to the following code in my site and again no luck:

mysql_select_db($database_em, $em);

$query_getReview =

"SELECT

reviews.title,

reviews.cover_art,

reviews.blog_entry,

reviews.rating,

reviews.published,

reviews.updated,

artists.artists_name,

contributors.contributors_name,

contributors.contributors_photo,

contributors.contributors_popup,

categories_name

FROM

reviews

JOIN artists ON artists.id = reviews.artistid

JOIN contributors ON contributors.id = reviews.contributorid

JOIN categories ON categories.id = reviews.categoryid

ORDER BY reviews.updated DESC LIMIT 3";

$getReview = mysql_query($query_getReview, $em) or die(mysql_error());

$row_getReview = mysql_fetch_assoc($getReview);

$totalRows_getReview = mysql_num_rows($getReview);

And here's the only place on my display page that even mentions mysql so far:

I did see something at oracle that another stackoverflow answer pointed someone to that updates this stuff automagically, but I have so little code at this point it seems like overkill.

解决方案

Adding an i to any mysql function won't make it a valid mysqli function. Even if such function exists, maybe the parameteres are different. Take a look here http://php.net/manual/en/book.mysqli.php and take some time to check mysqli functions. Maybe try some examples to become familiar with the way things work. I also reccomend you to choose either object oriented code, either procedural. Don't mix them.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值