Diferència entre revisions de la pàgina «A1- Accés a BD amb PHP (10h)»
De wikiserver
Línia 8: | Línia 8: | ||
== Mysqli Procedural == | == Mysqli Procedural == | ||
+ | <source lang="php"> | ||
+ | <?php | ||
+ | $servername = "localhost"; | ||
+ | $username = "username"; | ||
+ | $password = "password"; | ||
+ | // Create connection | ||
+ | $conn = mysqli_connect($servername, $username, $password); | ||
+ | |||
+ | // Check connection | ||
+ | if (!$conn) { | ||
+ | die("Connection failed: " . mysqli_connect_error()); | ||
+ | } | ||
+ | echo "Connected successfully"; | ||
+ | ?> | ||
+ | </source> | ||
Revisió del 12:57, 17 feb 2015
Connexió a BD amb PHP
La base de dades a utilitzar serà mysql que haureu d'instal·la en el servidor Web.
Per connectar-nos a BD podem utilitzar tres maneres:
- mysqli procedural
- mysqli object-oriented
- PDO (PHP Data Objects)
Mysqli Procedural
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Mysqli object-oriented
<?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";
?>