dtmm0148603 2018-12-21 16:45
浏览 110
已采纳

为什么PDO查询不能在服务器上运行?

I have just uploaded my local project to server but PDO queries are not working in LIVE server. It's working fine on local. I thought PDO might not be installed on server so i ran the below script

PHP version in server is PHP Version 5.3.3, Linux server

<?php
if (!defined('PDO::ATTR_DRIVER_NAME')) {
    echo 'PDO unavailable';
}else{
    echo 'pdo is installed';
}
?>

Connection.php:

<?php
    try{
        $pdo = new PDO('mysql: host=localhost; dbname12', 'myusername', 'password');
    }catch(PDOException $e){
        exit('Database Error');
    }

?>

Login:

<?php
session_start();
include_once('../includes/connection.php');
if(isset($_POST['user_name'], $_POST['user_password'])){
    $username = $_POST['user_name'];
    $password = md5($_POST['user_password']);

    if(empty($username) or empty($password)){
        $error = 'All fields are required!';
    }else {
        $query = $pdo->prepare("SELECT * FROM users WHERE user_name = ? AND user_password = ?");
        $query->bindValue(1, $username);
        $query->bindValue(2, $password);
        $query->execute();
        //var_dump($query);exit;
        $num = $query->rowCount();

        if ($num == 1) {
            // User correct details let's log him in
            $_SESSION['logged_in'] = true;

            header('Location: dashboard.php');
            exit();
        } else {
            $error = 'Incorrect Details';
        }
    }
}
?>

By entering correct credentials the login is showing incorrect details always + For debugging i bypassed the login and entered dashboard where we are fetching articles from DB but nothing was there. And in database we have many entries.

  • 写回答

1条回答 默认 最新

  • doxp30826 2018-12-21 17:44
    关注

    Well I don't know if this is a bug or not, but adding dbname=dbname12 to below line everything starts working!

    $pdo = new PDO('mysql: host=localhost; dbname=dbname12', 'myusername', 'password');
    

    Locally it was working fine with the below code but on server when I added dbname then queries started working.

    $pdo = new PDO('mysql: host=localhost; dbname12', 'myusername', 'password');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