2017-08-11 96 views
2

我不知道是否有人可以幫忙。大約三年前我放棄了網絡編程。自從一週前回到編程階段以後,我意識到我已經忘記了很多,而且已經發生了很多變化。從php5升級到有運行腳本有問題的php7

我簽署了一個與php7的交易,但我的代碼是php5,當我來運行我的腳本沒有真正的作品。

例如我甚至無法連接到數據庫。爲PHP5我的數據庫連接文件是

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

// Run the actual connection here 
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); 
mysql_select_db("$db_name") or die ("no database");    
?> 

我跟服務器的公司,他們告訴我,一切都改變了,並把我的代碼與PHP7兼容。然後他們通過給我一個示例文件說明這個代碼是什麼,幫助我連接數據庫。

<? 

/* DATEBASE CONFIGURATION */ 

$dbHost = "ip Address"; 
$dbName = "name_of_database_here";   // Database name 
$dbUser = "db_username_here";   // Database user 
$dbPasswd = "db_password_here";     // Database password 

function dbConnect(){ 
global $dbHost, $dbUser, $dbPasswd, $dbName; 
mysql_connect($dbHost, $dbUser, $dbPasswd) or error(mysql_error()); 
mysql_select_db($dbName); 
} 
?> 

我一直運行從本地主機我的數據庫$ DBHOST所以我沒有想到,因爲他們沒有這樣做的連接爲localhost,做它作爲gobal的IP地址我已經想通數據庫不上與網站相同的服務器。

然後我來到我的數據庫腳本從瀏覽器運行到phpmyadmin,他們也沒有工作。這是我的數據庫腳本php5。

<?php 
require "connect_to_mysql.php"; 

    $sqlCommand = "CREATE TABLE admin (
        id int(11) NOT NULL auto_increment, 
        username varchar(24) NOT NULL, 
        password varchar(24) NOT NULL, 
        last_log_date date NOT NULL, 
        PRIMARY KEY (id), 
        UNIQUE KEY username (username) 
        ) "; 
    if (mysql_query($sqlCommand)){ 
     echo "Your admin table has been created successfully!"; 
    } else { 
     echo "CRITICAL ERROR: admin table has not been created."; 
    } 
    ?> 

我想知道,如果有人可以給我爲什麼腳本不運行 非常感謝一些建議,加里

+4

'mysql'擴展在php7中是__removed__。 –

+3

你的'mysql()'函數不再被支持。 – JustBaron

回答

1

的mysql_connect從PHP7刪除。

而不是您可以使用mysqli的要麼庫MySQLi或PDO

例如與PDO

<?php 
$servername = "localhost"; 
$username = "username"; 
$password = "password"; 

try { 
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); 
    // 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 
$servername = "localhost"; 
$username = "username"; 
$password = "password"; 

// Create connection 
$conn = new mysqli($servername, $username, $password); 

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

例如更多細節reffer https://www.w3schools.com/php/php_mysql_connect.asp

+0

對不起,你可以解釋一下,因爲服務器公司已經完成我的連接文件,他們仍然包括mysql_connect – gary33

+0

是我的連接腳本設置的原因,我的數據庫腳本不工作的原因,因爲用腳本我把數據庫是連接的? – gary33

+0

@ gary33所有使用'mysql_ *'函數的代碼都需要重新編碼。這些功能在PHP7中消失了。這通常不是您主人的責任。 – ceejayoz