简体   繁体   English

PDO无法在GoDaddy服务器中运行

[英]PDO not working in GoDaddy Server

I am working on a project that is using a GoDaddy private server. 我正在使用GoDaddy私有服务器的项目。 Looks like the project is using mysql_connect to connect to the db. 看起来该项目正在使用mysql_connect连接到数据库。

I have worked with PDO before and figured I would just create a new file within the server so I can connect by using PDO. 我曾经使用过PDO,并且认为我只是在服务器中创建一个新文件,所以我可以使用PDO进行连接。 However I can't get it to work, and I can't get any errors to show up, nothing seems to happen when I run this code. 但是我无法让它工作,我无法显示任何错误,当我运行此代码时似乎没有任何事情发生。

If I try to echo out a string after that block of code, the string wont show up, if I echo out a string before this block of code, the string will show up. 如果我尝试在该代码块之后回显字符串,则字符串不会显示,如果我在此代码块之前回显字符串,则字符串将显示。

If I try to execute a prepared statement nothing happens. 如果我尝试执行准备好的声明,则没有任何反应。 PW, UN, HOST, and db name are all correct. PW,UN,HOST和db名称都是正确的。 Am I doing something wrong? 难道我做错了什么?

<?php
    error_reporting(E_ALL);
    $dns = "mysql:host=localhost;dbname=mainsql;";
    $username = "bowski";
    $passwd = "kingsman1";
    try {
        $db = new PDO($dsn, $username, $passwd);
} catch (PDOException $ex) {
        echo $ex->getMessage();
}

Some host providers or even your own server might have PDO disabled or not installed per default. 某些主机提供商甚至您自己的服务器可能默认情况下已禁用或未安装PDO。 GoDaddy is one such provider. GoDaddy就是这样一个提供者。

I often see people struggling in similar situations thinking their PDO code is wrong, and asking the same question. 我经常看到人们在类似的情况下挣扎,认为他们的PDO代码是错误的,并且问同样的问题。

Therefore, it is always a good practice to check the availability of PDO driver before checking PDO code. 因此,在检查PDO代码之前检查PDO驱动程序的可用性始终是一个好习惯。 One quick check is to perform the phpinfo(); 一个快速检查是执行phpinfo(); call which dumps a (long) table of installed components. 调用转储已安装组件的(长)表。

Even if you have PDO installed, this is to ensure and guaranty that PDO driver is installed and running. 即使您安装了PDO,也是为了确保并保证PDO驱动程序已安装并正在运行。

Add PDO driver checker code before your PDO code, saving you from some unnecessary debugging time: 在PDO代码之前添加PDO驱动程序检查程序代码,从而避免一些不必要的调试时间:

if (!defined('PDO::ATTR_DRIVER_NAME'))
    echo 'PDO driver unavailable';

Here is a step-by-step procedure to enable PDO extension on Godaddy: 以下是在Godaddy上启用PDO扩展的分步过程:

  1. Login to your CPanel 登录您的CPanel

  2. Go to Web Hosting -> Manage for your domain 转到Web Hosting -> Manage您的域名

  3. Click on Select PHP version menu 单击Select PHP version菜单

在此输入图像描述

  1. Now, you have the option to choose PHP version from the dropdown. 现在,您可以从下拉列表中选择PHP版本。 Choose a different version than selected (eg 5.4 or 5.5) and then click Set as current . 选择与所选版本不同的版本(例如5.4或5.5),然后单击“ 设为当前”

  2. After that, check the checkboxes of PHP extension you want on your site (eg PDO) and then click Save button present at the bottom. 之后,检查您站点上所需的PHP扩展的复选框(例如PDO),然后单击底部的Save按钮。

在此输入图像描述

I have exact same situation as what you described. 我和你描述的情况完全相同。 After a big while of confusion and debugging, and I found out the reason / solution. 经过一段时间的混乱和调试,我发现了原因/解决方案。 But still not sure why Godaddy PDO has this issue. 但仍然不确定为什么Godaddy PDO有这个问题。

PHP v5.4.45 PHP v5.4.45

<?php
$pdo = new PDO('mysql:localhost;dbname=mydb', $user, $pass);

$sth = $pdo->prepare('select * from tab limit 1');
$sth->execute();
$row = $sth->fetchAll();
echo '<pre>select * from test -- not working -- ';
print_r($row);

$sth = $pdo->prepare('select now()');
$sth->execute();
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
echo "\nselect now -- working -- ";
print_r($row);

$sth = $pdo->prepare('show databases');
$sth->execute();
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
echo "\nshow databases -- working -- ";
print_r($row);

$sth = $pdo->prepare('show tables');
$sth->execute();
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
echo "\nshow tables -- not working -- ";
print_r($row);

$sth = $pdo->prepare('show privileges');
$sth->execute();
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
echo "\nshow privileges -- working but not right -- ";
print_r($row);

$sth = $pdo->prepare('select * from information_schema.TABLES where TABLE_SCHEMA != \'information_schema\'');
$sth->execute();
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
echo "\nselect * from information_schema.TABLES -- working -- ";
print_r($row);

No error -- how confusing. 没有错误 - 多么令人困惑。 And I test this, works: 我测试这个,工作:

<?php
$pdo = new PDO('mysql:localhost;dbname=mydb', $user, $pass);

$sth = $pdo->prepare('select * from mydb.tab limit 1');
$sth->execute();
$row = $sth->fetchAll();
echo '<pre>select * from test -- working !!! -- ';
print_r($row);

And now I come up with this solution, but not sure if it is Godaddy PDO bug? 现在我想出了这个解决方案,但不确定它是否是Godaddy PDO的错误?

<?php
$pdo = new PDO('mysql:localhost', $user, $pass);

$sth = $pdo->prepare('use mydb');
$sth->execute();

$sth = $pdo->prepare('select * from tab limit 1');
$sth->execute();
$row = $sth->fetchAll();
echo '<pre>select * from test -- working !!! -- ';
print_r($row);

Conclusion: 结论:

It looks like Godaddy PDO does not use dbname, you need to manually run ' use dbname ' after new PDO and before any other SQLs 看起来Godaddy PDO不使用dbname,你需要在新的PDO之后和任何其他SQL之前手动运行' use dbname '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM