所以,我现在很茫然。我有一个本地的mysql数据库,我正在用作POC,但是有一个奇怪的问题。
我在使PHP连接到数据库时遇到了一些麻烦,因此我编写了一个简单的PHP脚本来仅测试数据库连接。看起来像这样:
<?php
define("DB_HOST", "127.0.0.1");
define("DB_PORT", "3306");
define("DB_NAME", "mydb");
define("DB_USER", "root");
define("DB_PASS", 'mypass');
try {
// $connStr = sprintf("mysql:host=%s;dbname=%s", DB_HOST, DB_NAME);
$connStr = "mysql:host=127.0.0.1;dbname=mydb;port=3306";
$db = new PDO($connStr, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e) {
$issue = $e->getMessage();
echo "Unable to connect to the database: $issue<br>";
}
当我通过http://127.0.0.1/filename.php访问文件时,它返回“已成功连接”状态,这很棒。
但是,我在PHP类定义中使用了完全相同的代码,并且将该类包含在PHP脚本中以处理来自表单的发布数据,并且看来根本没有连接。浏览器窗口根本不显示任何内容-完全空白。我已经尝试过Firefox和Google Chrome,但它们的行为相同。更令人沮丧的是,似乎没有任何记录可以让我知道在哪里看。
如果有帮助,请参见以下处理器PHP脚本的前几行:
<?php
ini_set('display_errors','On');
error_reporting(E_ERROR);
include 'myfile.php';
// Begin main function calls below
date_default_timezone_set("America/New_York");
$a = new DB_Update();
$weekday = date("l");
$caldate = date("Y-m-d");
echo "<p>weekday is $weekday and date is $caldate<br>";
这时,我什至没有得到第一个echo语句来显示格式化的日期和工作日。
我想念什么?似乎应该很容易...尤其是因为连接代码本身可以工作...
预先感谢您的帮助。
编辑:clint从我正在使用的类中询问代码,所以我在这里添加它...
class DB_Update {
define("DB_HOST", '127.0.0.1:3306');
define("DB_NAME", 'mydb');
define("DB_USER", 'root');
define("DB_PASS", 'mypass');
try{
// $connStr = sprintf("mysql:host=%s;dbname=%s", DB_HOST, DB_NAME);
$connStr = "mysql:host=127.0.0.1;dbname=mydb;port=3306";
$db = new PDO($connStr, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e){
$issue = $e->getMessage();
echo "Unable to connect to the database: $issue<br>";
}
function CheckIn($student,$class,$date,$time,$wday,$signature) {
try {
$info = array(':student' => $student,
':class' => $class,
':dt' => $date,
':tm' => $time,
':weekday' => $wday,
':signature' => $signature);
$sql = 'INSERT INTO TGP_CheckIn (
student,
class,
datein,
timein,
weekday,
signature)
VALUES (
:student,
:class,
:dt,
:tm,
:weekday,
:signature);';
$a = $db->prepare($sql);
echo "<br>Checking in<br>";
$a->execute($info);
}
catch (PDOException $pe) {
die($pe->getMessage());
}
}
function CheckOut($student,$class,$date,$time,$wday,$signature) {
try {
$info = array(':student' => $student,
':class' => $class,
':dt' => $date,
':tm' => $time,
':wday' => $wday,
':signature' => $signature);
$sql = 'INSERT INTO TGP_CheckOut (
student,
class,
dateout,
timeout,
weekday,
signature)
VALUES (
:student,
:class,
:dt,
:tm,
:wday,
:signature);';
$a = $db->prepare($sql);
$a->execute($info);
echo "Checking Out";
echo "<br>";
}
catch (PDOException $pe) {
die($pe->getMessage());
}
}
}
让我知道是否还有其他可以提供的东西。