這裏絕對新手請幫忙。這是我第一次嘗試,因此不睡覺6-7個小時,無法弄清楚,所以尋求幫助。使用PHP獲取MySQL結果?
這裏是數據庫:
CREATE TABLE `user_properties`
(`user_id` int(11) NOT NULL,
`username` varchar(10) NOT NULL,
`password` varchar(10) NOT NULL,
`firstname` varchar(20) NOT NULL,
`property_type` varchar(50) NOT NULL,
`property_location` varchar(50),
`no_of_beds` int(11) NOT NULL,
`no_of_baths` int(11) NOT NULL,
`car_spaces` int(11) NOT NULL,
`aditional facilities` varchar(100) NOT NULL,
PRIMARY KEY (`user_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO `user_properties` VALUES (1, 'john', '123456', 'John', 'house', 'strathfield Sydney NSW', '3', '2', '2', 'swimmimg pool sauna gym');
這裏是我正在試圖獲取:
<?php
$host = "localhost"; // Host name
$username = "abc_123"; // Mysql username
$password = "123456"; // Mysql password
$db_name = "abc_test"; // Database name
$tbl_name = "user_properties"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$con=mysqli_connect("$host", "$username", "$password");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$result = mysqli_query($con,"SELECT * FROM user_properties");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['property_type'] . "</td>";
echo "<td>" . $row['property_location'] . "</td>";
echo "<td>" . $row['no_of_beds'] . "</td>";
echo "<td>" . $row['no_of_baths'] . "</td>";
echo "<td>" . $row['car_spaces'] . "</td>";
echo "</tr>";
}
mysqli_close($con);
?>
不要結合mysql或mysqli。只使用mysql – 2014-09-06 05:02:53
我不是一個PHP開發人員,但可能拼寫錯誤'mysqli_connect_error()'或'if'語句後省略了括號,並應在'echo'後關閉它。 – Unihedron 2014-09-06 05:12:09
如果你剛開始,我會學習mysqli over mysql,然後開始學習準備好的語句,這些語句在mysql中不可用以提高安全性。 – 2014-09-06 05:15:48