2011-11-20 73 views
2

嘿大家都希望一切順利。我有這樣的代碼,一個MySQL數據庫上完美的作品更改php mysql到php mssql

<?php 
// visit http://php.net/pdo for more details 
// start error handling 

try 
{ 
    // connect 
    $pdo = new PDO('mysql:host=localhost;dbname=name', 'name', 'password'); 
    // enable error handling through exceptions 
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    // create safe query 
    $query = $pdo->prepare("SELECT ip FROM tester WHERE state = ? ORDER BY RAND 

(UNIX_TIMESTAMP(NOW())) LIMIT 1"); 

    // pass data & execute query (since the data are of string type 
    // and therefore can be passed in this lazy way) 
    $query->execute(array($_POST['State'])); 
    // get value 
    $ip = $query->fetchColumn(); 
    // print out the IP address using $ip 
} 
catch (Exception $e) 
{ 
    echo "sorry, there was an error."; 
    mail("[email protected]", "database error", $e->getMessage(), "From: [email protected]"); 
} 

if(isset($_POST['email'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = "[email protected]"; 
    $email_subject = "This is a test"; 


    function died($error) { 
     // your error code can go here 
     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
    if(!isset($_POST['first_name']) || 
     !isset($_POST['last_name']) || 
     !isset($_POST['email']) || 
     !isset($_POST['what']) || 
     !isset($_POST['State']) || 
     !isset($_POST['comments'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');  
    } 

    $what = $_POST['what']; // required 
    $first_name = $_POST['first_name']; // required 
    $last_name = $_POST['last_name']; // required 
    $email_from = $_POST['email']; // required 
    $state = $_POST['State']; // not required 
    $comments = $_POST['comments']; // required 

    $error_message = ""; 
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
    if(!preg_match($email_exp,$email_from)) { 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 
    $string_exp = "/^[A-Za-z .'-]+$/"; 
    if(!preg_match($string_exp,$first_name)) { 
    $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
    } 
    if(!preg_match($string_exp,$last_name)) { 
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
    } 
    if(strlen($comments) < 2) { 
    $error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
    } 
    if(strlen($error_message) > 0) { 
    died($error_message); 
    } 
    $email_message = "Form details below.\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "First Name: ".clean_string($first_name)."\n"; 
    $email_message .= "Last Name: ".clean_string($last_name)."\n"; 
    $email_message .= "What: ".clean_string($what)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "State: ".clean_string($ip)."\n"; 
    $email_message .= "Comments: ".clean_string($comments)."\n"; 
// create email headers 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
if (!mail($email_to, $email_subject, $email_message, $headers)) 
{ 
    echo "failed to send message"; 
} 

?> 

<!-- include your own success html here --> 

Thank you for contacting us. We will be in touch with you very soon. 

<?php 
} 
?> 

當有人選擇的狀態並提交網頁表單,該代碼連接到MySQL數據庫和

隨機選擇一個IP地址,並將其發送到我的電子郵件。這十分完美。

的問題我已經是我做這行的人並不想使用MySQL數據庫,所以希望它

在MSSQL數據庫intergrated。

我知道在PHP代碼上面我連接到MySQL數據庫,而不是MSSQL數據庫,但

不能制定出什麼需要改變,使這一切工作。

我確實發現了這個在線,並認爲我可能能夠與我現有的代碼進行整合,但到目前爲止沒有運氣。

<?php 
$myServer = "localhost"; 
$myUser = "your_name"; 
$myPass = "your_password"; 
$myDB = "examples"; 

//connection to the database 
$dbhandle = mssql_connect($myServer, $myUser, $myPass) 
    or die("Couldn't connect to SQL Server on $myServer"); 

//select a database to work with 
$selected = mssql_select_db($myDB, $dbhandle) 
    or die("Couldn't open database $myDB"); 

//declare the SQL statement that will query the database 
$query = "SELECT id, name, year "; 
$query .= "FROM cars "; 
$query .= "WHERE name='BMW'"; 

//execute the SQL query and return records 
$result = mssql_query($query); 

$numRows = mssql_num_rows($result); 
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 

//display the results 
while($row = mssql_fetch_array($result)) 
{ 
    echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>"; 
} 
//close the connection 
mssql_close($dbhandle); 
?> 

任何幫助獲得這項工作將不勝感激。謝謝大家

阿里

+0

你用mysql的功能。你應該調整它們。 (蘭特是MySQL,而不是SQL) – Bram

+0

你可以發佈你正在得到的任何錯誤?您收到 – danielrsmith

+0

後的錯誤,並表示這是指向該行觸發錯誤,所以我們可以看得更遠 – Nonym

回答

1

看來你沒有啓用MSSQL擴展在php.ini文件。

在Linux中

  • 您將在php.ini中找到這個文件;extension=mssql.so 以及與此替換:extension=mssql.so(只是刪除 分號啓用。)

  • 你會發現一個文件(如果你正在使用xampp linux) lampp/etc/freetds.conf文件。在這個文件中,你需要添加下面的配置。

    補充一點:

    [DevSqlServer]

    主機= 172.16.1.123

    實例= DEVAPSERVER

    端口= 1433

    TDS版本= 7.0

  • 重新啓動您的服務器。你的php代碼應該是這樣的。

  • 在這裏,你可以看到我做了什麼。 DevSqlSerrver我給出了 的freetds.cof。所以我需要給服務器名稱,我在freetds.conf中給出了 。

    // Connect to MSSQL 
        $connection = mssql_connect("DevSqlServer", 'sa', '****'); 
        $db_connect=mssql_select_db("DBname",$connection); 
    

,您可以訪問MSSQL Server數據庫。

欲瞭解更多信息:在您的SQL

Link1

Link2