Deprecated : mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
As the warning suggest use either pdo or mysqli.
mysqli :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sanpham";
// Create connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');
$sql = "SELECT * FROM cpu";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$rows = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($rows);
} else {
echo "no results found";
}
PDO :
<?php
$host = 'localhost';
$db = 'sanpham';
$user = 'root';
$pass = '';
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$dbh = new PDO($dsn, $user, $pass, $opt);
$result = $dbh->query("SELECT * FROM cpu");
$rows = $result->fetchAll();
echo json_encode($rows);
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\project\app\server\data\data-cpu.php on line 5
it tell me use mysqli – Akashii Mar 20 '17 at 10:28