简体   繁体   English

如何使用php连接到mssql?

[英]How to connect to mssql with php?

Thing i got all the addons working, some unofficial pdo driver for php(5.6) 我所有的插件都正常工作,一些非官方的PDO驱动程序适用于php(5.6)

This is my php; 这是我的PHP;

<?php
$head =  $_POST['title'];
$bread = $_POST['html'];
$author = $_POST['selectlist1'];
$postdate = date('y-m-d h:i:s',time());
$cat = $_POST['selectlist2'];
$serverName = 'SERVER\SQLEXPRESS';
$db = new PDO('sqlsrv:Server=$serverName;Database=blog', 'sa', '******');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = 'INSERT INTO dbo.blog_posts (blog_title, blog_post, blog_author, blog_date, blog_category) VALUES (:head, :bread, :author, :postdate, :cat)';

$query = $db->prepare( $sql );
$query->execute( array(':head'=>$head, ':bread'=>$bread, ':author'=>$author, ':postdate'=>$postdate, ':cat'=>$cat ) );

?>

And i get the error: 而且我得到了错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[08001]: [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. 致命错误:消息为'SQLSTATE [08001]的未捕获异常'PDOException':[Microsoft] [SQL Server本机客户端11.0]命名管道提供程序:无法打开与SQL Server [53]的连接。 ' in C:\\inetpub\\wwwroot\\blog\\post.php:9 Stack trace: #0 C:\\inetpub\\wwwroot\\blog\\post.php(9): PDO->__construct('sqlsrv:Server=$...', 'sa', '*****') #1 {main} thrown in C:\\inetpub\\wwwroot\\blog\\post.php on line 9 在C:\\ inetpub \\ wwwroot \\ blog \\ post.php:9中堆栈跟踪:#0 C:\\ inetpub \\ wwwroot \\ blog \\ post.php(9):PDO-> __ construct('sqlsrv:Server = $。 。','sa','*****')#1 {main}在第9行的C:\\ inetpub \\ wwwroot \\ blog \\ post.php中引发

i have enabled named pipes etc i have no more ideas what im doing wrong... i have googled several hours... instead of $serverName i have tried with localhost,127.0.0.1 etcetc but i cant get it working! 我启用了命名管道等,我不知道我在做什么错...我用了Google几个小时...而不是$ serverName我尝试使用localhost,127.0.0.1 etcetc但我无法使它正常工作!

after some more digging around i found out that Named Pipes Provider: Could not open a connection to SQL Server [53] is some kind of network problem, dunno what problem tho... 经过更多的挖掘之后,我发现命名管道提供者:无法打开与SQL Server [53]的连接是某种网络问题,不知道是什么问题。

Oke i think im something on the way, now i get: 好吧,我认为我正在前进,现在我得到了:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[22007]: [Microsoft][SQL Server Native Client 11.0][SQL Server]The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.' 致命错误:消息为'SQLSTATE [22007]的未捕获异常'PDOException':[Microsoft] [SQL Server Native Client 11.0] [SQL Server]将nvarchar数据类型转换为datetime数据类型导致超出范围值。' in C:\\inetpub\\wwwroot\\blog\\post.php:15 Stack trace: #0 C:\\inetpub\\wwwroot\\blog\\post.php(15): PDOStatement->execute(Array) #1 {main} thrown in C:\\inetpub\\wwwroot\\blog\\post.php on line 15 在C:\\ inetpub \\ wwwroot \\ blog \\ post.php:15中堆栈跟踪:#0 C:\\ inetpub \\ wwwroot \\ blog \\ post.php(15):PDOStatement-> execute(Array)#1 {main}被抛出第15行的C:\\ inetpub \\ wwwroot \\ blog \\ post.php

i have done so many changes to everything and nothing tbh, but now it finally works, at the end my code looks like this: 我对所有内容都做了很多更改,但是什么也没做,但是现在终于可以了,最后我的代码如下所示:

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$head =  $_POST['title'];
$bread = $_POST['html'];
$author = $_POST['selectlist1'];
$postdate = date('Y-m-d H:i:s');
$cat = $_POST['selectlist2'];
$db = new PDO('sqlsrv:server=localhost;Database=blog', '****', '******');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = 'INSERT INTO dbo.blog_posts (blog_title, blog_post, blog_author, blog_date, blog_category) VALUES (:head, :bread, :author, :postdate, :cat)';

$query = $db->prepare( $sql );
$query->execute( array(':head'=>$head, ':bread'=>$bread, ':author'=>$author, ':postdate'=>$postdate, ':cat'=>$cat ) );

?>

So basically the thing ive tried all along did work, i have changed some ownership thingys in ms sql server management studio etc, i think that did the thing for me! 因此,基本上我一直尝试的东西都可以正常工作,我在ms sql服务器管理工​​作室等中更改了一些所有权东西,我认为这对我来说是成功的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM