473,490 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Change php mysql to php mssql

14 New Member
Hey Everyone hope all well. I have this code that works perfectly on a mysql database

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // visit http://php.net/pdo for more details
  3. // start error handling
  4.  
  5. try 
  6. {
  7.   // connect
  8.   $pdo = new PDO('mysql:host=localhost;dbname=name', 'name', 'password');
  9.   // enable error handling through exceptions
  10.   $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11.   // create safe query
  12.   $query = $pdo->prepare("SELECT ip FROM tester WHERE state = ? ORDER BY RAND
  13.  
  14. (UNIX_TIMESTAMP(NOW())) LIMIT 1");
  15.  
  16.   // pass data & execute query (since the data are of string type
  17.   // and therefore can be passed in this lazy way)
  18.   $query->execute(array($_POST['State']));
  19.   // get value
  20.   $ip = $query->fetchColumn();
  21.   // print out the IP address using $ip
  22. }
  23. catch (Exception $e)
  24. {
  25.   echo "sorry, there was an error.";
  26.   mail("email@gmail.com", "database error", $e->getMessage(), "From: email@gmail.com");
  27. }
  28.  
  29. if(isset($_POST['email'])) {
  30.  
  31.     // EDIT THE 2 LINES BELOW AS REQUIRED
  32.     $email_to = "email@gmail.com";
  33.     $email_subject = "This is a test";
  34.  
  35.  
  36.     function died($error) {
  37.         // your error code can go here
  38.         echo "We are very sorry, but there were error(s) found with the form you submitted. ";
  39.         echo "These errors appear below.<br /><br />";
  40.         echo $error."<br /><br />";
  41.         echo "Please go back and fix these errors.<br /><br />";
  42.         die();
  43.     }
  44.  
  45.     // validation expected data exists
  46.     if(!isset($_POST['first_name']) ||
  47.         !isset($_POST['last_name']) ||
  48.         !isset($_POST['email']) ||
  49.         !isset($_POST['what']) ||
  50.         !isset($_POST['State']) ||
  51.         !isset($_POST['comments'])) {
  52.         died('We are sorry, but there appears to be a problem with the form you submitted.');       
  53.     }
  54.  
  55.     $what = $_POST['what']; // required
  56.     $first_name = $_POST['first_name']; // required
  57.     $last_name = $_POST['last_name']; // required
  58.     $email_from = $_POST['email']; // required
  59.     $state = $_POST['State']; // not required
  60.     $comments = $_POST['comments']; // required
  61.  
  62.     $error_message = "";
  63.     $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  64.   if(!preg_match($email_exp,$email_from)) {
  65.     $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  66.   }
  67.     $string_exp = "/^[A-Za-z .'-]+$/";
  68.   if(!preg_match($string_exp,$first_name)) {
  69.     $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  70.   }
  71.   if(!preg_match($string_exp,$last_name)) {
  72.     $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  73.   }
  74.   if(strlen($comments) < 2) {
  75.     $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  76.   }
  77.   if(strlen($error_message) > 0) {
  78.     died($error_message);
  79.   }
  80.     $email_message = "Form details below.\n\n";
  81.  
  82.     function clean_string($string) {
  83.       $bad = array("content-type","bcc:","to:","cc:","href");
  84.       return str_replace($bad,"",$string);
  85.     }
  86.  
  87.     $email_message .= "First Name: ".clean_string($first_name)."\n";
  88.     $email_message .= "Last Name: ".clean_string($last_name)."\n";
  89.     $email_message .= "What: ".clean_string($what)."\n";
  90.     $email_message .= "Email: ".clean_string($email_from)."\n";
  91.     $email_message .= "State: ".clean_string($ip)."\n";
  92.     $email_message .= "Comments: ".clean_string($comments)."\n";
  93. // create email headers
  94. $headers = 'From: '.$email_from."\r\n".
  95. 'Reply-To: '.$email_from."\r\n" .
  96. 'X-Mailer: PHP/' . phpversion();
  97. if (!mail($email_to, $email_subject, $email_message, $headers))
  98. {
  99.     echo "failed to send message";
  100. }  
  101.  
  102. ?>
  103.  
  104. <!-- include your own success html here -->
  105.  
  106. Thank you for contacting us. We will be in touch with you very soon.
  107.  
  108. <?php
  109. }
  110. ?>
When someone selects a state and submits the web form, this code connects to the mysql database and

selects an ip address randomly and sends it to my email. It is perfect.

The problem i have is that the person i am doing this for does not want to use a mysql database so wants it

intergrated in a mssql database.

I know in the php code above that i am connecting to a mysql database instead of a mssql database but

can not work out what needs to be changed to make it all work.

I did find this online and thought i might be able to intergrate it with my exsisting code but so far no luck.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $myServer = "localhost";
  3. $myUser = "your_name";
  4. $myPass = "your_password";
  5. $myDB = "examples"; 
  6.  
  7. //connection to the database
  8. $dbhandle = mssql_connect($myServer, $myUser, $myPass)
  9.   or die("Couldn't connect to SQL Server on $myServer"); 
  10.  
  11. //select a database to work with
  12. $selected = mssql_select_db($myDB, $dbhandle)
  13.   or die("Couldn't open database $myDB"); 
  14.  
  15. //declare the SQL statement that will query the database
  16. $query = "SELECT id, name, year ";
  17. $query .= "FROM cars ";
  18. $query .= "WHERE name='BMW'"; 
  19.  
  20. //execute the SQL query and return records
  21. $result = mssql_query($query);
  22.  
  23. $numRows = mssql_num_rows($result); 
  24. echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 
  25.  
  26. //display the results 
  27. while($row = mssql_fetch_array($result))
  28. {
  29.   echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
  30. }
  31. //close the connection
  32. mssql_close($dbhandle);
  33. ?>
Any help in getting this to work would be much appreciated. Thanks Everyone

Ali
Nov 20 '11 #1
1 2029
ck9663
2,878 Recognized Expert Specialist
This is more of PHP question than a sql server question.

Good Luck!!!


~~ CK
Nov 22 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
4372
by: badz | last post by:
Hi frenz Lately I try to use MSSQL and PHP , the problem arise when PHP try to read MSSQL field with 'image' data type, header("Content-type: image/jpeg"); // act as a jpg file to browser I...
0
3962
by: laredotornado | last post by:
Hi, I have both MySQL 5 client and server running on a Linux Fedora Core 5 machine (x86 Desktop PC). Regarding hte client, what can I do to change the default socket location? Right now, I'm...
0
1134
by: MomentisMan | last post by:
I have an app with a MySQL backend and MS-Access frontend. I have a table on the database (tblTime) with three Decimal columns. When I connect to the table through ODBC in Access, the precision...
0
1252
by: MomentisMan | last post by:
I have an app with a MySQL backend and MS-Access frontend. I have a table on the database (tblTime) with three Decimal columns. When I connect to the table through ODBC in Access, the precision...
3
12290
by: tanyali | last post by:
I am using Linux RedHat , MySql Ver 14.12 Distrib 5.0.18 not enough disk space left on /dev/sda7, which I am working on, I insert BLOBs into database and the table is as big as 5.54468 GB I...
1
2296
by: frank63 | last post by:
Hello , can someone tell me a simple way / tool how to parse data (keyword and following data ) from txt files into a SQL (Mysql MsSql ) database ? I am total newcomer, so apologize if this a...
0
13811
JamieHowarth0
by: JamieHowarth0 | last post by:
I have been trying to find a solution to this on the Internet for months. Literally, ages and ages and ages, praying that someone in the open-source community has enough knowledge to put together an...
2
5627
by: tariquetuku | last post by:
Can anyone plz advice, how to change mysql date format to (d-m-Y) permanently which will affect all the databases in the mysql server. i.e, i want to change the date format in one place which will...
0
1550
by: johnyjj2 | last post by:
Hello! I've got web application written in MSSQL and I need to run it on server. But on the server I've go MySQL (not MSSQL), PHP, KFWS. Do I have to install MSSQL and have both MySQL and MSSQL?...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.