我正在尝试通过PHP连接到我的数据库,但是通过Safari运行时会出现空白页(http://pushchat.local:11111 / test / databasename.php)
我可以在http://pushchat.local:11111 / test /下看到我的datbasename.php文件,如下所示:
以下是我的PHP代码:
try
{
if (!defined('APPLICATION_ENV'))
define('APPLICATION_ENV', getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : ‘development’);
require_once '../api_config.php';
$config = $config[APPLICATION_ENV];
$pdo = new PDO(
'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'],
$config['db']['username'],
$config['db']['password'],
array());
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->query('SET NAMES utf8');
echo 'Database connection successful!';
}
catch (Exception $e)
{
echo 'Could not connect to the database. Reason: ' . $e;
}
我什至试图用Codebug叹息,但我认为它永远不会停止。
谢谢你的帮助。
对不起,但是我看到的是database.php而不是databasename.php。
您甚至检查文件是否存在?
同样,连接到数据库也应该在配置文件中键入,而在主PHP文件中则需要它。
例如,配置文件应如下所示:
<?php
$connection = mysql_connect('localhost', 'root', 'password'); //here is the host, username and password of mysql account.instead of localhost type in your websites domain name.
if (!$connection){ //and instead of password,type in your own password.
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('test');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
?>
&主PHP应该看起来像这样:
<?php
require('config.php'); //requires the config.php page
//rest of the code goes here...
?>
同样在第10行,您错过了结束语。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句