我需要为学校制作一个简单的汽车租赁系统。
我相信我有必要的所有部分,但问题是,我不知道如何从MySQL数据库中获取数据。
我做了一些研究,发现下面的代码片段。
澄清一下,我想使用数据库中的具体价格并将其乘以天数
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>.';
}
}
这是我得到的错误:
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
答案 0 :(得分: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>';
}
使用mysqli_query
,而不是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>';
}
答案 1 :(得分:-1)
我完全赞同关于不使用mysql_ *函数的评论,但如果你选择,你首先需要连接到数据库。
这是通过像
这样的代码完成的$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');
mysql_select_db('my_database');