使用PHP获取MySQL结果?

时间:2014-09-06 04:58:30

标签: php html mysql

绝对新手在这里请帮忙。这是我的第一次尝试,因此没有睡6-7个小时,无法弄清楚,所以寻求帮助。

这是数据库:

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);
?>

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:-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);

?>

试试这个