我想從W3schools學習php,其中包括一個mysql部分。到目前爲止,我已經完成了關於w3school的教程的每一部分,除了打印來自數據庫表。對於一些非常奇怪的原因,當我運行我的代碼時,沒有任何顯示。請我怎麼才能得到這個工作,並且我的問題可能來自我使用MariaDB與Xampp而不是Mysql的事實,儘管他們說它實際上是相同的語法。 下面是代碼我怎樣才能讓mysql從數據庫表中打印行
<?php
$servername = "localhost";
$username = "uhexos";
$password = "strongpassword";
$database = "fruitdb";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE fruitDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
// Create connection
$conn = mysqli_connect($servername, $username, $password,$database);
// sql to create table
$complexquery = "CREATE TABLE MyFruits (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
FruitType VARCHAR(30) NOT NULL,
FruitTaste VARCHAR(30) NOT NULL,
FruitQuantity INT NOT NULL,
DatePurchased TIMESTAMP
)";
if ($conn->query($complexquery) === TRUE) {
echo "Table Fruits created successfully<br> ";
} else {
echo "Error creating table: " . $conn->error;
}
$entry = "INSERT INTO myfruits (fruittype,fruittaste,fruitquantity) VALUES ('orange','sweet','50'),('lemon','sour','10'),('banana','sweet','15')";
if ($conn->query($entry) === TRUE) {
echo "New records created successfully";
} else {
echo "Error: " . $conn->error;
}
$sql = 'SELECT id, fruitname, fruittaste FROM myfruits';
$retval = mysql_query($sql, $conn);
if(! $retval) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "EMP ID :{$row['id']} <br> ".
"EMP NAME : {$row['fruitname']} <br> ".
"EMP SALARY : {$row['fruittaste']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
這是輸出從我的所有回波得到。
Error creating database: Can't create database 'fruitdb'; database existsError creating table: Table 'myfruits' already existsNew records created successfully
或
Database created successfullyTable Fruits created successfully
New records created successfully
該死的,擊敗了我,我正要發佈這個相同的答案 –
,沒有幫助我得到存儲在表中的項目。儘管存在這些醜陋的錯誤,記錄實際上也會被添加@manassehkatz – uhexos
@uhexos - 更正記錄無論如何都存儲 - 這是預期的。你是說在運行結束時你沒有得到'無法獲取數據:'或'成功提取數據'嗎? – manassehkatz