将php mysql更改为php mssql

将php mysql更改为php mssql,php,mysql,sql-server,database,Php,Mysql,Sql Server,Database,嘿,大家都希望一切顺利。我有这段代码,它可以在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 exce

嘿,大家都希望一切顺利。我有这段代码,它可以在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文件中找到这一点:
    ;扩展名=mssql.so
    并替换为:
    extension=mssql.so
    (只需删除 要启用的分号。)

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

    添加以下内容:

    [DevSqlServer]

    主机=172.16.1.123

    实例=DEVAPSERVER

    端口=1433

    tds版本=7.0

  • 重新启动服务器。你的php代码应该是这样的

  • 在这里你可以看到我做了什么DevSqlSerrver我已经放弃了 免费咖啡。所以我需要给服务器起一个名字 在freetds.conf中给出

      // Connect to MSSQL
       $connection = mssql_connect("DevSqlServer", 'sa', '****');
       $db_connect=mssql_select_db("DBname",$connection);
    
您可以访问MSSQL服务器数据库

有关更多信息:


在sql中使用mysql函数。你应该调整它们。(rand是mysql而不是sql)你能发布你收到的任何错误吗?发布你收到的错误,并指出它指向触发错误的那一行,这样我们可以进一步查看我在电子邮件中收到的错误是数据库错误找不到驱动程序也Bram,如果我接受rand的订单,我如何让它获取随机ip。感谢大家提供的所有帮助,RAND()应该可以工作,但您需要使用
SELECT TOP 1
而不是
ORDER BY RAND()Limit 1
。您可能还需要更改unix_时间戳(now())。如果您经常这样做,那么我可以强烈推荐O'Reilly出版的SQL Cookbook,它在我在数据库之间迁移时确实帮助了我解决了这种情况