Retrieve data from mysql using php

好久不见. 提交于 2019-12-13 09:08:03

问题


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


回答1:


// 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();
}



回答2:


select * from table
order by plan1, plan2 desc, plan3



回答3:


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>

    <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);
?>

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:

  • http://php.net/manual/en/book.pdo.php
  • http://php.net/manual/en/pdo.query.php
  • How can I properly use a PDO object for a parameterized SELECT query

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




回答4:


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


来源:https://stackoverflow.com/questions/14208369/retrieve-data-from-mysql-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!