I am keep getting error of deprcating api in php. Can you please fix my form fro
ID: 3846203 • Letter: I
Question
I am keep getting error of deprcating api in php. Can you please fix my form from mysql to sqli
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:wamp64wwwCoffeeWebsiteModelCoffeeModel.php on line 31
<?php
require ("Entities/CoffeeEntity.php");
//Contains database related code for the Coffee page.
class CoffeeModel {
//Get all coffee types from the database and return them in an array.
function GetCoffeeTypes() {
require ('Credentials.php');
//Open connection and Select database.
mysql_connect($host, $user, $passwd) or die(mysql_error());
mysql_select_db($database);
$result = mysql_query("SELECT DISTINCT type FROM coffee") or die(mysql_error());
$types = array();
//Get data from database.
while ($row = mysql_fetch_array($result)) {
array_push($types, $row[0]);
}
//Close connection and return result.
mysql_close();
return $types;
}
//Get coffeeEntity objects from the database and return them in an array.
function GetCoffeeByType($type) {
require ('Credentials.php');
//Open connection and Select database.
mysql_connect($host, $user, $passwd) or die(mysql_error);
mysql_select_db($database);
$query = "SELECT * FROM coffee WHERE type LIKE '$type'";
$result = mysql_query($query) or die(mysql_error());
$coffeeArray = array();
//Get data from database.
while ($row = mysql_fetch_array($result)) {
$id = $row[0];
$name = $row[1];
$type = $row[2];
$price = $row[3];
$roast = $row[4];
$country = $row[5];
$image = $row[6];
$review = $row[7];
//Create coffee objects and store them in an array.
$coffee = new CoffeeEntity($id, $name, $type, $price, $roast, $country, $image, $review);
array_push($coffeeArray, $coffee);
}
//Close connection and return result
mysql_close();
return $coffeeArray;
}
function GetCoffeeById($id) {
require ('Credentials.php');
//Open connection and Select database.
//$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);
mysql_connect($host, $user, $passwd) or die(mysql_error);
mysql_select_db($database);
$query = "SELECT * FROM coffee WHERE id = $id";
$result = mysql_query($query) or die(mysql_error());
//Get data from database.
while ($row = mysql_fetch_array($result)) {
$name = $row[1];
$type = $row[2];
$price = $row[3];
$roast = $row[4];
$country = $row[5];
$image = $row[6];
$review = $row[7];
//Create coffee
$coffee = new CoffeeEntity($id, $name, $type, $price, $roast, $country, $image, $review);
}
//Close connection and return result
mysql_close();
return $coffee;
}
function InsertCoffee(CoffeeEntity $coffee) {
$query = sprintf("INSERT INTO coffee
(name, type, price,roast,country,image,review)
VALUES
('%s','%s','%s','%s','%s','%s','%s')",
mysql_real_escape_string($coffee->name),
mysql_real_escape_string($coffee->type),
mysql_real_escape_string($coffee->price),
mysql_real_escape_string($coffee->roast),
mysql_real_escape_string($coffee->country),
mysql_real_escape_string("Images/Coffee/" . $coffee->image),
mysql_real_escape_string($coffee->review));
$this->PerformQuery($query);
}
function UpdateCoffee($id, CoffeeEntity $coffee) {
$query = sprintf("UPDATE coffee
SET name = '%s', type = '%s', price = '%s', roast = '%s',
country = '%s', image = '%s', review = '%s'
WHERE id = $id",
mysql_real_escape_string($coffee->name),
mysql_real_escape_string($coffee->type),
mysql_real_escape_string($coffee->price),
mysql_real_escape_string($coffee->roast),
mysql_real_escape_string($coffee->country),
mysql_real_escape_string("Images/Coffee/" . $coffee->image),
mysql_real_escape_string($coffee->review));
$this->PerformQuery($query);
}
function DeleteCoffee($id) {
$query = "DELETE FROM coffee WHERE id = $id";
$this->PerformQuery($query);
}
function PerformQuery($query) {
require ('Credentials.php');
mysql_connect($host, $user, $passwd) or die(mysql_error());
mysql_select_db($database);
//Execute query and close connection
mysql_query($query) or die(mysql_error());
mysql_close();
}
}
?>
Explanation / Answer
1. Error is thrown by mysql_fetch_array
2. Generally that means something is wrong with your SQL. Try echoing out the sql, you will see that $type is not getting passed properly.
3. Issue is with way you are passing the variable in query: $query = "SELECT * FROM coffee WHERE type LIKE '$type'";
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.