PDO PHP __construct()警告?

时间:2015-10-05 03:07:48

标签: php mysql pdo

我有什么

// Make an attempt to connect to the database
    try {
        // Make our connection
        $DataBaseHandle = new PDO($Host,$Username,$Password,$Table);

            $DataBaseHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES, false); 
                echo 'Connected!<br/>';

    // This will give us a output as an average 
    $SQLQuery = "SELECT 
                CONCAT('$',TRUNCATE(AVG(`payper`),2)) AS `Average Pay`
                FROM `rep_commission`
                ";

foreach ($DataBaseHandle->query($SQLQuery) as $row)
    {
    echo $row["Average Pay"];
    }

    $DataBaseHandle = null;
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    die();
    }

我想做什么

我正在重写一堆我的MYSQL_文件,因为我们都知道它们已被弃用。我也在努力防止咧嘴的注射可能性。脚本将在员工仪表板后面,但是如果我们有一个有恶意的聪明员工,我们会采取额外的措施。

请阅读Are PDO prepared statements sufficient to prevent SQL injection?

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);可以解决问题???

我也在学习:D

我很好奇我出错了,因为我得到了一个

E_WARNING : type 2 --
PDO::__construct() expects parameter 4 to be array, string given 
-- at line 11

E_ERROR : type 1 --
Call to a member function setAttribute() on a non-object
 -- at line 12

我哪里出错了?

另外,如果有人可以告诉我如何使用上面的代码执行var_dump()。我很难理解如何正确地做到这一点。试着阅读但却迷失了:(

1 个答案:

答案 0 :(得分:0)

我需要改变 从

$Table = 'dbname=tablename';
$Host ='mysql:host=$Localhost';
$Localhost='localhost';
$Username='';
$Password='';

$Host ="mysql:host=$Localhost;DBName=database";
$Username='test';
$Password='';

和 从

 $DataBaseHandle = new PDO($Host,$Username,$Password,$Table);

$DataBaseHandle = new PDO($Host,$Username,$Password);