Jump to content

[SOLVED] SELECT *


Maverickb7

Recommended Posts

I was wondering if someone could help me. What would I have to put within the $sort variable to have that request display all rows starting with a number? right now I have to put 1,2,3,4,5,6,7,8,9 seperate. Anyway I can include it all in one variable that would work below?

 

$q = 'SELECT * '
        . ' FROM `company_details` '
        . ' WHERE `cname` LIKE  \'' . $sort . '%\''
        . ' ORDER BY `cname` ASC'; 
$r = mysql_query($q);

Link to comment
Share on other sites

look at string formatting

 

% means any char of any length.  look at other wildcards that do stuf with only 1 character, so that you can loop through the string......... its all in string formatting and conditions

 

 

good luck

Link to comment
Share on other sites

so I would use the following:

 

if ($sort = #) {
$q = 'SELECT * '
        . ' FROM `company_details` '
        . ' WHERE SUBSTR(cname, 1, 1) IN (0,1,2,3,4,5,6,7,8,9)'
        . ' ORDER BY `cname` ASC'
}
else {
$q = 'SELECT * '
        . ' FROM `company_details` '
        . ' WHERE `cname` LIKE  \'' . $sort . '%\''
        . ' ORDER BY `cname` ASC'; 
}

 

Basically what I'm doing is receiving information from the url. detail.php?sort=a <-- like that. With that it will list all results starting with A. I'm trying to have it display all numeric results if # is inputed where A was. Would this code do that for me.. if not how would I change it to work?

Link to comment
Share on other sites

Well, did you at least test if the SQL statement I posted gave you the results you wanted? Because determining wether the input is a number or not is a different question. So, which one are you asking now?

 

Assuming the query I gave you works, here is how you would determine if the input was a number:

 

<?php
if (is_numeric($sort)) {
   $q = 'SELECT * '
        . ' FROM `company_details` '
        . ' WHERE SUBSTR(cname, 1, 1) IN (0,1,2,3,4,5,6,7,8,9)'
        . ' ORDER BY `cname` ASC';
}
else {
   $q = 'SELECT * '
        . ' FROM `company_details` '
        . ' WHERE `cname` LIKE  \'' . $sort . '%\''
        . ' ORDER BY `cname` ASC'; 
}
?>

Link to comment
Share on other sites

OK, the numbers just need to be enclosed in quotes. I have tested this and it works:

 

Made some minor changes to simplify as well

<?php

if (is_numeric($sort)) {
  $WHERE = "WHERE SUBSTR(cname, 1, 1) IN ('0','1','2','3','4','5','6','7','8','9')";
} else {
  $WHERE = "WHERE cname LIKE '$sort%'";
}

$q = "SELECT *
      FROM `company_details`
      $WHERE
      ORDER BY `cname` ASC";

?>

Link to comment
Share on other sites

Works beautiful mjdamato, thanks so much!

 

Just curious... it's not importent but how come this doesn't work?

if ($sort == "#") {
  $WHERE = "WHERE SUBSTR(cname, 1, 1) IN ('0','1','2','3','4','5','6','7','8','9')";
} else {
  $WHERE = "WHERE cname LIKE '$sort%'";
}

$q = "SELECT *
      FROM `company_details`
      $WHERE
      ORDER BY `cname` ASC";

 

I'm trying to get it to allow me to use detail.php?sort=# and have it display all the numeric results but that doesn't seems to work. Any ideas?

Link to comment
Share on other sites

Some characters cannot be sent via the query string in an unencoded format. You have probably seen plenty of web addresses that have %20 in them. That is the URL encoding for a space. Similarly the pound character needs to be encoded if passed on the query string.

 

So you would need to use detail.php?sort=%23 and then your PHP code would recognize it as the pound symbol - #

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.