xxxxxxxxxx
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
xxxxxxxxxx
<?php
$hostName = "localhost";
$userName = "root";
$password = "";
$dbName = "database name";
$conn= new mysqli($hostName,$userName,$password,$dbName);
if($conn){
echo "connected";
}else{
echo "not connected";
}
?>
xxxxxxxxxx
<?php
$hostName = "localhost";
$userName = "root";
$password = "";
$dbName = "database name";
$conn= new mysqli($hostName,$userName,$password,$dbName);
if($conn){
echo "connected";
}else{
echo "not connected";
}
?>
xxxxxxxxxx
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
xxxxxxxxxx
<?php
$servername = "localhost"; // Change this to your MySQL server hostname or IP address
$username = "your_username"; // Change this to your MySQL username
$password = "your_password"; // Change this to your MySQL password
$database = "your_database"; // Change this to your MySQL database name
// Create a connection
$conn = new mysqli($servername, $username, $password, $database);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully!";
?>
xxxxxxxxxx
SQL SERVER 2012 conexion in PHP
<?php
$serverName = "SERVER"; //serverName\instanceName
$connectionInfo = array( "Database"=>"base", "UID"=>"ex", "PWD"=>"******");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
;
if( $conn ) {
echo "Conexión establecida.<br />";
}else{
echo "Conexión no se pudo establecer.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT name FROM [base].[dbo].[table]";
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt = sqlsrv_query( $conn, $sql , $params, $options );
$row_count = sqlsrv_num_rows( $stmt );
if ($row_count === false)
echo "Error al obtener datos.";
else
echo "bien";
//echo $row_count;
while( $row = sqlsrv_fetch_array( $stmt) ) {
print json_encode($row);
}
sqlsrv_close($conn);
?>
xxxxxxxxxx
This is an update to a note I wrote earlier concerning how to set multiple attributes when you create you PDO connection string.
You can put all the attributes you want in an associative array and pass that array as the fourth parameter in your connection string. So it goes like this:
<?php
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING
];
// Now you create your connection string
try {
// Then pass the options as the last parameter in the connection string
$connection = new PDO("mysql:host=$host; dbname=$dbname", $user, $password, $options);
// That's how you can set multiple attributes
} catch(PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
?>