2017-05-12 25 views
0

データベースに接続しようとしていますが、mysql_connect関数でエラーが表示されます。MySQL Connect機能が動作しません。

エラー: 致命的なエラー:未知のエラー:C:\ xampp \ htdocs \ Connect.phpで未定義の関数mysql_connect()を呼び出す:12スタックトレース:#0 C:\ xampp \ htdocs \ Test.php (3):\ XAMPP \ htdocsに\ Connect.phpライン上の12

接続ファイル:

<?php 

$db_host = "localhost"; 
// Place the username for the MySQL database here 
$db_username = "root"; 
// Place the password for the MySQL database here 
$db_pass = ""; 
// Place the name for the MySQL database here 
$db_name = "oscar"; 

// Run the connection here 
$con = mysql_connect("db_host","$db_username","$db_pass"); 
if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db("$db_name", $con); 
try 
{ 
$conn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_username, $db_pass); 
// set the PDO error mode to exception 
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
echo "Connected successfully"; 
} 
catch(PDOException $e) 
{ 
echo "Connection failed: " . $e->getMessage(); 
} 

?> 

テキストファイル:

<?php 
// Connect to the MySQL database 
require "Connect.php"; 
echo "Success"; 
?> 
()#1 {メイン} Cでスロー必要
+2

どのバージョンのPHPですか? mysql関数は非推奨でPHP 7以降から削除されています。 – RamRaider

+0

あなたのPHPのバージョンは何ですか? mysql_connect この拡張モジュールはPHP 5.5.0で廃止され、PHP 7.0.0では削除されました – ToujouAya

+0

この行は '$ con = mysql_connect(" db_host "、" $ db_username "、" $ db_pass ");' – Akintunde007

答えて

0

を接続します。代わりに、次のコードは動作するはずですmysqli_connectをこのhere

についてもっと知ろ使用する必要があります。

<?php 
/** 
* Created by PhpStorm. 
* User: ... 
* Date: 5-12-2017 
* Time: 09:47 
* Database connection. 
*/ 
?> 
<?php 
define('DB_SERVER', 'localhost'); 
define('DB_USERNAME', 'admin'); 
define('DB_PASSWORD', 'admin'); 
define('DB_DATABASE', 'your_database'); 
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); 

// Check connection 
if (mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
?> 
3

なぜmysql_coを使用していますか同時にPDOと接続しますか? mysqlは廃止され、SQLインジェクションに脆弱です。

のみ、このコードは、関数mysql_connectは推奨されない機能であるデータベースに

<?php 
$db_host = "localhost"; 
// Place the username for the MySQL database here 
$db_username = "root"; 
// Place the password for the MySQL database here 
$db_pass = ""; 
// Place the name for the MySQL database here 
$db_name = "oscar"; 


try { 
    $conn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_username, $db_pass); 
// set the PDO error mode to exception 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    echo "Connected successfully"; 
} catch (PDOException $e) { 
    echo "Connection failed: " . $e->getMessage(); 
} 
0

のMySQL関数は、PHP 5.5.0で非推奨になりました。私はあなたのコードのいくつかの変更を変更しました。 mysqli connect文に$ symbolが追加されました。

$con = mysqli_connect("$db_host","$db_username","$db_pass"); 
if (!$con) 
{ 
    die('Could not connect: ' . mysqli_error()); 
} 
mysqli_select_db("$db_name", $con); 
0

は、これは完璧に動作mysqli

<?php 
$db_host = "localhost"; 
$db_username = "root"; // Place the username for the MySQL database here 
$db_pass = "";// Place the password for the MySQL database here 
$db_name = "test";// Place the name for the MySQL database here 
$conn = new mysqli($db_host, $db_username, $db_pass,$db_name); 

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

を使用してみてください。

関連する問題