gpt4 book ai didi

php - 让 PDO(或 mysqli 扩展)在数据库上运行查询时出现问题

转载 作者:行者123 更新时间:2023-11-29 23:17:03 25 4
gpt4 key购买 nike

主要问题是过去一周我无法让我的脚本运行查询。

我正在运行 wamp apache 2.4、php 5.5 和 mysql 5.6

  if ($flag == true) {

//try to connect to db with given parameters
// minimal validation to make sure no input was sent in error so now i can begin code to connect to database
// using the pdo php data object extension

print 'all input validated';

$host= "127.0.0.1";
$db_name ="theturnbuckle";
$port= "3306";
$dbusername= 'root';
$dbpassword= '';

//registration details including host, port and database name
$dsn = "mysql:host=$host;port=$port;dbname=$db_name;";

//error handling for connection
try {
$db = new PDO($dsn, $dbusername, $dbpassword);
//if no proplems flag a variable for connection status which is to be used later.
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setFetchMode(PDO::FETCH_ASSOC);


//first query to test that the username is not in the database.
$username = $_POST['username'];
$userquery = "SELECT username FROM registation_view WHERE username = :username" ;


//second query to test for existing email
$email = $_POST['email'];
$emailquery = "SELECT email FROM registation_view WHERE email = :email";

//preparing statement, binding html input to variables to be used in the prepared statement.
$db = new PDO($dsn, $dbusername, $dbpassword);
$stmt = $db->prepare($userquery);
$stmt->bindParam(':username', $username, PDO::PARAM_STR, 15);

//execute the query in variable
$stmt->execute($userquery);
if ($stmt->fetchColumn() > 0){
print 'Sorry ' .$username . 'is already in use please try another';
}//else the username is not is use as fetchColumn is null or the if is false
else {$registerprocess === true;}


$db = new PDO($dsn, $dbusername, $dbpassword);
$stmt = $db->prepare($emailquery);
$stmt->bindParam(':email', $email, PDO::PARAM_STR, 15);

$stmt->execute($emailquery);
if ($stmt->fetchColumn() > 0){
print 'Sorry ' .$_POST['email'] . 'is already in use please try another';
$registerprocess === false;
}//else the email is not is use as fetchColumn is not > 0 so the registration proceeds.
else {$registerprocess === true;}

// catch (PDOException $e){
//die("Unable to run query: " . $e->getMessage());}


if ($registerprocess === true) {
//username and email are not on record so i am free to INSERT data into database
$query = "INSERT INTO registration_view (username, password, email, gender, date_of_birth, joindate)
VALUES (?, ?, ?, ?, ?, ?)";

$joindate = now();
$date_of_birth = $_POST['day'] . $_POST['month'] . $_POST['year'];

$stmt->prepare($query);
$stmt->bindParam(1, $_POST['username'], PDO::PARAM_STR, 20) ;
$stmt->bindParam(2, $_POST['password'], PDO::PARAM_STR, 20) ;
$stmt->bindParam(3, $_POST['email'], PDO::PARAM_STR, 20) ;
$stmt->bindParam(4, $_POST['gender']) ;
$stmt->bindParam(5, $date_of_birth) ;
$stmt->bindParam(6, $joindate) ;

$stmt->execute();

print ' Welcome ' .$username . 'you are now registered';
}
} catch (PDOException $e) {
die("Unable to connect: " . $e->getMessage());
}

}//INPUT VALIDATION IF

这是我的代码,第一个标志是在数据验证成功后继续运行,大部分工作已经完成并打印“验证”;证实了这一点。

我确信 $dsn 没有错误,因为之前用单引号括起来时会显示错误,现在不会显示此类错误。

虽然我对代码困惑持开放态度,但我也一直在检查 php.ini my.ini 和 apache.config,但正如所说的一周没有运气。

我唯一可以添加的帮助(或者至少可能是一个线索)是 apache 和 php 错误日志的一些错误。

当我尝试从本地主机运行脚本时,Apache 显示此信息。

[Mon Dec 29 12:49:13.855653 2014] [authz_core:error] [pid 3632:tid 964] [client ::1:49206] AH01630: client denied by server configuration: C:/Apache24, referer: http://localhost/theturnbuckle/

并且 php 显示了这个,尽管我在安装之前重新安装了 apche 服务器,但我将 php_intl.dll 和 phpldap.dll 移动到 wamp 中的另一个文件夹,这些错误消失了。 (甚至不确定这是否是最佳实践)

[24-Dec-2014 12:00:37 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.5.12/ext/php_intl.dll' - The specified module could not be found.

在第 0 行未知

[24-Dec-2014 12:00:37 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.5.12/ext/php_ldap.dll' - The specified module could not be found.

第 0 行未知

我取消了一些 mysql 服务器变量的注释。

innodb_data_home_dir = C:\mysql\data/
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = C:\mysql\data/
innodb_log_arch_dir = C:\mysql\data/

最后我检查了一些 php.ini 服务器变量来尝试帮助解决这种情况,显示错误已打开。

似乎安装了所有 php 扩展,例如 php_pdo_mysql 和 php_mysqli。

但最终我还是在黑暗中挣扎。

最佳答案

您有两个警告:

[24-Dec-2014 12:00:37 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.5.12/ext/php_intl.dll' - The specified module could not be found.

[24-Dec-2014 12:00:37 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'c:/wamp/bin/php/php5.5.12/ext/php_ldap.dll' - The specified module could not be found.

因此只需安装或启用这些扩展即可。

关于php - 让 PDO(或 mysqli 扩展)在数据库上运行查询时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27691241/

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