Jump to content

[SOLVED] sql statement to extract a string problem


geroido

Recommended Posts

Hi

I'm looking for a way to solve this problem. I have database entries in the form:

 

geroido20080825122526

 

These are unique order numbers, the first part (geroido) being the users username. So when a user logs in, I have their username say 'geroido' for example. Firstly, I get the username string length as follows:

 

$len = strlen($_SESSION['username']);

 

In the case of 'geroido' this will give me the length of 7.

 

 

What I now want to do is form an sql statement that can trawl through my table, extract the first 7 ($len) characters of the ordernum or however long the particular username is  and if they match the username display them else you have no orders with us.

 

So I was thinking something like:

$query = "Select DISTINCT(Ordernum),  MenuItemID, orddate, ordtime FROM orderdetails  where

substr(Ordernum, 0, $len) = '".$_SESSION['username']);."' group by Ordernum ";

Is this possible. Sorry if this is confusing

 

 

Link to comment
Share on other sites

Hi

This is further to an earlier post. I'm getting the length of a any particular username. Each ordernum in the table has the username concatenated on the to beginning of it as in:

 

geroido20080825122526

'geroido' being the username

 

I go through each record extracting the username from each ordernum so that I can display this users orders. How can I add a where clause where the substring extracted is equal to the username ($_SESSION['username']) so that in this example geroido = geroido. I store the result in 'string'. Can I add something like WHERE string = '".$_SESSION['username']."'

$len = strlen($_SESSION['username']);
echo $len;
$query = "Select SUBSTRING(Ordernum,1, $len) as string,  Ordernum, MenuItemID, orddate, ordtime FROM orderdetails group by Ordernum "; 

Link to comment
Share on other sites

I have found the solution to my problem with the following sql statement:

 

$query = "Select SUBSTRING(Ordernum,1, $len) as string,  Ordernum, MenuItemID, orddate, ordtime FROM orderdetails WHERE SUBSTRING(Ordernum,1, $len) = '".$_SESSION['username']."'  group by Ordernum order by orddate asc ";

 

 

Thanks

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.