Solved

issue with mysql query

Posted on 2012-12-22
6
266 Views
Last Modified: 2012-12-24
Im working on a login script Im having alittle issue with the query when it comes to validate the status of the account & the user that is logging in that is associated with the account. (accounts can have many users)

I created a table for systemStatus since there will be many different statuses. Im joining the tables with a LEFT JOIN 2x as I need to make sure the account is active and the user is active. my query below for some reason is returning 2 results. Im pretty sure because systemStatus has 2 rows. view attached image... the second result is all Im expecting to get

query result
DB Structure
-- phpMyAdmin SQL Dump
-- version 3.3.3
-- http://www.phpmyadmin.net
--
-- Host: localhost:3306
-- Generation Time: Dec 22, 2012 at 09:43 PM
-- Server version: 5.0.95
-- PHP Version: 5.2.6

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `admin_iPrint`
--

-- --------------------------------------------------------

--
-- Table structure for table `accounts`
--

CREATE TABLE IF NOT EXISTS `accounts` (
  `accountId` int(9) unsigned NOT NULL auto_increment,
  `accountOpenDate` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `accountStatusId` int(3) NOT NULL,
  `companyName` blob NOT NULL,
  `referredBy` blob NOT NULL,
  PRIMARY KEY  (`accountId`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

-- --------------------------------------------------------

--
-- Table structure for table `systemStatus`
--

CREATE TABLE IF NOT EXISTS `systemStatus` (
  `statusId` int(3) unsigned NOT NULL auto_increment,
  `statusDescription` varchar(255) NOT NULL,
  `allowAccess` varchar(5) NOT NULL default 'false',
  PRIMARY KEY  (`statusId`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `userId` int(9) unsigned NOT NULL auto_increment,
  `accountId` int(9) NOT NULL,
  `accountStatusId` int(3) NOT NULL,
  `userAddedDate` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `userPositionId` int(3) NOT NULL,
  `firstName` varchar(35) NOT NULL,
  `lastName` varchar(50) NOT NULL,
  `userEmail` varchar(100) NOT NULL,
  `username` varchar(35) NOT NULL,
  `password` blob NOT NULL,
  `preferredLanguageId` int(2) NOT NULL,
  PRIMARY KEY  (`userId`),
  UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

Open in new window


Current query
SELECT a.companyName, u.userId, u.firstName, u.lastName, u.username, u.password, s.statusDescription AS companyStatusDesc, s.allowAccess AS companyAccess, ss.statusDescription AS userStatusDesc, ss.allowAccess AS userAccess 
									   FROM users AS u 
									   LEFT JOIN accounts AS a
									   ON u.accountId=a.accountId
									   LEFT JOIN systemStatus AS s
									   ON a.accountStatusId=s.statusId
									   LEFT JOIN systemStatus AS ss
									   ON u.accountStatusId=s.statusId
									   WHERE u.username ='" . $_SERVER['username'] . "'

Open in new window

0
Comment
Question by:prowebinteractiveinc
  • 3
  • 3
6 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38716958
It seems like the WHERE clause needs additional clarification, maybe testing userStatusDesc or userAccess columns.  You might also consider using an ORDER BY clause.  And if you're checking for only one client record at the time of login, the LIMIT1 will speed up the query.
0
 

Author Comment

by:prowebinteractiveinc
ID: 38717427
by using limit1 how will I be garantied to get the result I want from the image above. is joining the same table twice what I need to do in this case ?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38717541
LIMIT alone will not guarantee anything except a reduced number of rows in the results set.  It's purpose is to reduce the number of rows returned in the query results set, thus improving the performance.  The important strategic elements are in the WHERE and ORDER clauses, and maybe the GROUP.  In these clauses your objective is to cause the "right" answer to percolate up to the top.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:prowebinteractiveinc
ID: 38718731
so can you help me with the query that will give me the right result. Im looking for the second result to be the only result in the image I have attached above

Thanks
0
 

Author Comment

by:prowebinteractiveinc
ID: 38718765
its all good, I figured it out... there was a very small mistake in the query
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38718794
Great, glad you got it sorted out.  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question