Php PDO:未选择任何数据库

Php PDO:未选择任何数据库,php,mysql,pdo,dojo,Php,Mysql,Pdo,Dojo,以下是按顺序排列的文本: 登录->选择数据库->从给定卡片列表中选择->mysql流程生成测试列表->选择测试->mysql流程根据所选卡片和测试生成批次号 一个脚本“mysql进程生成测试列表”正在运行,我可以选择其中一个测试 然后,弹出错误消息“SQLSTATE[3D000]:无效目录名:1046未选择任何数据库”的异常“PDOException” 为什么错误说明虽然已选择数据库,但未选择任何数据库 有两个php文件:gatheretests2.php(工作)和gatherebatches.

以下是按顺序排列的文本:

登录->选择数据库->从给定卡片列表中选择->mysql流程生成测试列表->选择测试->mysql流程根据所选卡片和测试生成批次号

一个脚本“mysql进程生成测试列表”正在运行,我可以选择其中一个测试

然后,弹出错误消息“SQLSTATE[3D000]:无效目录名:1046未选择任何数据库”的异常“PDOException”

为什么错误说明虽然已选择数据库,但未选择任何数据库

有两个php文件:
gatheretests2.php
(工作)和
gatherebatches.php
(不工作)

main.php(节选1)

gathertests2.php

<?php
    require_once($_SERVER['DOCUMENT_ROOT'] . '/firephp_include.php');
$firephp->setEnabled(TRUE);
$firephp->info('Start debugging in gathertest2.php');
require_once '../scripts/login.php';
    $db = $_POST['testDB'];
$stmt_use = $dbh->prepare("use $db");//ok
$firephp->fb($stmt_use);

    try //PDO Driver is used in PHP for working with MySQL!!!!
    {
        $stmt_use  -> execute();//ok
        $firephp->log("Successfully executed!");
    }

    catch (PDOException $err) //PDOException is declared in login.php
    {
        $alertmsg = $err->getMessage();
        include 'alertmessage.php';
        $firephp->error("Unsuccessfully executing: $err");
    }
    $stmt_call1 = $dbh->prepare('call listmfg_codes()');
$firephp->fb($stmt_call1);

    try //PDO Driver is used in PHP for working with MySQL!!!!
    {
        $stmt_call1->execute();
        $firephp->log("Successfully executed!");
    }

    catch (PDOException $err) //PDOException is declared in login.php
    {
        $alertmsg = $err->getMessage();
        include 'alertmessage.php';
        $firephp->error("Unsuccessfully executing: $err");
    }

$result = $stmt_call1->fetchAll(PDO::FETCH_ASSOC); //ok
$output = json_encode($result); 
echo $output;
$firephp -> fb($result);
$firephp -> info("End");
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/firephp_include.php');
$firephp->setEnabled(TRUE);
$firephp->warn('Start debugging in gatherbatches.php');
require_once '../scripts/login.php';
    $card = $_POST['testCard'];//must add '' in "" bracket for call command to work
$stmt_call2 = $dbh->prepare("call listbatch('$card')");
$firephp->fb($stmt_call2);
try //PDO Driver is used in PHP for working with MySQL!!!!
    {
        $stmt_call2->execute();
        $firephp->log("Successfully executed!");
    }

catch (PDOException $err) //PDOException is declared in login.php
    {
        $alertmsg = $err->getMessage();
        include 'alertmessage.php';
        $firephp->error("Unsuccessfully executing: $err");  
        }

$result = $stmt_call2->fetchAll(PDO::FETCH_ASSOC); //ok
    $output = json_encode($result); 
echo $output;
$firephp -> fb($result);
$firephp -> warn("End");
main.php(节选2)

gatherbatches.php

<?php
    require_once($_SERVER['DOCUMENT_ROOT'] . '/firephp_include.php');
$firephp->setEnabled(TRUE);
$firephp->info('Start debugging in gathertest2.php');
require_once '../scripts/login.php';
    $db = $_POST['testDB'];
$stmt_use = $dbh->prepare("use $db");//ok
$firephp->fb($stmt_use);

    try //PDO Driver is used in PHP for working with MySQL!!!!
    {
        $stmt_use  -> execute();//ok
        $firephp->log("Successfully executed!");
    }

    catch (PDOException $err) //PDOException is declared in login.php
    {
        $alertmsg = $err->getMessage();
        include 'alertmessage.php';
        $firephp->error("Unsuccessfully executing: $err");
    }
    $stmt_call1 = $dbh->prepare('call listmfg_codes()');
