2011-11-20 67 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