【发布时间】:2011-11-20 02:03:40
【问题描述】:
大家好,希望一切顺利。我有这段代码可以在 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);
?>
任何帮助使其工作的帮助将不胜感激。谢谢大家
阿里
【问题讨论】:
-
您在 sql 中使用 mysql 函数。你应该调整它们。 (rand 是 mysql 而不是 sql)
-
你能发布你遇到的任何错误吗?
-
发布您收到的错误,并指出它指向的触发错误的行,以便我们进一步查看
-
我在电子邮件中收到的错误是数据库错误找不到驱动程序还有 Bram,如果我接受 ORDER BY RAND,如何让它获取随机 IP。感谢大家的帮助
-
Rand() 应该可以工作,但您需要使用
SELECT TOP 1而不是ORDER BY RAND() Limit 1。您可能还需要更改 unix_timestamp(now())。如果您要经常这样做,那么我强烈推荐 O'Reilly 出版的 SQL Cookbook,当我在数据库之间迁移时,它确实帮助我解决了这种情况
标签: php mysql sql-server database