Skip to content

Instantly share code, notes, and snippets.

@Prroffessorr
Created April 19, 2019 17:39
Show Gist options
  • Save Prroffessorr/2e9c5922fe5e244bfdf44585d8b715ad to your computer and use it in GitHub Desktop.
Save Prroffessorr/2e9c5922fe5e244bfdf44585d8b715ad to your computer and use it in GitHub Desktop.
Exaple connection wirh DB using PDO(SQLIte,SQL) and SQLI, with examples of requests
<?php
//MAKE YOUR CHOICE!!!!!!!!
//PDO SQL
error_reporting(E_ALL);
try {
$dsn="mysql:host=host;dbname=db_name; charset=utf8";
$dbh=new PDO($dsn,'root','');
}
catch(PDOException $e ){
print "Error".$e->getMessage()."<br/>";
die();
}
?>
<?php
// SQLITE
$myPDO=new PDO('sqlite:db_name.sqlite');
//exaple of create table
$myPDO->exec(
" CREATE TABLE table_name
(
ID INTEGER PRIMARY KEY,
field1 TEXT,
field2 TEXT,
field3 INTEGER,
field4 INTEGER
)
"
);
//filling the database
$myPDO->exec("INSERT INTO equipments (field1, field2,field3,field4)
VALUES ('exaple1_field1', 'exaple2_field2',2019,10)");
?>
<?php
//MYSQLI
$mysqli = new mysqli('host', 'user_name', 'password', 'db_name');
// Check if the connection succeeded.
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// Set the connection encoding
$mysqli->set_charset('utf8');
// example
$sql = 'SELECT * FROM Test_Table WHERE Test="Table_Table"';
$result = $mysqli->query($sql);
// Freeing memory
$result->free();
// Close the connection
$mysqli->close();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment