charles07 Posted December 15, 2011 Share Posted December 15, 2011 have you ever wondered how to select a single coloumn from mysql using php & checking whether a value or variable exists in the resulting array... well here is the code $busid = '5412'; $results = mysql_query("SELECT `myname` FROM mytable WHERE myuser = 'user'"); while($rows = mysql_fetch_array($results)) { $colnames[] = $rows['myname']; } if(!in_array($busid, $colnames))die("Access denied"); Quote Link to comment https://forums.phpfreaks.com/topic/253209-select-a-single-coloumn-from-mysql/ Share on other sites More sharing options...
SergeiSS Posted December 15, 2011 Share Posted December 15, 2011 You are selecting an information (MySQL) and then check if something exists in it (PHP). You'd better do it inside MySQL's request. For example: SELECT `myname` FROM mytable WHERE myuser = 'user' and myname='5412' Maybe "WHERE myname='5412'" is enough. Send this request and then check just a number of rows in result but not values. It will work faster. The difference could be 10e-3 seconds, of course, or something like this Quote Link to comment https://forums.phpfreaks.com/topic/253209-select-a-single-coloumn-from-mysql/#findComment-1298052 Share on other sites More sharing options...
charles07 Posted December 16, 2011 Author Share Posted December 16, 2011 thanks SergeiSS, i think you are right, but WHERE myuser = 'user' is needed since each myname is related to specific users. anyways let me try out your code. Quote Link to comment https://forums.phpfreaks.com/topic/253209-select-a-single-coloumn-from-mysql/#findComment-1298403 Share on other sites More sharing options...
MasterACE14 Posted December 16, 2011 Share Posted December 16, 2011 add 'LIMIT 1' to the end of the query if you want just a single record. Quote Link to comment https://forums.phpfreaks.com/topic/253209-select-a-single-coloumn-from-mysql/#findComment-1298406 Share on other sites More sharing options...
charles07 Posted December 16, 2011 Author Share Posted December 16, 2011 thanks SergeiSS that worked like a charm SELECT `myname` FROM mytable WHERE myuser = 'user' and myname='5412' iam a enthusiastic newbie to PHP, googled the saem topic but couldn't get an answer, now it looks fine Quote Link to comment https://forums.phpfreaks.com/topic/253209-select-a-single-coloumn-from-mysql/#findComment-1298418 Share on other sites More sharing options...
SergeiSS Posted December 16, 2011 Share Posted December 16, 2011 I'm glad that it helps Just to note: it's not a PHP task, it's MySQL task. I'd recommend you to read more about MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/253209-select-a-single-coloumn-from-mysql/#findComment-1298426 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.