$firephp->fb($stmt_call1);

    try //PDO Driver is used in PHP for working with MySQL!!!!
    {
        $stmt_call1->execute();
        $firephp->log("Successfully executed!");
    }

    catch (PDOException $err) //PDOException is declared in login.php
    {
        $alertmsg = $err->getMessage();
        include 'alertmessage.php';
        $firephp->error("Unsuccessfully executing: $err");
    }

$result = $stmt_call1->fetchAll(PDO::FETCH_ASSOC); //ok
$output = json_encode($result); 
echo $output;
$firephp -> fb($result);
$firephp -> info("End");
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/firephp_include.php');
$firephp->setEnabled(TRUE);
$firephp->warn('Start debugging in gatherbatches.php');
require_once '../scripts/login.php';
    $card = $_POST['testCard'];//must add '' in "" bracket for call command to work
$stmt_call2 = $dbh->prepare("call listbatch('$card')");
$firephp->fb($stmt_call2);
try //PDO Driver is used in PHP for working with MySQL!!!!
    {
        $stmt_call2->execute();
        $firephp->log("Successfully executed!");
    }

catch (PDOException $err) //PDOException is declared in login.php
    {
        $alertmsg = $err->getMessage();
        include 'alertmessage.php';
        $firephp->error("Unsuccessfully executing: $err");  
        }

$result = $stmt_call2->fetchAll(PDO::FETCH_ASSOC); //ok
    $output = json_encode($result); 
echo $output;
$firephp -> fb($result);
$firephp -> warn("End");
好了,完成了。 请告诉我哪里出错了。请参阅以下附件:

//添加了login.php

<?php 
   $dsn = 'mysql:host=localhost;Port=3306';
   $user = 'root'; $pswd = '';
   $dbh = new PDO($dsn, $user, $pswd, array(PDO::ATTR_PERSISTENT, TRUE));     
   $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
   $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
  ?>

我没有读那么多代码,但我觉得你对PHP工作原理的看法是错误的

最有可能的情况是,您将客户机-服务器应用程序想象成一个桌面应用程序—实体和独立。这是错误的。所有这些AJAX调用都是为了分离和清理PHP实例而进行的,对以前的执行一无所知。因此,在以前的调用中选择的任何数据库在当前的调用中都没有帮助-您必须再次初始化所有资源。

还有一件非常奇怪的事情:您正在从客户端传递一个db名称!从架构和安全的角度来看,我认为这是一种奇怪的行为。此外,对于这种本地化的情况,没有一个单一的理由像几个测试那样拥有多个数据库

因此,请以常规和适当的方式:

  • 使用一个数据库
  • 在login.php中选择一次
  • 可能会重新安排测试,使用one表保存所有类似数据

你永远不会有任何麻烦

您在哪里初始化了$dbh变量。。确保它看起来像$dbh=newpdo('mysql:host=localhost;dbname=','',PASSWORD')$dbh变量在“../scripts/login.php”中初始化。请参阅require_once命令。因此,我能够连接到数据库,GathereTestS2.php正在工作。但是gatherbatches.php不起作用。@user1739825,在phpfire日志中显示login.phpLooking,看起来您在gathertests2.php上选择了数据库,但在gatherbatches.php中没有。您在gatheretest2.php中有“$stmt_use=$dbh->prepare(“use$db”)”语句,并且您没有在gatherebatches.php中的任何位置选择数据库,因此“$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);”是罪魁祸首。现在,由于没有错误,即使删除了“ATTR_ERRMODE”,也不会更新任何记录。GathereTestS2.php工作,而GathereBatch不工作。再次证明“$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);”与此无关。相反,似乎每次执行任何mysql脚本之前都需要指示数据库的名称。不是吗?
 DELIMITER $$
 DROP PROCEDURE IF EXISTS `testdata2060_03`.`listbatch` $$
 CREATE DEFINER=`root`@`localhost` PROCEDURE `listbatch`(mfgnum VARCHAR(24))
 BEGIN
     SELECT batch FROM test WHERE mfg_code = mfgnum group by batch order by batch desc;
 END $$
 DELIMITER ; 
<?php 
   $dsn = 'mysql:host=localhost;Port=3306';
   $user = 'root'; $pswd = '';
   $dbh = new PDO($dsn, $user, $pswd, array(PDO::ATTR_PERSISTENT, TRUE));     
   $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
   $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
  ?>