简体   繁体   中英

PHP script to connect to SQL Server

I am running this script

<?php
$mysqli = mysqli_connect("ServerName", "username", "password", "dbname");
$res = mysqli_query($mysqli, "SELECT * from table1");
$row = mysqli_fetch_assoc($res);
echo $row['_msg'];
?>

I am getting this error:

mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it

I have looked around online, but I can't figure out what is wrong. All the credentials are correct and it still doesn't work.

I am connecting to a local db on SQL Server through SQL Server authentication. Do I need MySQL running on xamp for this to work? Any ideas?

I am using Windows 10 through bootcamp on a mac. This is for SQL Server, not for mysql

It seems that you want to connect to a Microsoft SQL Server database. In this case maybe this code helps:

<?php
$serverName = "serverName\\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

Also, if you run your php server locally with XAMPP you need to have Apache activated. If you try to connect to a MS SQL Server database you dont have to have MySQL enabled since you are not trying to connect to a MySQL database.

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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