php - 出现 'Class PDO not found' 错误

标签 php mysql pdo

<分区>

我有一个简单的“即将推出”页面,我在该页面上订阅了我打算插入到 mysql 数据库中的电子邮件。

我之前运行过代码,但现在 1-2 周后回过头来,似乎出现了一些问题。

基本上只涉及2个文件:index.htmlsubscribe.phpindex.html 是实际的“即将推出”页面,它调用 subscribe.php 将电子邮件实际插入数据库,前提是它是有效的电子邮件,不是重复的等..

subscribe.php 的代码如下。它真的是一个非常简单的代码。

请不要认为这之前有效!但是现在似乎在使用 PDO 的行出现了“未找到类 PDO ...”错误:

<?php

function isValidEmail( $email = null )
{
    return preg_match( "/^
    [\d\w\/+!=#|$?%{^&}*`'~-]
    [\d\w\/\.+!=#|$?%{^&}*`'~-]*@
    [A-Z0-9]
    [A-Z0-9.-]{0,61}
    [A-Z0-9]\.
    [A-Z]{2,6}$/ix", $email );
}

try {
    // Connect to the SQLite Database.
    $db = new PDO('mysql:host=hostnamehere;dbname=dbnamehere', 'usernamehere', 'passwordhere');
} catch(Exception $e) {
    die('connection_unsuccessful');
}

/* Check if table exists */
$db->exec('CREATE TABLE IF NOT EXISTS subscribers (email VARCHAR(255), time VARCHAR(255))');

/* Check if email has been posted */
if ( isset($_POST['email']) ) {

    /* Validate email */
    if ( isValidEmail($_POST['email']) ) {

        /* Check for duplication */
        $query = $db->prepare('SELECT COUNT(*) AS count FROM subscribers WHERE email = :email');  
        $query->execute(array(':email' => $_POST['email']));
        $result = $query->fetch();

        if ( $result['count'] == 0 ) { // E-mail is unique.

            $query = $db->prepare('INSERT INTO subscribers (email, time) VALUES (:email, :time)');  
            $query->execute(array('email' => $_POST['email'], 'time' => date('Y-m-d H:i:s')));

            /* Send mail notification */
            $to = 'newsubscriber@xyz.com'; // Email notified of the new subscription
            $subject = 'New subscriber';
            $message = 'Hi, you have one new subscriber. This is his/her e-mail address: ' . $_POST['email'] . '.';
            $headers = "From:" . $_POST['email'];
            mail($to,$subject,$message,$headers);

            echo 'successful';

        } else { // E-mail is already being used.
            echo 'already_subscribed';
        }

    } else {
        echo 'invalid_email';
    }

}

最佳答案

可能 PDO 在您的 php.ini 中被禁用,只需启用它即可。确保你在那里

extension=pdo.so
extension=pdo_mysql.so

关于php - 出现 'Class PDO not found' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16617937/

相关文章:

php - 禁用文件的 url 访问

java - java中的随机数

php - PDO 准备 NOT IN 或 <> 多语言

php - 使用 jQuery/PHP/AJAX/JSON 进行 Lucene/Solr 即时开发

php - 使用发布按钮更新表行值

MySQL 在结果集中添加缺失的月份

php - 添加新行时,mysql 表中的主索引是否自动具有值?

php - 如何使用 post 或 get 在表单中传递选择列表的 id 值?

javascript - jQuery $ .ajax发布获取引荐来源网址以进行错误调试

PHP/PDO - 服务器已经消失和异常