Tutoriel N° 2ea
Connect to database using php mysqli – procedural way
Extraction exemple with mysqli_fetch_row
<?
$base='XXX';
$login='XXX';
$pwd='XXX';
$host='localhost';
$link = mysqli_connect("$host", "$login", "$pwd", "$base");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$sql="SELECT field1,field2 from table where conditions";
if ($res = mysqli_query($link, $sql)) {
$num = mysqli_num_rows($res);
while ($row = mysqli_fetch_row($res)) {
$field1 = $row[0];
$field2 = $row[1];
echo $field1." ".$field2."<br />";
}
mysqli_free_result($res);
}
mysqli_close($link);
?>
$base='XXX';
$login='XXX';
$pwd='XXX';
$host='localhost';
$link = mysqli_connect("$host", "$login", "$pwd", "$base");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$sql="SELECT field1,field2 from table where conditions";
if ($res = mysqli_query($link, $sql)) {
$num = mysqli_num_rows($res);
while ($row = mysqli_fetch_row($res)) {
$field1 = $row[0];
$field2 = $row[1];
echo $field1." ".$field2."<br />";
}
mysqli_free_result($res);
}
mysqli_close($link);
?>
Extraction exemple with mysqli_fetch_assoc
<?
$base='XXX';
$login='XXX';
$pwd='XXX';
$host='localhost';
$link = mysqli_connect("$host", "$login", "$pwd", "$base");
if (mysqli_connect_errno()) {
echo "Could not connect to database: Error: ".mysqli_connect_error();
exit();
}
$sql="SELECT field1,field2 from table where conditions";
if ($res = mysqli_query($link, $sql)) {
$num = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
echo $row["id_moteur"]." ".$row["site"]."<br />";
}
mysqli_free_result($res);
}
mysqli_close($link);
?>
$base='XXX';
$login='XXX';
$pwd='XXX';
$host='localhost';
$link = mysqli_connect("$host", "$login", "$pwd", "$base");
if (mysqli_connect_errno()) {
echo "Could not connect to database: Error: ".mysqli_connect_error();
exit();
}
$sql="SELECT field1,field2 from table where conditions";
if ($res = mysqli_query($link, $sql)) {
$num = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
echo $row["id_moteur"]." ".$row["site"]."<br />";
}
mysqli_free_result($res);
}
mysqli_close($link);
?>
Update request
<?
$base='XXX';
$login='XXX';
$pwd='XXX';
$host='localhost';
$link = mysqli_connect("$host", "$login", "$pwd", "$base");
if (mysqli_connect_errno()) {
echo "Could not connect to database: Error: ".mysqli_connect_error();
exit();
}
$sql="UPDATE table set field='value'";
mysqli_query($link, $sql);
mysqli_close($link);
?>
$base='XXX';
$login='XXX';
$pwd='XXX';
$host='localhost';
$link = mysqli_connect("$host", "$login", "$pwd", "$base");
if (mysqli_connect_errno()) {
echo "Could not connect to database: Error: ".mysqli_connect_error();
exit();
}
$sql="UPDATE table set field='value'";
mysqli_query($link, $sql);
mysqli_close($link);
?>