connection query by php code example
Example 1: php sql connection
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn= mysqli_connect($servername,$username,$password,$dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected Successfully.";
?>
Example 2: query php
<?php
$mysqli = new mysqli("localhost", "my_username", "my_password", "my_database");
if($mysqli->connect_error) {
echo "<b>Failed to connect to MySQL: </b>" . $mysqli->connect_error;
}
$selectQuery = $mysqli->prepare("SELECT * FROM my_table");
$selectQuery->execute();
$selectQueryResult = $selectQuery->get_result();
while($selectQueryRow = $selectQueryResult->fetch_array()) {
echo $selectQueryRow['my_column'];
}
$selectUsername = $mysqli->prepare("SELECT * FROM users");
$selectUsername->execute();
$selectUsernameResult = $selectUsername->get_result();
while($selectUsernameRow = $selectUsernameResult->fetch_array()) {
echo $selectUsernameRow['username'];
}