2013-01-08 127 views
-2

我正在做一個項目使用PHP。我想通過付款plan1,plan2,plan3從mysql表格順序檢索數據,使用php免費。怎麼樣?使用php檢索數據從mysql

+0

試試這個(和更好的措辭)http://w3schools.com/php/php_mysql_intro.asp – cbronson

+0

是什麼在你的數據庫表中的行 – Daya

+0

只是說,但你幾乎沒有給我們任何工作。如果你想要一個特定的答案,請給我們一個具體的問題。 http://w3schools.com/php/php_mysql_intro.asp這裏是基礎;因爲我不確定你是否還連接到數據庫。 –

回答

0
select * from table 
order by plan1, plan2 desc, plan3 
2
// 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(); 
} 
+0

爲PDO(PHP數據對象) –

-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); 
} 
?> 
+0

請不要教mysql_ *函數給新手,嘗試使用mysqli或PDO –

+0

請不要使用mysql_ *棄用函數。 – Infinity

+0

我覺得對於初學者來說,mysql_ *很容易使用。 – neeraj

0

從數據庫中檢索數據的步驟是:

  1. 使一個MySQL連接到數據庫。
  2. 寫下您的查詢。要檢索數據,必須使用 mysql_query(「select * from your_table where id = $ id」)。 (但這是 已在PHP 5.5中棄用)。看看這個鏈接來進一步解釋 你: http://www.w3schools.com/php/func_mysql_query.asp:現在http://php.net/manual/en/function.mysql-query.php

,一個完整的例子從連接到數據庫中選擇數據在這裏說明。見例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

+0

我認爲我們不應該給出包含mysql_ *函數的答案,因爲它們已被棄用。 – Infinity

+0

我剛發佈,所以他們會明白它是如何工作的。我認爲它對新手來說是一個很好的開始學習的幫助。我也注意到mysql_query在PHP 5.5.0中已被棄用。 – user1149244