When I'm doing a request on a database with PHP I print the result in an array. I would like to know if it's possible to print the reslut (names) in an alphabetical order????
alphabetical order
Moderator: General Moderators
alphabetical order
Hi guys
When I'm doing a request on a database with PHP I print the result in an array. I would like to know if it's possible to print the reslut (names) in an alphabetical order????

When I'm doing a request on a database with PHP I print the result in an array. I would like to know if it's possible to print the reslut (names) in an alphabetical order????
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Try:
Mac
Code: Select all
$sql = "SELECT * FROM table ORDER BY name DESC";Sorry but it doesn't work. This is the syntax I use:
$requete="SELECT * FROM beneficiaire WHERE (login='$lg' AND pays='Belgique') ORDER BY nombenef DESC";
May be the problem come from the fact that after I use:
if ($resultat = mysql_db_query("maredsous", $requete, $connexion)) {
while($acces = mysql_fetch_array($resultat)) {
What about using something like sort or natcasesort ???
$requete="SELECT * FROM beneficiaire WHERE (login='$lg' AND pays='Belgique') ORDER BY nombenef DESC";
May be the problem come from the fact that after I use:
if ($resultat = mysql_db_query("maredsous", $requete, $connexion)) {
while($acces = mysql_fetch_array($resultat)) {
What about using something like sort or natcasesort ???
- daven
- Forum Contributor
- Posts: 332
- Joined: Tue Dec 17, 2002 1:29 pm
- Location: Gaithersburg, MD
- Contact:
Does the query work without the ORDER BY clause?
If I am thinking correctly, putting a PHP variable in single quotes causes the variable to not be evaluated. So:
"SELECT * FROM beneficiaire WHERE (login='$lg' AND pays='Belgique') ORDER BY nombenef DESC"
will be read as (notice the login= statement):
"SELECT * FROM beneficiaire WHERE (login='' AND pays='Belgique') ORDER BY nombenef DESC".
Try concatenating the strings instead:
"SELECT * FROM beneficiaire WHERE (login='".$lg."' AND pays='Belgique') ORDER BY nombenef DESC";
Warning: I have been doing a lot of PERL and C coding recently, so it is entirely possible that my PHP syntax/theory is off a bit.
If I am thinking correctly, putting a PHP variable in single quotes causes the variable to not be evaluated. So:
"SELECT * FROM beneficiaire WHERE (login='$lg' AND pays='Belgique') ORDER BY nombenef DESC"
will be read as (notice the login= statement):
"SELECT * FROM beneficiaire WHERE (login='' AND pays='Belgique') ORDER BY nombenef DESC".
Try concatenating the strings instead:
"SELECT * FROM beneficiaire WHERE (login='".$lg."' AND pays='Belgique') ORDER BY nombenef DESC";
Warning: I have been doing a lot of PERL and C coding recently, so it is entirely possible that my PHP syntax/theory is off a bit.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
In this case it is - the string is actually double quoted so the variables will be just fine. It would be a different case, however, if the code looked like this:daven wrote:Warning: I have been doing a lot of PERL and C coding recently, so it is entirely possible that my PHP syntax/theory is off a bit.
Code: Select all
$sql = 'SELECT * FROM beneficiaire WHERE (login=''$lg'' AND pays='Belgique') ORDER BY nombenef DESC';Code: Select all
SELECT * FROM beneficiaire WHERE (login='$lg' AND pays='Belgique') ORDER BY nombenef DESC- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
I'm going to give you my full syntax:
Whith this syntax everuthing is ok but if I ad ORDER BY nombenef DESC and that I execute the request nothing appear on the screen.
Help help help help
Code: Select all
<?php
$requete="SELECT * FROM beneficiaire WHERE (login='$lg' AND pays='Belgique') ";
if ($resultat = mysql_db_query("maredsous", $requete, $connexion)) {
while($acces = mysql_fetch_array($resultat)) {
$nump=$acces["numbenef"];
$nombenef=$acces["nombenef"];
$rue=$acces["ruebenef"];
$bal=$acces["balbenef"];
$cp=$acces["cpost"];
$local=$acces["localbenef"];
$pays=$acces["pays"];
$cs1=$acces["cs1"];
$cs2=$acces["cs2"];
$cs3=$acces["cs3"];?>
<table width="50%" border="1" cellspacing="2">
<tr>
<td width="30%"><?php echo $nombenef; ?></td>
<td><form action="maredsous2.php" method="post"> <input type="submit" name="parti" value="Choisir">
<input type="hidden" name="lg" value="<?php echo $lg; ?>">
<input type="hidden" name="nombenef" value="<?php echo $nombenef; ?>">
<input type="hidden" name="cs1" value="<?php echo $cs1; ?>">
<input type="hidden" name="cs2" value="<?php echo $cs2; ?>">
<input type="hidden" name="cs3" value="<?php echo $cs3; ?>">
<input type="hidden" name="rue" value="<?php echo $rue; ?>">
<input type="hidden" name="bal" value="<?php echo $bal; ?>">
<input type="hidden" name="cp" value="<?php echo $cp; ?>">
<input type="hidden" name="local" value="<?php echo $local; ?>">
<input type="hidden" name="pays" value="<?php echo $pays; ?>">
</form>
</td>
<td>
<form action="modif.php" method="post">
<input type="submit" name="change" value="modifier">
<input type="hidden" name="nump" value="<?php echo $nump; ?>">
<input type="hidden" name="cs1" value="<?php echo $cs1; ?>">
<input type="hidden" name="cs2" value="<?php echo $cs2; ?>">
<input type="hidden" name="cs3" value="<?php echo $cs3; ?>">
<input type="hidden" name="lg" value="<?php echo $lg; ?>">
<input type="hidden" name="nombenef" value="<?php echo $nombenef; ?>">
<input type="hidden" name="rue" value="<?php echo $rue; ?>">
<input type="hidden" name="bal" value="<?php echo $bal; ?>">
<input type="hidden" name="cp" value="<?php echo $cp; ?>">
<input type="hidden" name="local" value="<?php echo $local; ?>"></td>
</tr>
</table>
</form>
<p>
<p>
<p>
<?phpHelp help help help
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
For one thing you shouldn't be using mysql_db_query():
For more info:
http://www.php.net/manual/en/function.m ... ect-db.php
http://www.php.net/manual/en/function.mysql-query.php
http://www.php.net/manual/en/function.mysql-error.php
http://www.php.net/manual/en/function.m ... m-rows.php
http://www.php.net/manual/en/function.die.php
Mac
Try:PHP Manual - mysql_db_query() wrote:Note: This function has been deprecated since PHP 4.0.6. Do not use this function. Use mysql_select_db() and mysql_query() instead.
Code: Select all
@mysql_select_db('maredsous') or die(mysql_error());
$requete = "SELECT numbenef, nombenef, ruebenef, balbenef, cpost, localbenef, pays, cs1, cs2, cs3 FROM beneficiaire WHERE login='$lg' AND pays='Belgique' ORDER BY nombenef DESC";
@$resultat = mysql_query($requete) or die(mysql_error().'<p>'.$requete.'</p>');
if (mysql_num_rows($resultat) > 0) {
while ($acces = mysql_fetch_assoc($resultat)) {
$nump = $acces['numbenef'];
// the rest of the code
}
} else {
echo 'The query returned no results.';
}http://www.php.net/manual/en/function.m ... ect-db.php
http://www.php.net/manual/en/function.mysql-query.php
http://www.php.net/manual/en/function.mysql-error.php
http://www.php.net/manual/en/function.m ... m-rows.php
http://www.php.net/manual/en/function.die.php
Mac
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK