簡體   English   中英

將php mysql更改為php mssql

[英]Change php mysql to php mssql

大家好。 我有這段代碼可以完美地在mysql數據庫上工作

<?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@gmail.com", "database error", $e->getMessage(), "From: email@gmail.com");
}

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

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "email@gmail.com";
    $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._%-]+@[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
}
?>

當某人選擇一個州並提交Web表單時,此代碼將連接到mysql數據庫並

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

我的問題是我正在為此工作的人不想使用mysql數據庫,所以想要它

在mssql數據庫中集成。

我在上面的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);
?>

任何幫助使它起作用的方法將不勝感激。 感謝大家

阿里

似乎您沒有在php.ini文件中啟用MSSQL擴展。

在Linux中

  • 您將在php.ini文件: ;extension=mssql.so找到它,並替換為: extension=mssql.so (只需刪除分號即可啟用。)

  • 您可以在lampp / etc / freetds.conf文件中找到一個文件(如果使用的是xampp linux)。 在此文件中,您需要添加以下配置。

    添加:

    [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數據庫。

有關更多信息:

鏈接1

LINK2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM