Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to connect database in php

<?php
 $hostName = "localhost";
 $userName = "root";
 $password = "";
 $dbName = "database name";
 $conn= new mysqli($hostName,$userName,$password,$dbName);
 if($conn){
    echo "connected";
 }else{
    echo "not connected";
 }

  ?>
Comment

how to connect to a database in php

<?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";
?>
Comment

create a php database connection

<?php
 
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
 
// Create connection
$conn = new mysqli($servername,
    $username, $password, $dbname);
 
// Check connection
if ($conn->connect_error) {
    die("Connection failed: "
        . $conn->connect_error);
}
 
$sqlquery = "INSERT INTO table VALUES
    ('John', 'Doe', 'john@example.com')"
 
if ($conn->query($sql) === TRUE) {
    echo "record inserted successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
Comment

how to connect sql database inside php

<?php
    $dbServername = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbName = "dbname";
    
    $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
	
    $sql = "SELECT * FROM dbname;";
    $result = mysqli_query($conn, $sql);
    $resultCheck = mysqli_num_rows($result);

    if ($resultCheck > 0) {
      while ($row = mysqli_fetch_assoc($result)) {
        echo $row['sql_row_name'] . "<br>";
      }
    }
?>
Comment

connect sql server php

//Connect MSSQL
$serverName = '192.xxx.xxx.xx';
$userName = 'usertest';
$userPassword = 'pwdtest';
$dbName = 'dbtest';
 
try{
	$conn = new PDO("sqlsrv:server=$serverName ; Database = $dbName", $userName, $userPassword);
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e){
	die(print_r($e->getMessage()));
}
 
//การ query และแสดงข้อมูล จัดเรียงตามฟิวด์ field1 แบบมากไปน้อย เริ่มที่เรคคอร์ดที่ 0-100
$query = " SELECT * FROM tbl 
ORDER BY field1 DESC
OFFSET 0 ROWS
FETCH NEXT 100 ROWS ONLY ";
$getRes = $conn->prepare($query);
$getRes->execute();
 
while($row = $getRes->fetch( PDO::FETCH_ASSOC ))
{
echo $row['field1']."<br />";
echo $row['field2']."<br />";
}
Comment

connect php in sql server

SQL SERVER 2012 conexion in PHP
<?php
    $serverName = "SERVER"; //serverNameinstanceName
    $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);
?>
Comment

connect php to db

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());
}
?>
Comment

PREVIOUS NEXT
Code Example
Php :: foreach limit laravel 
Php :: upload file in wp from url 
Php :: in random order laravel 
Php :: current timestamp carbon 
Php :: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress. 
Php :: change background color php 
Php :: laravel check if eloquent just created 
Php :: default php timezone to newyork 
Php :: php self 
Php :: carbon day 30 days ago 
Php :: pre_r 
Php :: composer require laravel/ui not working laravel 7 
Php :: setup_postdata not working 
Php :: cakephp 404 exception 
Php :: How to change add to cart button in wordpress 
Php :: Wordpress hook for newly published post 
Php :: setcookie php 
Php :: php read xml from url 
Php :: intl extension php ubuntu 
Php :: laravel sum group by 
Php :: get current page slug 
Php :: switch in laravel 
Php :: repeater acf 
Php :: wordpress check shortcode exists 
Php :: Wordpress Pagination for WP_Query 
Php :: add dd function composer 
Php :: get all post meta 
Php :: header.php file how to fetch in index.php file in wordpress 
Php :: Search for text in string php 
Php :: calcul age php datetime 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =