我正在使用php做一个项目。我希望通过支付plan1,plan2,plan3,免费使用php从mysql表顺序检索数据。怎么样?
答案 0 :(得分:3)
// Initializing connection data.
$host_db = 'localhost';
$name_db = 'article_db';
$user_db = 'username';
$pass_db = 'password';
try {
// Connecting using the PDO object.
$connection = new PDO("mysql:host=$host_db; dbname=$name_db", $user_db, $pass_db);
// Setting the query and runnin it...
$sql = "SELECT * FROM `article` WHERE `category` = 5 ORDER BY 3";
$result = $connection->query($sql);
// Iterating over the data and printing it.
foreach($result as $row) {
echo $row['id']. ' - '. $row['name']. ' - '. $row['category']. ' - '. $row['editor']. '<br />';
}
// Closing the connection.
$connection = null;
}
// Catching it if something went wrong.
catch(PDOException $e) {
echo $e->getMessage();
}
答案 1 :(得分:0)
select * from table
order by plan1, plan2 desc, plan3
答案 2 :(得分:0)
从数据库中检索数据的步骤如下:
现在,这里说明了从连接到数据库到选择数据的完整示例: http://www.w3schools.com/php/func_mysql_query.asp。见例1。
或者您可以查看我为此参考的示例代码:
<?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM your_table";
$query= mysql_query($sql);
?>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
<tr>
<?php
//this is to display your data
while($row=mysql_fetch_array($query))
{
?>
<td><?php echo $row['id']?></td>
<td><?php echo $row['full_name']?></td>
<td><?php echo $row['email_address']?></td>
<?php
}//end while
?>
</tr>
</table>
<?php
mysql_close($con);
?>
检索数据的另一种方法是使用PDO。这是最好,最安全的方式。请阅读以下链接,进一步向您解释:
但是,这个概念仍然是一样的。在执行查询之前连接到数据库。
您也可以阅读此主题:display table columns with for loop based on user input
答案 3 :(得分:-2)
<?php
$con = mysql_connect("localhost","root","") or die("Could not connect");
mysql_selectdb("test", $con);
$query = 'SELECT * FROM payment ORDER BY plan1,plan2,plan3';
$res = mysql_query($query, $con) or die(mysql_error());
while($row = mysql_fetch_array($res)){
print_r($row);
}
?>