【问题标题】:mysql_real_escape_string working perfectly in localhost, not working onlinemysql_real_escape_string 在本地主机中完美运行,无法在线运行
【发布时间】:2015-07-29 14:14:59
【问题描述】:

在页面http://www.immocorbati.be/tehuur.php 上,过滤器功能无法正常工作。只有“Prijsklasse”(价格)有效。但是,如果您过滤“Gemeente”(区域)Boom,例如,它应该返回 1 个家,但它说没有找到。与“Type”工作室相同,应该返回 3 但不返回任何内容,“Slaapkamer”(卧室)3 slaapkamer 应该返回 1 个家,但同样没有显示。

我不明白为什么它在 localhost 上完美运行,而且它在网上运行一半一半,因为这意味着功能已达到。这是文件。也许有人有一些指示为什么会发生这种情况?它可能与数据库相关吗?但是后端等中的所有其他功能都可以正常工作。

class Pand
{

    //DB settings - Don't worry these are correct
    private $m_sHost = "??????";
    private $m_sUser = "????????";
    private $m_sPassword = "??????";
    private $m_sDatabase = "????????";     

    public function GetToonPandenHuurFilter()
    {
    //de panden worden op pagina getoond volgens filter
            if ($link = mysqli_connect($this->m_sHost, $this->m_sUser, $this->m_sPassword, $this->m_sDatabase))
            {

                    $b = $_POST['typewoning'];
                    $p = $_POST['plaats'];
                    $s = $_POST['slaapkamers'];
                    $k = $_POST['prijsklasse'];


                    $sSql = "select * from tblpand WHERE PandVerkoopstype='Te huur' AND PandOnline='Ja'";
                    if ($b !== 'all') {
                            $sSql .= " AND PandBebouwing='" . mysql_real_escape_string($b) . "'";
                    }                      
                    if ($p !== 'all') {
                            $sSql .= " AND PandPostcodeGemeente='" . mysql_real_escape_string($p) . "'";
                    }
                    if ($s !== 'all') {
                            $sSql .= "AND PandSlaapkamers='" . mysql_real_escape_string($s) . "'";
                    }
                    if ($k == '0') {
                            $sSql .= "";
                    }
                    if ($k == '1') {
                            $sSql .= "AND PandPrijs <'" . 301 . "'";
                    }
                    if ($k == '2') {
                            $sSql .= "AND PandPrijs <'" . 501 . "'";
                    }
                    if ($k == '3') {
                            $sSql .= "AND PandPrijs <'" . 701 . "'";
                    }
                    if ($k == '4') {
                            $sSql .= "AND PandPrijs <'" . 701 . "'";
                    }


                    $rResult = mysqli_query($link, $sSql);
                    return $rResult;
            }
            else
            {
                    // er kon geen connectie gelegd worden met de databank
                    throw new Exception('Ooh my, something terrible happened to the database connection'); 
            }      
            mysqli_close($link);
    }
}

我添加了错误报告,反馈说:

mysql_real_escape_string(): Access denied for user ''@'10.246.64.177'
(using password: NO) in /customers/e/a/b/immocorbati.be/httpd.www/classes/Pand.class.php on line 1086

第 1086 行是:

$sSql .= " AND PandBebouwing='" . mysql_real_escape_string($b) . "'";

【问题讨论】:

  • 请进行更多调试。插入调试语句,var_dump 变量,缩小它开始失败的确切位置。在这方面,我们没有比您更好的位置了。
  • 在测试代码时,您需要确保已启用 display_errors。如果服务器上出现问题,PHP 可能会详细说明。在您的脚本顶部error_reporting(E_ALL); ini_set('display_errors', 1); 然后在您部署到实时环境时禁用它。
  • 好的,我就从这个开始。
  • Is it possibe that it is database related - 您正在查询数据库吗?然后确定这是可能的。这是什么$b$p,您可以使用多个字母,如果您将其命名为与您使用的密钥相同的名称,它会更容易阅读,即。 $b = $_POST['typewoning'] 读起来更好,因为 $typewoning = $_POST['typewoning'] 而这些 ` if ($k == '0') {` 应该是 }else if ($k == '1') {
  • 感谢您迄今为止的帮助。如你所料,我当时处于恐慌状态。我添加了错误报告,反馈说:mysql_real_escape_string(): Access denied for user ''@'10.246.64.177' (using password: NO) in /customers/e/a/b/immocorbati.be/httpd.www/classes/Pand.class.php on line 1086 – 第 1086 行是:$sSql .= " AND PandBebouwing='" . mysql_real_escape_string($b) . "'"; 据我所知,连接数据有效,因为它也适用于其他功能并且以相同的方式链接到。

标签: php filter localhost


【解决方案1】:

mysql_real_escape_string 需要数据库连接才能完成工作。 mysql_real_escape_string 属于 mysql API。您正在使用 mysqli 建立连接。 mysql 和 mysqli 是两个完全不同的数据库 API; mysqli 连接对 mysql_* 是无用的。 mysql 有在需要时隐式(试图)建立连接的坏习惯。在您的本地主机上,它尝试建立的隐式连接恰好起作用(默认用户名,无密码等)。在您的主机上,它没有。

在使用 mysqli 时不要使用mysql_real_escape_string。使用mysqli's parameter binding API 或至少mysqli_real_escape_string

【讨论】:

  • 这就是答案。我很高兴我也发现了这一点。愚蠢,但 mysql 和 mysqli 的组合在 localhost 中工作也是非常不合逻辑的。我现在正在实现绑定 API。谢谢。
  • 是的,这是不推荐使用 mysql 且不应触摸的原因之一,即使是十英尺长的杆子也不行。
猜你喜欢
  • 2021-08-11
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2015-12-24
  • 2017-06-11
  • 1970-01-01
  • 2016-09-16
相关资源
最近更新 更多