【问题标题】:How do I correctly connect AND query MySQL using MySQLi?如何使用 MySQLi 正确连接和查询 MySQL?
【发布时间】:2015-12-18 11:06:29
【问题描述】:

我开始将使用 Mysql 的网站转换为使用 Mysqli,但遇到了一些问题。 有人可以帮助我如何使用 Mysqli 正确连接和查询 Mysql。

1) 以下是我网站上的 .Error 警告消息:

a) 警告:mysql_query():第 3556 行 /myside/xxxx/xxxxx/functions.php 中用户“localhost”的访问被拒绝(使用密码:否)

b) 警告:mysql_query():无法在第 3556 行的 /myside/xxxx/xxxxx/functions.php 中建立到服务器的链接

c) 警告:mysql_results() 期望参数 1 是资源,布尔值在 /myside/xxxx/xxxxx/functions.php 第 3558 行给出

2) MYSQL Query 函数代码,我用于从第 3544 行到第 3558 行:

// Get Custom Post Type post count
function get_stats($uid,$type,$sub_type = "all") {
if ($type == "reviews") {
    $result = mysql_query("SELECT COUNT(*) FROM wp_comments WHERE user_id =    $uid AND comment_approved = 1");
} elseif ($type == "listings") {
    if ($sub_type == "all") {
        $post_types =   get_post_types_custom(unserialize(EXCLUDED_CUSTOM_POST_TYPES));
    } else {
        $post_types = "'".$sub_type."'";
    }
    $result = mysql_query("SELECT COUNT(*) FROM wp_posts WHERE post_author = $uid AND post_type IN($post_types) AND post_status = 'publish'");
} else {
    $result = mysql_query("SELECT COUNT(*) FROM wp_posts WHERE post_author = $uid AND post_type = '$type' AND post_status = 'publish'");
}
return mysql_result($result,0);

非常感谢!

【问题讨论】:

  • 我没有看到任何错误信息?!警告与错误不同...另外,我在您提供的代码中看不到任何 mysqli ..
  • 不要混合mysql_*和mysqli_*`

标签: php mysql mysqli


【解决方案1】:

火影忍者 - 啊,是的,你是对的。它们是警告消息而不是错误,感谢您指出 - 我将更新我的初始帖子。对我来说,解决这些警告的最佳方式是什么,因为它们目前公开显示在前端供用户查看,给人的印象是网站“已损坏”。

感谢 Jens 的反馈。想详细说明一下吗?我对此很陌生,正在尝试“修复”我刚刚继承并需要尽可能多的手的代码。 “不要混合使用 mysql_* 和 mysqli_*`” ...我在共享的代码中是否将它们混合在一起?

【讨论】:

    猜你喜欢
    • 2017-08-18
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多