I am doing one project using php. I want retrieve data from mysql table order by payment plan1,plan2,plan3,Free using php. How?
Asked
Active
Viewed 1.6k times
-2
-
Try this (and better wording) http://w3schools.com/php/php_mysql_intro.asp – cbronson Jan 08 '13 at 04:50
-
what are the rows in yours db table – Daya Jan 08 '13 at 04:50
-
Just saying but you pretty much gave us nothing to work with. If you want a specific answer please give us a specific question. http://w3schools.com/php/php_mysql_intro.asp Here is the basics; because I am not sure if you have even connected to a db yet. – Devon Bernard Jan 08 '13 at 04:55
-
no tutorials avail here .... – Arun Killu Jan 08 '13 at 04:59
4 Answers
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:
- Make a mysql connection to the database.
- 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:
- 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

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
-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
-
Please don't teach the mysql_* functions to newbie, try to use mysqli or PDO – iLaYa ツ Jan 08 '13 at 05:03
-
-
-
But they are deprecated. Look at the big red box here: http://php.net/manual/en/function.mysql-connect.php – Yam Mesicka Jan 08 '13 at 05:06