2014-09-06 64 views
0

這裏絕對新手請幫忙。這是我第一次嘗試,因此不睡覺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); 
?> 
+0

不要結合mysql或mysqli。只使用mysql – 2014-09-06 05:02:53

+0

我不是一個PHP開發人員,但可能拼寫錯誤'mysqli_connect_error()'或'if'語句後省略了括號,並應在'echo'後關閉它。 – Unihedron 2014-09-06 05:12:09

+0

如果你剛開始,我會學習mysqli over mysql,然後開始學習準備好的語句,這些語句在mysql中不可用以提高安全性。 – 2014-09-06 05:15:48

回答

-1
<?php 
$link = mysql_connect('localhost', 'root', ''); 
if (!$link) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 
echo 'Connected successfully'; 


$db_selected = mysql_select_db('abc_test', $link); 
if (!$db_selected) 
{ 
    die ('Can\'t use database name : ' . mysql_error()); 
} 

$result = mysql_query("SELECT * FROM user_properties"); 


while($row = mysql_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>"; 
} 


mysql_close($link); 

?> 

試試這個

+0

請檢查user_name兩次。因爲默認的用戶名是根 – 2014-09-06 05:06:44

+0

嗨帕雷什它的工作正常。謝謝,但它顯示在一行。連接工程 – rosi 2014-09-06 05:28:27

+0

它是正確的方式來顯示在單行,因爲如果我們有多個記錄它的好用的表如果我我幫忙請upvote。 – 2014-09-06 05:48:25

0

試試這個

//you have to change database name, password and other database info 

<?php 

$host = "localhost"; // Host name 
$username = "root"; // Mysql username 
$password = ""; // Mysql password 
$db_name = "nes"; // Database name 
$tbl_name = "user_properties"; // Table name 

// Connect to server and select databse. 
$con= mysqli_connect($host, $username, $password, $db_name); 
// Check connection 
if (mysqli_connect_errno()) { 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
$result = mysqli_query($con,"SELECT * FROM user_properties"); 
echo '<table border=1px solid black>'; 
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>"; 
} 
echo '</table>'; 
mysqli_close($con); 
+0

如何顯示提取行 – rosi 2014-09-06 05:53:53

+0

物業類型:房屋 物業地點:美國 – rosi 2014-09-06 05:55:00

+0

一切都完成了,它的顯示結果,但我想顯示像屬性位置;位置然後另一row.i我現在嘗試使用表格,但不成功 – rosi 2014-09-06 06:08:35