-2

I am doing one project using php. I want retrieve data from mysql table order by payment plan1,plan2,plan3,Free using php. How?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1956949
  • 1
  • 1
  • 1
  • 1

4 Answers4

4
// 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();
}
Yam Mesicka
  • 6,243
  • 7
  • 45
  • 64
1

The steps of retrieving data from the database are:

  1. Make a mysql connection to the database.
  2. Write your query. For retrieving data, you have to use mysql_query("select * from your_table where id=$id"). (But this is already deprecated in PHP 5.5). Take a look at this link to explain you further: http://php.net/manual/en/function.mysql-query.php

Now, a complete example stated here from connecting to the database to selecting data: http://www.w3schools.com/php/func_mysql_query.asp. See example 1.

or you can check this example code I did for your reference:

<?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>
    
    <?php
    //this is to display your data
    while($row=mysql_fetch_array($query))
    {
    ?>
    <tr>
         <td><?php echo $row['id']?></td>
        <td><?php echo $row['full_name']?></td>
        <td><?php echo $row['email_address']?></td>
    </tr>
    
     <?php
    }//end while
    ?>
    </table>
<?php
mysql_close($con);
?>

The other way to retrieve data is to used PDO. This is the best and safest way. Read the following links to explain you further:

But, the concept is still the same. Connect to the database before executing your query.

And also you may read this thread: display table columns with for loop based on user input

KUMAR
  • 1,993
  • 2
  • 9
  • 26
user1149244
  • 711
  • 4
  • 10
  • 27
  • I think we shouldn't give answers containing the mysql_* functions, as they are deprecated. – Yam Mesicka Jan 08 '13 at 08:07
  • I just posted that so they would understand how it works. I think its helpful for newbie for a good start to learn. I have it in my note too that mysql_query is deprecated in PHP 5.5.0. – user1149244 Jan 08 '13 at 08:11
0
select * from table
order by plan1, plan2 desc, plan3
Hikari
  • 3,797
  • 12
  • 47
  • 77
-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);
}
?>
Neeraj
  • 8,625
  • 18
  • 60
  • 89