PHP手册上的例子:<?php // This could be supplied by a user, for example $firstname = 'fred'; $lastname = 'fox';// Formulate Query // This is the best way to perform an SQL query // For more examples, see mysql_real_escape_string() $query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'", mysql_real_escape_string($firstname), mysql_real_escape_string($lastname));// Perform Query $result = mysql_query($query);// Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); }// Use result // Attempting to print $result won't allow access to information in the resource // One of the mysql result functions must be used // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) { echo $row['firstname']; echo $row['lastname']; echo $row['address']; echo $row['age']; }// Free the resources associated with the result set // This is done automatically at the end of the script mysql_free_result($result); ?>
// This could be supplied by a user, for example
$firstname = 'fred';
$lastname = 'fox';// Formulate Query
// This is the best way to perform an SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
mysql_real_escape_string($firstname),
mysql_real_escape_string($lastname));// Perform Query
$result = mysql_query($query);// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>
mysql_select_db($db_name,$connection)or die("选择数据库失败");
echo "<table border='1'>"; //输出表头
echo "<tr><th>hotel_id</th><th>start_date</th><th>end_date</th><th>content</th><th>source</th><th>s_type</th></tr>";
$result = mysql_query("SELECT * FROM hotel_activities_2");
mysql_close($connection);这是我现在的PHP,你说的DESC没有作用
mysql_select_db($db_name,$connection)or die("选择数据库失败");
echo "<table border='1'>"; //输出表头
echo "<tr><th>hotel_id</th><th>start_date</th><th>end_date</th><th>content</th><th>source</th><th>s_type</th></tr>";
$result = mysql_query("desc hotel_activities_2");
$arr = array();
while($row = mysql_fetch_assoc($result)){
$arr[] = $row;
}
mysql_close($connection);
echo '<pre>';print_r($arr);
比如
id title con type
1 2 3 5
2 2 3 5
显示就好像.NET中的datagrid那样
$connection=mysql_connect($db_host,$db_user,$db_psw)or die("连接服务器失败");
mysql_select_db($db_name,$connection)or die("选择数据库失败");
echo "<table border='1'>"; //输出表头
echo "<tr><th>hotel_id</th><th>start_date</th><th>end_date</th><th>content</th><th>source</th><th>s_type</th></tr>";
$result = mysql_query("SELECT * FROM hotel_activities_2");
$arr = array();
while($row = mysql_fetch_assoc($result)){
$arr[] = $row;
}
mysql_close($connection);
echo '<pre>';print_r($arr);
h_id hotel_id start_date end_date content source s_type 1 22 00:00:00 00:00:00 22 222 2
2 3 00:00:00 00:00:00 3 3 33 而不是这种
Array
(
[0] => Array
(
[h_id] => 1
[hotel_id] => 22
[start_date] => 0000-00-00 00:00:00
[end_date] => 0000-00-00 00:00:00
[content] => 22
[source] => 222
[s_type] => 2
) [1] => Array
(
[h_id] => 2
[hotel_id] => 3
[start_date] => 0000-00-00 00:00:00
[end_date] => 0000-00-00 00:00:00
[content] => 3
[source] => 3
[s_type] => 33
))
$arr[] = $row;
}
改成while($row = mysql_fetch_assoc($result)){
echo '<td>' . $row['h_id'] . '</td>';
echo '<td>' . $row['hotel_id'] . '</td>';
echo '<td>' . $row['start_date'] . '</td>';
echo '<td>' . $row['end_date'] . '</td>';
echo '<td>' . $row['content'] . '</td>';
echo '<td>' . $row['source'] . '</td>';
echo '<td>' . $row['s_type'] . '</td>';
}