mr msg Posted August 8, 2010 Share Posted August 8, 2010 Hi I have done this IF statment using PHP and MYSQL so far and its not showing the results I want its 1:18am and I need to get it done now! can anybody help, please.. $result = mysql_query("SELECT * FROM People WHERE Access='1'"); if ($result) { echo"Access"; } else { echo"No Access"; } ?> :shrug: Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/ Share on other sites More sharing options...
dix.selken Posted August 8, 2010 Share Posted August 8, 2010 hello there... could you please tell us a bit more about the table structure, if your sql syntax works on a query browser, if you already have the mysql/mysqli modules activated on your php? and if you also checked your connection variables Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1096476 Share on other sites More sharing options...
abdfahim Posted August 8, 2010 Share Posted August 8, 2010 1) I always prefer use of die statement with mysql_error to each mysql query to see whether it stuck on SQL 2) Always quote table and field name with `` $result = mysql_query("SELECT * FROM `People` WHERE `Access`='1'") or die(mysql_error); Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1096504 Share on other sites More sharing options...
mr msg Posted August 8, 2010 Author Share Posted August 8, 2010 Hi ix.selken, this is the code: (I am using Phpmyadmin for mysql database and dreamweaver to make the php in) <?php $con = mysql_connect("********","********","********"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("names", $con); $result = mysql_query("SELECT * FROM People WHERE Access='1'"); if ($result) { echo"Access"; } else { echo"No Access"; } ?> Hi abdbuet oh rite thats cool but I need an IF statement in there as I need 2 optional outcomes, thats the thing. I just need it to say if value in the "Access" colum equlas "1" then it will "Access" the user if not "No Access", Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1096533 Share on other sites More sharing options...
mr msg Posted August 8, 2010 Author Share Posted August 8, 2010 abdbuet , I also tried the quotes but It didnt work Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1096534 Share on other sites More sharing options...
mr msg Posted August 8, 2010 Author Share Posted August 8, 2010 I also forgot to add that When I run this query it only displays one output either "Access" or "No Access". When I change the value to "0" it still stays the same output as before. so the query does work it just doesn't show either option when the value is changed to either "1" or "0", if you get me, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1096542 Share on other sites More sharing options...
PFMaBiSmAd Posted August 8, 2010 Share Posted August 8, 2010 mysql_query() for a SELECT query either returns a result resource if the query executed without error or it returns a FALSE value if the query fails due to an error. A query that matches zero rows is a successful query and returns a result resource. Your if/else code - if ($result) { echo"Access"; } else { echo"No Access"; } will echo "Access" as long as the query executes without any errors, even if it matches zero rows. You would want to use mysql_num_rows() inside the TRUE branch of the if/else statement to find out if the query returned any rows. Also, your query makes little sense. You are only asking if there are any rows with Access='1'. Do you in fact want to test if a specific visitor has his access value set to 1? Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1096688 Share on other sites More sharing options...
mr msg Posted August 8, 2010 Author Share Posted August 8, 2010 Hi PFMaBiSmAd, oh rite so what I am trying to do is, I have made a visual basic program were when the member log s in it sends a value "1" to the mysql n wen the member logs out it sends a value "0" to the mysql. So im using this php and mysql to connect to databse so when user log s in, the php will pik up on the databse and the value wil be"1" it will show a red image in the php, then when user logs out it that value in mysql change to "0" then the colours goes green in the php, this is what im trting to do but not working this is the code I got so far, please let me know. i dont think the "mysql_num_rows()" will work for what I am trying to do. <?php $con = mysql_connect("*****","*****","*****"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("table"); $query = mysql_query("SELECT * FROM 'people' WHERE 'Access'='1'"); if ($query) { echo'<img src="red.jpg">'; } else { echo'<img src="green.jpg">'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1096711 Share on other sites More sharing options...
PFMaBiSmAd Posted August 8, 2010 Share Posted August 8, 2010 The following link is the definition of what mysql_query() returns, which I repeated in the post I wrote above - http://php.net/mysql_query If you are not gong to read and follow the information that exists for a programming language, you will not be able to write code that works. Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1096713 Share on other sites More sharing options...
mr msg Posted August 8, 2010 Author Share Posted August 8, 2010 oh okay thanks i shal read upon it. Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1096716 Share on other sites More sharing options...
mr msg Posted August 8, 2010 Author Share Posted August 8, 2010 oh im not sure on any of these - mysql_fetch_array() mysql_num_rows() mysql_affected_rows() i tired to add it to the code but nothing happens or just one image would appear and they are not responding to the actual databse bieng updating. i am a bit of a NOOB unfortunantly. Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1096756 Share on other sites More sharing options...
kgenly Posted August 8, 2010 Share Posted August 8, 2010 mysql_fetch_array() only gets one result row at a time. If you are expecting lots of results to come back, you need to use a loop to handle each one at a time. For example: while ($row = mysql_fetch_array($result)) { echo $row[0]; } Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1096760 Share on other sites More sharing options...
mr msg Posted August 9, 2010 Author Share Posted August 9, 2010 mmmmm I did try it but nothing getting done right the code is not repeating and sometimes causes errors. i have a seperate program that updates the value of the field in a table from "1" to "0" all the time when someone logs in and out so its allways getting updated i need php code to get this ALL the time, can someone write the code giving indication what to do, been on this for 3 days got 2 days to finish this if you could would be much appriciated... Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1097126 Share on other sites More sharing options...
fenway Posted August 10, 2010 Share Posted August 10, 2010 You need to consult some basic tutorials on mysql_query() usage. Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1097308 Share on other sites More sharing options...
mr msg Posted August 11, 2010 Author Share Posted August 11, 2010 thanks all, I completed the code in the end. Quote Link to comment https://forums.phpfreaks.com/topic/210097-mysql-query-help/#findComment-1098230 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.