{"id":1729,"date":"2026-06-17T07:02:06","date_gmt":"2026-06-17T07:02:06","guid":{"rendered":"https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/"},"modified":"2026-07-25T15:38:42","modified_gmt":"2026-07-25T13:38:42","password":"","slug":"exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql","status":"publish","type":"docs","link":"https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/","title":{"rendered":"Exemplu de cod PHP pentru conectarea la o baz\u0103 de date MS SQL"},"content":{"rendered":"<p>Exemplu de conectare la o baz\u0103 de date MS SQL folosind extensia <code>sqlsrv<\/code> (disponibil\u0103 pe g\u0103zduirea Windows):<\/p>\n<pre>&lt;?php\n$serverName = \"SQL_SERVER\";\n$connectionInfo = array(\n    \"UID\"      =&gt; \"sqlusername\",\n    \"PWD\"      =&gt; \"sqlpassword\",\n    \"Database\" =&gt; \"DBName\"\n);\n\n$conn = sqlsrv_connect($serverName, $connectionInfo);\nif ($conn === false) {\n    die(print_r(sqlsrv_errors(), true));\n}\n\n$tsql = \"SELECT id, FirstName, LastName, Email FROM tblContact\";\n$stmt = sqlsrv_query($conn, $tsql);\n\nwhile ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) {\n    echo $row[0] . \" \" . $row[1] . \" \" . $row[2] . \"&lt;br&gt;\";\n}\n\nsqlsrv_free_stmt($stmt);\nsqlsrv_close($conn);\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Exemplu de conectare la o baz\u0103 de date MS SQL folosind extensia sqlsrv (disponibil\u0103 pe g\u0103zduirea Windows): &lt;?php $serverName = &#8222;SQL_SERVER&#8221;; $connectionInfo = array( &#8222;UID&#8221; =&gt; &#8222;sqlusername&#8221;, &#8222;PWD&#8221; =&gt; &#8222;sqlpassword&#8221;, &#8222;Database&#8221; =&gt; &#8222;DBName&#8221; ); $conn = sqlsrv_connect($serverName, $connectionInfo); if ($conn === false) { die(print_r(sqlsrv_errors(), true)); } $tsql = &#8222;SELECT id, FirstName, LastName, Email FROM tblContact&#8221;; &#8230; <a title=\"Exemplu de cod PHP pentru conectarea la o baz\u0103 de date MS SQL\" class=\"read-more\" href=\"https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/\" aria-label=\"Cite\u0219te mai multe despre Exemplu de cod PHP pentru conectarea la o baz\u0103 de date MS SQL\">Cite\u0219te mai mult<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"whmcs_kb_id":1046,"footnotes":""},"doc_category":[169],"doc_tag":[231,227,376,247],"class_list":["post-1729","docs","type-docs","status-publish","hentry","doc_category-php","doc_tag-baza-de-date","doc_tag-configurare","doc_tag-ms-sql","doc_tag-php"],"year_month":"2026-11","word_count":85,"total_views":"29","reactions":{"happy":"0","normal":"0","sad":"0"},"author_info":{"name":"Adrian Firican","author_nicename":"techeditor1","author_url":"https:\/\/www.hostx.ro\/kb\/author\/techeditor1\/"},"doc_category_info":[{"term_name":"PHP","term_url":"https:\/\/www.hostx.ro\/kb\/docs-category\/php\/"}],"doc_tag_info":[{"term_name":"baz\u0103 de date","term_url":"https:\/\/www.hostx.ro\/kb\/docs-tag\/baza-de-date\/"},{"term_name":"configurare","term_url":"https:\/\/www.hostx.ro\/kb\/docs-tag\/configurare\/"},{"term_name":"MS SQL","term_url":"https:\/\/www.hostx.ro\/kb\/docs-tag\/ms-sql\/"},{"term_name":"PHP","term_url":"https:\/\/www.hostx.ro\/kb\/docs-tag\/php\/"}],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Conectare MS SQL \u00een PHP cu sqlsrv - Cod Exemplu - Centrul de suport HostX<\/title>\n<meta name=\"description\" content=\"Cod PHP pentru conectare la baz\u0103 de date MS SQL folosind extensia sqlsrv. Tutorial cu exemplu complet de interogare \u0219i prelucrare date.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/\" \/>\n<meta property=\"og:locale\" content=\"ro_RO\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Conectare MS SQL \u00een PHP cu sqlsrv - Cod Exemplu - Centrul de suport HostX\" \/>\n<meta property=\"og:description\" content=\"Cod PHP pentru conectare la baz\u0103 de date MS SQL folosind extensia sqlsrv. Tutorial cu exemplu complet de interogare \u0219i prelucrare date.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/\" \/>\n<meta property=\"og:site_name\" content=\"Centrul de suport HostX\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-25T13:38:42+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hostx.ro\\\/kb\\\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\\\/\",\"url\":\"https:\\\/\\\/www.hostx.ro\\\/kb\\\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\\\/\",\"name\":\"Conectare MS SQL \u00een PHP cu sqlsrv - Cod Exemplu - Centrul de suport HostX\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hostx.ro\\\/kb\\\/#website\"},\"datePublished\":\"2026-06-17T07:02:06+00:00\",\"dateModified\":\"2026-07-25T13:38:42+00:00\",\"description\":\"Cod PHP pentru conectare la baz\u0103 de date MS SQL folosind extensia sqlsrv. Tutorial cu exemplu complet de interogare \u0219i prelucrare date.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hostx.ro\\\/kb\\\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\\\/#breadcrumb\"},\"inLanguage\":\"ro-RO\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hostx.ro\\\/kb\\\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hostx.ro\\\/kb\\\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Prima pagin\u0103\",\"item\":\"https:\\\/\\\/www.hostx.ro\\\/kb\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"InfoCentru\",\"item\":\"https:\\\/\\\/www.hostx.ro\\\/kb\\\/docs\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Exemplu de cod PHP pentru conectarea la o baz\u0103 de date MS SQL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hostx.ro\\\/kb\\\/#website\",\"url\":\"https:\\\/\\\/www.hostx.ro\\\/kb\\\/\",\"name\":\"Centrul de suport HostX\",\"description\":\"tutoriale gazduire web, domenii web \u0219i servere\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hostx.ro\\\/kb\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ro-RO\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Conectare MS SQL \u00een PHP cu sqlsrv - Cod Exemplu - Centrul de suport HostX","description":"Cod PHP pentru conectare la baz\u0103 de date MS SQL folosind extensia sqlsrv. Tutorial cu exemplu complet de interogare \u0219i prelucrare date.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/","og_locale":"ro_RO","og_type":"article","og_title":"Conectare MS SQL \u00een PHP cu sqlsrv - Cod Exemplu - Centrul de suport HostX","og_description":"Cod PHP pentru conectare la baz\u0103 de date MS SQL folosind extensia sqlsrv. Tutorial cu exemplu complet de interogare \u0219i prelucrare date.","og_url":"https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/","og_site_name":"Centrul de suport HostX","article_modified_time":"2026-07-25T13:38:42+00:00","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/","url":"https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/","name":"Conectare MS SQL \u00een PHP cu sqlsrv - Cod Exemplu - Centrul de suport HostX","isPartOf":{"@id":"https:\/\/www.hostx.ro\/kb\/#website"},"datePublished":"2026-06-17T07:02:06+00:00","dateModified":"2026-07-25T13:38:42+00:00","description":"Cod PHP pentru conectare la baz\u0103 de date MS SQL folosind extensia sqlsrv. Tutorial cu exemplu complet de interogare \u0219i prelucrare date.","breadcrumb":{"@id":"https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/#breadcrumb"},"inLanguage":"ro-RO","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.hostx.ro\/kb\/exemplu-de-cod-php-pentru-conectarea-la-baza-de-date-ms-sql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Prima pagin\u0103","item":"https:\/\/www.hostx.ro\/kb\/"},{"@type":"ListItem","position":2,"name":"InfoCentru","item":"https:\/\/www.hostx.ro\/kb\/docs\/"},{"@type":"ListItem","position":3,"name":"Exemplu de cod PHP pentru conectarea la o baz\u0103 de date MS SQL"}]},{"@type":"WebSite","@id":"https:\/\/www.hostx.ro\/kb\/#website","url":"https:\/\/www.hostx.ro\/kb\/","name":"Centrul de suport HostX","description":"tutoriale gazduire web, domenii web \u0219i servere","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hostx.ro\/kb\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ro-RO"}]}},"knowledge_base_info":[],"knowledge_base_slug":[],"_links":{"self":[{"href":"https:\/\/www.hostx.ro\/kb\/wp-json\/wp\/v2\/docs\/1729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostx.ro\/kb\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/www.hostx.ro\/kb\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostx.ro\/kb\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostx.ro\/kb\/wp-json\/wp\/v2\/comments?post=1729"}],"version-history":[{"count":2,"href":"https:\/\/www.hostx.ro\/kb\/wp-json\/wp\/v2\/docs\/1729\/revisions"}],"predecessor-version":[{"id":5201,"href":"https:\/\/www.hostx.ro\/kb\/wp-json\/wp\/v2\/docs\/1729\/revisions\/5201"}],"wp:attachment":[{"href":"https:\/\/www.hostx.ro\/kb\/wp-json\/wp\/v2\/media?parent=1729"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/www.hostx.ro\/kb\/wp-json\/wp\/v2\/doc_category?post=1729"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/www.hostx.ro\/kb\/wp-json\/wp\/v2\/doc_tag?post=1729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}