-1
我有一些php代码,在那里我做了一些MySQL数据库的一些信息。当我运行我的代码时,我得到这个答案:“连接到MySQL”,但该页面不返回我的ID,名称和年份。任何人都可以看到这是为什么?回声没有返回结果
问候 的Mads
<?php
//echo phpinfo();
$username = "root";
$password = "root";
$hostname = "127.0.0.1:8889";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("examples",$dbhandle)
or die("Could not select examples");
//execute the SQL query and return records
$result = mysql_query("SELECT id, model, year FROM cars");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ".$row{'year'}."<br>"; //display the results
}
//close the connection
mysql_close($dbhandle);
?>
CREATE DATABASE `examples`;
USE `examples`;
CREATE TABLE `cars` (
`id` int UNIQUE NOT NULL,
`name` varchar(40),
`year` varchar(50),
PRIMARY KEY(id)
);
INSERT INTO cars VALUES(1,'Mercedes','2000');
INSERT INTO cars VALUES(2,'BMW','2004');
INSERT INTO cars VALUES(3,'Audi','2001');
INSERT INTO cars VALUES(4, 'Ferrari', '2013');
INSERT INTO cars VALUES(5, 'Lamborghini', '2012');
INSERT INTO cars VALUES(6, 'Mazda', '2010');