0

I need to make a simple car rental system for school.

I believe I have all the parts necessary, but the problem is, I don't know how to fetch data from the MySQL database.

I have done some research and found the snippet below .

to clarify , i want to use the specific price from the database and multiply it with the number of day

if ($modelname && $numday) {
    if ($modelname = 'Jetta') {
        $harga = mysql_query("SELECT price FROM carlist WHERE price = '13300'");
        $tambah = mysql_fetch_row($harga);
        $totalAll = $numday * $tambah;
        $totalAll = number_format($totalAll, 2);
        echo 'Total Price: RM<b>' . $totalAll . '</b>.';
    }
}

Here is the error that I get:

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\assignment2\orderform.php on line 109
Total Price: RM0.00.
Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\assignment2\orderform.php on line 231
Hensemnm
  • 1
  • 3
  • 1
    First of all: Don 't use the mysql_* functions anymore. More informations about why you shouldn 't use mysql_* functions anymore: https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Marcel Jun 08 '17 at 16:24
  • Please, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://news.php.net/php.internals/53799), and `mysql_*` functions have been officially removed in PHP 7. Instead you should learn about [prepared statements](https://en.wikipedia.org/wiki/Prepared_statement) and use either `PDO` or `mysqli_*`. If you can't decide, [this article will help to choose your best option](http://php.net/manual/en/mysqlinfo.api.choosing.php). – GrumpyCrouton Jun 08 '17 at 16:26
  • Also, your errors mention lines 109 and 231, it is impossible for us to tell which lines these are/if they are even present. Please provide more code/more information. – GrumpyCrouton Jun 08 '17 at 16:31

2 Answers2

0
$harga = mysql_query("SELECT * FROM carlist WHERE price = '13300'");

while ($rows = mysql_fetch_array($harga )){
  $price = $rows['price'];
  echo 'Total Price: RM<b>'.$price. '</b></br>';
}

Use mysqli_query, not mysql_query.

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root');
define('DB_DATABASE', 'mydatabase');

$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if ($conn->connect_error) {
   die("Error: " . $conn->connect_error);

}
mysqli_query($conn, "SET NAMES utf8");
mysqli_query($conn, "SET CHARACTER SET utf8");
mysqli_query($conn, "SET COLLATION_CONNECTION='utf8_general_ci'");

$harga = "SELECT * FROM carlist WHERE price = '13300'";
$getquery = mysqli_query($conn, $harga );

while ($rows = mysqli_fetch_array($getquery)){
    $price = $rows['price'];
    echo 'Total Price: RM<b>'.$price. '</b></br>';
}   
gkubed
  • 1,767
  • 2
  • 31
  • 42
Predrag
  • 1
  • 1
  • 3
-2

I totally agree with the comment about not using mysql_* functions, but if you choose to, you first need to connect to a database.

This is done with code like

$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');
mysql_select_db('my_database');
Alexios Tsiaparas
  • 804
  • 10
  • 17
  • Don 't use mysql_* functions anymore: https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Marcel Jun 08 '17 at 16:25
  • @Marcel, I agree with you, but I think that if this is homework, he/she probably does not have much say into that – Alexios Tsiaparas Jun 08 '17 at 16:27
  • Partly agree with you. If school teaches it false or on deprecated techniques it is still false. Over here in germany we have the same problems in schools. Teachers teach old stuff that is not right anymore. – Marcel Jun 08 '17 at 16:40