加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Codeigniter无法连接到外部SQL Server

发布时间:2020-12-13 22:26:13 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试将我的codeigniter框架连接到外部数据库.但它显示错误 A Database Error Occurred Unable to connect to your database server using the provided settings. Filename: core/Loader.php Line Number: 346 然后,我将这一个插入到config / database
我正在尝试将我的codeigniter框架连接到外部数据库.但它显示错误

A Database Error Occurred Unable to connect to your database server
using the provided settings. Filename: core/Loader.php Line Number:
346

然后,我将这一个插入到config / database.php的末尾

echo '<pre>';
  print_r($db['default']);
  echo '</pre>';

  echo 'Connecting to database: ' .$db['default']['database'];
  $dbh=mysql_connect
  (
    $db['default']['hostname'],$db['default']['username'],$db['default']['password'])
    or die('Cannot connect to the database because: ' . mysql_error());
    mysql_select_db ($db['default']['database']);

    echo '<br />   Connected OK:'  ;
    die( 'file: ' .__FILE__ . ' Line: ' .__LINE__);

但它表明

A PHP Error was encountered
Severity: 8192
Message: mysql_connect(): The mysql extension is deprecated and will
be removed in the future: use mysqli or PDO instead
Filename: config/database.php
Line Number: 79

A PHP Error was encountered
Severity: Warning
Message: mysql_connect(): Can’t connect to MySQL server on
‘167.114.xxx.xxx’ (111)
Filename: config/database.php
Line Number: 79

Cannot connect to the database because: Can’t connect to MySQL server
on ‘167.114.xxx.xxx’ (111)

然后我试图在codeigniter目录之外创建这个(在public_html中)

<?php
$servername = "167.114.xxx.xxx";
$username = "myusername";
$password = "dbpass";
$database = "dbname";

// Create connection
$conn = mysql_connect($servername,$username,$password,$database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

它显示连接成功.所以我该怎么做?而config / database.php中的db细节与上面相同

解决方法

默认情况下,MySQL将拒绝来自外部的连接.

你的MySQL服务器在哪里?通常,如果您使用某些Web托管服务,它们允许您从CPanel解锁外部连接.请注意,大多数需要一些时间才能使其真正活跃.

另外,请注意mysql_connect不返回对象,而只返回资源(连接ID).因此,如果您真的想继续使用已弃用的(旧)MySQL API,请执行以下操作:

<?php
$link = mysql_connect('localhost','mysql_user','mysql_password'); // note that if the port is different from 3306,you must do: 'server:port'
if (!$link) {
    die('Could not connect ' . mysql_error());
}
echo 'Connection successfull';
mysql_close($link);

但是,建议使用像MySQLi或PDO这样的东西,因为长时间不更新,缺乏OOP支持,缺乏新的MySQL功能支持等等,不推荐使用MySQL扩展.

使用MySQLI的替代方案,来自PHP手册:

<?php
$link = mysqli_connect("127.0.0.1","my_user","my_password","my_db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读