我有一个表格按ASC顺序对名称进行排序,但是当我点击按钮时它不起作用。 我尝试用2个按钮做同样的事情并检查了一些可用的代码,但它根本不起作用。有什么帮助吗?
PHP代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myfeeds";
$conn = mysql_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed");
}
$db = mysql_select_db("myfeeds", $conn);
if (!$db) {
die("Can't select database");
}
if (isset($_POST['asc'])) {
$result = mysql_query("SELECT * FROM websites ORDER BY name ASC");
} else {
$result = mysql_query("SELECT * FROM websites ORDER BY name DESC");
}
if (!$result) {
die("Failed to show queries from table");
}
$num = mysql_numrows($result);
mysql_close();
?>
这里是按钮:
SORT BY:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<button type="submit" id="asc" name="asc">ASC</button>
</form>
表:
<table cellpadding="0">
<tr>
<th>ID</th>
<th>Name</th>
<th>URL</th>
<th>Description</th>
<th>Logo</th>
</tr>
<?php
$i = 0;
while ($i < $num) {
$f5 = mysql_result($result, $i, "id");
$f1 = mysql_result($result, $i, "name");
$f2 = mysql_result($result, $i, "url");
$f3 = mysql_result($result, $i, "description");
$f4 = mysql_result($result, $i, "image");
?>
<tr>
<td><?php echo $f5; ?></td>
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
<td><?php echo "<img src='$f4'>"; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
答案 0 :(得分:2)
根据manual,mysql_connect
的第四个参数应该是新的连接链接,而不是数据库名称。
<强> new_link 强>
如果使用相同的参数对
mysql_connect()
进行第二次调用,则不会建立新的链接,而是返回已打开的链接的链接标识符。 &gt; new_link 参数会修改此行为,并使mysql_connect()
始终打开新链接,即使之前使用相同参数调用mysql_connect()
也是如此。在SQL安全模式下,将忽略此参数。
我建议改用mysqli_*
,因为不推荐使用mysql。
当然,不要忘记在查询后获取行。
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myfeeds";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$order = isset($_POST['asc']) ? 'ASC' : 'DESC';
$sql = "SELECT * FROM websites ORDER BY name $order";
$query = mysqli_query($conn, $sql);
$num = $query->num_rows;
if($num > 0) {
while($row = mysqli_fetch_assoc($query)) {
echo $row['name'] . '<br/>';
}
}
答案 1 :(得分:1)
试试这个......工作100%=)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myfeeds";
$conn = mysql_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed");
}
$db = mysql_select_db("myfeeds", $conn);
if (!$db) {
die("Can't select database");
}
if (isset($_GET['asc']))
$result = mysql_query("SELECT * FROM websites ORDER BY name ASC");
else
$result = mysql_query("SELECT * FROM websites ORDER BY name DESC");
if (!$result)
die("Failed to show queries from table");
if (mysql_num_rows($result) > 0) {
// output data of each row
while($row = mysql_fetch_assoc($result)) {
echo "name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
$num = mysql_numrows($result);
mysql_close();
?>
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<button type="submit" id="asc" name="asc" value="asc">ASC</button>
<button type="submit" id="asc" name="desc" value="desc">DESC</button>
</form>
最好使用PDO这是怎么回事..
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myfeeds";
try
{
$conn = new PDO("mysql:host=".$servername.";dbname=".$dbname, $username, $password);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
die($e->getMessage());
}
try
{
if (isset($_GET['asc']))
$result = $conn->prepare("SELECT * FROM websites ORDER BY name ASC");
else
$result = $conn->prepare("SELECT * FROM websites ORDER BY name DESC");
$result->execute();
if($result->rowCount())
{
while($r = $result->fetch(PDO::FETCH_OBJ))
{
echo 'Name:' . $r->name . '<br/>';
}
}
else echo 'no record found!';
}
catch (PDOException $e)
{
die($e->getMessage());
}
?>
使用mysql从数据库中查看数据
<table cellpadding="0">
<tr>
<th>ID</th>
<th>Name</th>
<th>URL</th>
<th>Description</th>
<th>Logo</th>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["url"]; ?></td>
<td><?php echo $row["description"]; ?></td>
<td><?php echo "<img src='".$row["image"]."'>"; ?></td>
</tr>
<?php
}
?>
</table>