PDO:未选择数据库

时间:2013-04-19 04:12:17

标签: php mysql pdo dojo

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

登录 - >选择数据库 - >从给定的卡片列表中选择 - > mysql-process产生一个测试列表 - >选择一个测试 - > mysql-process根据选定的卡片生成批号并进行测试。

脚本“生成测试列表的mysql-process”正在工作,我可以选择其中一个测试。

然后错误弹出exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected'

为什么错误表明已经选择了通过数据库选择的无数据库。

有两个php文件:gathertests2.php(正在工作)和gatherbatches.php(无效)。

main.php(excerpt1)

on(selPCBA, 'change', function(value)
{console.debug('value = '+value);
var selectedPCBA = this.get('displayedValue');
console.debug('Contents of "selected" variable ='+selectedPCBA);
var selTest = registry.byId('ID_selTest');
    if (selectedPCBA.indexOf("class='inUse'")!==-1)
         {request.post('gathertests2.php',
    {data:{testDB : value},
    handleAs: "json"}).then
    (
         function(response)
        {var memoStore1 = new Memory({data:response});
                selTest.set('store', memoStore1);
            selTest.set('value','');
        },
     function(error)
            {alert("Test's Error:"+error);
    });
         selTest.startup();
    }
   else
     {console.debug('Error:- Attempting to select the unavailable card');
      alert('Please select the only highlighted card');
}
});

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

其中call listmfg_codes()

DELIMITER $$
DROP PROCEDURE IF EXISTS `testdata2060_03`.`listmfg_codes` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `listmfg_codes`()
BEGIN
select distinct(mfg_code) from test order by mfg_code asc;
END $$
DELIMITER ;

main.php(excerpt2)

on(selTest, 'change', function(value)
    {var selectedTest = this.get('displayedValue');
 var selBatch = registry.byId('ID_selBatch');
 request.post('gatherbatches.php',
    {data:{testCard : value},
     handleAs: "json"}).then
(
    function(response)
    {var memoStore2 = new Memory({data:response});
     selBatch.set('store', memoStore2);
    selBatch.set('value','');
    },
   function(error)
       {alert("Batch's Error:"+error);
    }
);
       selBatch.startup();
 });

gatherbatches.php

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

其中call listbatch()

 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 ; 

好的,已经完成了。 请告诉我哪里出错了。请参阅下面的附件:

enter image description here

//添加了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); 
  ?>

1 个答案:

答案 0 :(得分:0)

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

您最有可能想象一个客户端服务器应用程序,如桌面应用程序 - 坚实且独立。这是错的。所有这些AJAX调用都是对分离和清理 PHP实例进行的,对以前的执行一无所知。因此,您在之前的调用中选择的任何数据库都无法帮助您使用当前的数据库 - 您必须再次初始化所有资源。

还有一个非常奇怪的事情:您从客户端传递数据库名称!从架构和安全的角度来看,我称之为奇怪的行为。此外,没有一个理由可以为多个测试这样的本地化案例建立多个数据库。

所以,通常和正确的方式:

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

你将不会遇到任何麻烦。