gpt4 book ai didi

php - PDO:未选择数据库

转载 作者:行者123 更新时间:2023-11-30 23:19:38 25 4
gpt4 key购买 nike

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

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

脚本“mysql-process to produce a list of tests”正在运行,我可以选择其中一个测试。

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

虽然已经选择了数据库,但为什么会报错说没有选择数据库。

有 2 个 php 文件:gathertests2.php(有效)和 gatherbatches.php(无效)。

ma​​in.php(摘录1)

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 ;

ma​​in.php(摘录2)

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

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

最佳答案

我没有阅读所有代码,但我感觉您对 PHP 工作原理的理解是错误的。

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

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

所以,让它成为常规和正确的方式:

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

而且您将不会遇到任何麻烦。

关于php - PDO:未选择数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16097076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com