0

我在 goddady 有一个主要的托管帐户,例如 a.com

我添加了一个插件域,例如 b.com

我创建了一个数据库 db_a,可以正确使用 a.com。

我创建了一个数据库 db_b,并尝试使用 b.com,我在哪里可以配置它,以便 b.com 可以作为 host=localhost 访问 db_b?

4

1 回答 1

0

经过一段时间的困惑和调试,我找到了原因/解决方案。但仍然不确定为什么 Godaddy PDO 有这个问题。

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);

没有错误——多么令人困惑。我对此进行了测试,有效:

<?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);

现在我想出了这个解决方案,但不确定它是否是 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);

结论:

看起来 Godaddy PDO 不使用 dbname,您需要在新 PDO之后和任何其他 SQL 之前手动运行“使用 dbname

于 2017-04-07T10:29:24.330 回答