【问题标题】:Problems getting PDO (or mysqli extension) to run queries on database让 PDO(或 mysqli 扩展)在数据库上运行查询时出现问题
【发布时间】:2015-02-25 18:42:17
【问题描述】:

主要问题是过去一周我无法让我的脚本运行查询。

我正在运行 wamp apache 2.4、php 5.5 和 mysql 5.6

  if ($flag == true) {

    //try to connect to db with given parameters
    // minimal validation to make sure no input was sent in error so now i can begin code to connect to database 
    // using the pdo php data object extension

    print 'all input validated';

    $host= "127.0.0.1";
    $db_name ="theturnbuckle";
    $port= "3306";
    $dbusername= 'root';
    $dbpassword= '';

    //registration details including host, port and database name
    $dsn = "mysql:host=$host;port=$port;dbname=$db_name;";

    //error handling for connection
    try {
       $db = new PDO($dsn, $dbusername, $dbpassword);
       //if no proplems flag a variable for connection status which is to be used later.
       $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
       $db->setFetchMode(PDO::FETCH_ASSOC);


       //first query to test that the username is not in the database.
       $username = $_POST['username'];
       $userquery = "SELECT username  FROM registation_view WHERE username = :username" ; 


       //second query to test for existing email
       $email = $_POST['email'];
       $emailquery = "SELECT email FROM registation_view WHERE email = :email";

       //preparing statement, binding html input to variables to be used in the prepared statement.
       $db = new PDO($dsn, $dbusername, $dbpassword);
       $stmt = $db->prepare($userquery);
       $stmt->bindParam(':username', $username, PDO::PARAM_STR, 15);

        //execute the query in variable
        $stmt->execute($userquery);
        if ($stmt->fetchColumn() > 0){
            print 'Sorry ' .$username . 'is already in use please try another';
        }//else the username is not is use as fetchColumn is null or the if is false
        else {$registerprocess === true;}


        $db = new PDO($dsn, $dbusername, $dbpassword);  
        $stmt = $db->prepare($emailquery);
        $stmt->bindParam(':email', $email, PDO::PARAM_STR, 15);

        $stmt->execute($emailquery);
         if ($stmt->fetchColumn() > 0){
             print 'Sorry ' .$_POST['email'] . 'is already in use please try another';
             $registerprocess === false;
         }//else the email is not is use as fetchColumn is not > 0 so the registration proceeds.
         else {$registerprocess === true;}

         // catch (PDOException $e){
         //die("Unable to run query: " . $e->getMessage());}


         if ($registerprocess === true) {       
              //username and email are not on record so i am free to INSERT data into database
              $query = "INSERT INTO registration_view (username, password, email, gender, date_of_birth, joindate)
              VALUES (?, ?, ?, ?, ?, ?)";

              $joindate = now();
              $date_of_birth = $_POST['day'] . $_POST['month'] . $_POST['year'];

              $stmt->prepare($query);
              $stmt->bindParam(1, $_POST['username'], PDO::PARAM_STR, 20) ;
              $stmt->bindParam(2, $_POST['password'], PDO::PARAM_STR, 20) ;
              $stmt->bindParam(3, $_POST['email'], PDO::PARAM_STR, 20) ;
              $stmt->bindParam(4, $_POST['gender']) ;
              $stmt->bindParam(5, $date_of_birth) ;
              $stmt->bindParam(6, $joindate) ;

              $stmt->execute();

              print ' Welcome ' .$username . 'you are now registered';
       }
    } catch (PDOException $e) {
        die("Unable to connect: " . $e->getMessage());
    }

}//INPUT VALIDATION IF

这是我的代码,第一个标志是在数据验证成功后继续,并且打印“验证”;证实了这一点。

我确定 $dsn 没有错误,因为以前用单引号括起来时会显示错误,现在不会显示此类错误。

虽然我对代码混乱持开放态度,但我也一直在检查 php.ini my.ini 和 apache.config,但据说一周没有运气。

我唯一可以添加的任何帮助(或至少可能是潜在客户)是 apache 和 php 错误日志的一些错误。

当我尝试从本地主机运行脚本时,Apache 会显示此内容。

[Mon Dec 29 12:49:13.855653 2014] [authz_core:error] [pid 3632:tid 964] [client ::1:49206] AH01630: client denied by server configuration: C:/Apache24, referer: http://localhost/theturnbuckle/

并且 php 显示这个,虽然我在安装之前和之前重新安装了 apche 服务器,但我将 php_intl.dll 和 phpldap.dll 移动到 wamp 中的另一个文件夹,这些错误就消失了。 (甚至不确定这是否是最佳做法)

[24-Dec-2014 12:00:37 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.5.12/ext/php_intl.dll' - The specified module could not be found.

在第 0 行的未知中

[24-Dec-2014 12:00:37 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.5.12/ext/php_ldap.dll' - The specified module could not be found.

在第 0 行的未知中

我取消了一些 mysql 服务器变量的注释。

innodb_data_home_dir = C:\mysql\data/
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = C:\mysql\data/
innodb_log_arch_dir = C:\mysql\data/

最后我检查了一些 php.ini 服务器变量来尝试帮助解决这种情况,显示错误已打开。

php_pdo_mysql 和 php_mysqli 等所有 php 扩展似乎都安装了。

但在一天结束的时候,我在黑暗中挣扎。

【问题讨论】:

  • 可能是愚蠢的问题:文件c:/wamp/bin/php/php5.5.12/ext/php_intl.dllc:/wamp/bin/php/php5.5.12/ext/php_ldap.dll 存在吗?
  • 你能确认mysqlnd已经安装了吗?
  • registation_view 有错字吗?正确的拼写应该是registration_view
  • 客户端 API 版本 mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $ 这在 phpinfo 中显示,但我会进一步研究。
  • php_intl.dll 和 php_ldap.dll 扩展现在似乎找到了。错字已修复,仍在研究 mysqlnd。

标签: php mysql apache pdo mysqli


【解决方案1】:

你有两个警告:

[24-Dec-2014 12:00:37 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.5.12/ext/php_intl.dll' - The specified module could not be found.

[24-Dec-2014 12:00:37 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.5.12/ext/php_ldap.dll' - The specified module could not be found.

所以只需安装或启用这些扩展。

【讨论】:

  • 在 php.ini 中启用了扩展,并且文件位于错误消息中所述的目录中。有人说也许他们没有被发现并将它们移动到上面的目录中,php5.5.12 目录,这在我前一阵子重新安装 wamp 之前已经摆脱了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-22
  • 2015-09-03
  • 1970-01-01
相关资源
最近更新 更多