By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Discussion: Select Single Column Using MySQL

P: 45
have you ever wondered how to select a single coloumn from mysql using php, then checking whether a value or variable exist in the resulting array... well here is the code

Expand|Select|Wrap|Line Numbers
  1. $busid = '5412';
  2. $results    =   mysql_query("SELECT `myname` FROM mytable WHERE myuser = 'user'");
  3.         while($rows = mysql_fetch_array($results))
  4.         {   
  5.             $colnames[] = $rows['myname'];
  6.         }
  7.         if(!in_array($busid, $colnames))die("Access denied");
Dec 15 '11 #1
Share this Question
Share on Google+
7 Replies


Dormilich
Expert Mod 5K+
P: 8,639
why going to the extend of using arrays? just get the count: SELECT COUNT() FROM mytable WHERE myuser = ? AND `myname` = ?
Dec 15 '11 #2

NeoPa
Expert Mod 15k+
P: 31,345
This wasn't close to Article quality, but may prove useful as the seed for a discussion.
Dec 15 '11 #3

P: 45
thanks Dormilich,

i think you are right. let me try out your code & would get back to you.
Dec 16 '11 #4

P: 45
thanks Dormilich

i used the below query, now it works fine,

SELECT `myname` FROM mytable WHERE myuser = 'user' and myname='5412'

Is there any difference between the above query & your query(SELECT COUNT() FROM mytable WHERE myuser = ? AND `myname` = ?), which is better?

iam a enthusiastic newbie to PHP, googled the same topic but couldn't get an answer, now it looks fine
Dec 16 '11 #5

Dormilich
Expert Mod 5K+
P: 8,639
Is there any difference between the above query & your query
there is. my query will always return one result (the number of rows) while your query returns all matching rows, which a) can be no results at all and b) returns redundant information (the myname field)
Dec 16 '11 #6

P: 45
but how do i check whether the result is above 0
Dec 16 '11 #7

Dormilich
Expert Mod 5K+
P: 8,639
it is the result* of my query. you can directly use it in a comparison.

e.g. by using PDOStatement->fetchColumn()
Dec 16 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.