rbvinc Posted February 22, 2015 Share Posted February 22, 2015 Please see code below, all connections working. but some how when I select with 'where' is not working. Please fix this, as well I want to display other records in table. Thank you, I can display 'country' on this page when I submit from 'form' selectoion. But could not get rest of the data for that country. Error is 'syntax' error. $query = "select * from countryTable where country = ".$_post['country']. " order by country asc"; Quote Link to comment https://forums.phpfreaks.com/topic/294804-where-help-please/ Share on other sites More sharing options...
marius_haugan Posted February 22, 2015 Share Posted February 22, 2015 Try this! $input = $_POST["country"]; $query = "SELECT * FROM countryTable WHERE country='$input' ORDER BY country ASC"; Quote Link to comment https://forums.phpfreaks.com/topic/294804-where-help-please/#findComment-1506422 Share on other sites More sharing options...
ginerjm Posted February 22, 2015 Share Posted February 22, 2015 Kind of weak to be sorting by country when you only select a single country. You should also sanitize your input value and not just put it into that query. You'll be asking for injection trouble. Use a PDO or mysqlI prepared query to avoid that. Quote Link to comment https://forums.phpfreaks.com/topic/294804-where-help-please/#findComment-1506423 Share on other sites More sharing options...
marius_haugan Posted February 22, 2015 Share Posted February 22, 2015 Kind of weak to be sorting by country when you only select a single country. You should also sanitize your input value and not just put it into that query. You'll be asking for injection trouble. Use a PDO or mysqlI prepared query to avoid that. Agreed.. PDO & prepared statements might be too advanced though. There are easier solutions. Maybe this is better as a quick fix. $dbhost = "localhost"; $dbuser = "root"; $dbpass = "password"; $dbname = "database_name"; $dblink = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); $input = $_POST["country"]; $input = mysqli_real_escape_string($dblink, $input); // or some other sanitizing function (htmlspecialchars) $query = "SELECT * FROM countryTable WHERE country='$input'; Quote Link to comment https://forums.phpfreaks.com/topic/294804-where-help-please/#findComment-1506424 Share on other sites More sharing options...
ginerjm Posted February 22, 2015 Share Posted February 22, 2015 Since the OP has not even shown us any real code yet, I'm going to stick with my original advice. If he/she doesn't know PDO/myqli prepared statements, it's time to do a little reading up on them. The manual makes it pretty clear and simple to understand. Quote Link to comment https://forums.phpfreaks.com/topic/294804-where-help-please/#findComment-1506425 Share on other sites More sharing options...
marius_haugan Posted February 22, 2015 Share Posted February 22, 2015 He did show code. This:$query = "select * from countryTable where country = ".$_post['country']. " order by country asc"; Quote Link to comment https://forums.phpfreaks.com/topic/294804-where-help-please/#findComment-1506429 Share on other sites More sharing options...
ginerjm Posted February 22, 2015 Share Posted February 22, 2015 If he showed us code, pray tell me what interface he is using then? Quote Link to comment https://forums.phpfreaks.com/topic/294804-where-help-please/#findComment-1506433 Share on other sites More sharing options...
rbvinc Posted February 22, 2015 Author Share Posted February 22, 2015 Agreed Sirs. Thank you all, took your suggestions. One last help 'PLEASE'. I got this connection, select * from countryTable where country ='USA' All below listed data is there, now I am trying to get/display in a table rest of these rows data with loop. Like, COUNTRY - STATE - CITY - SCHOOL# USA - Pennsylvania - Pittsburgh - 124 USA - Pennsylvania - Harrisburg - 126 USA - Pennsylvania - Strougtsburg - 177 so on.... Please help. Quote Link to comment https://forums.phpfreaks.com/topic/294804-where-help-please/#findComment-1506437 Share on other sites More sharing options...
marius_haugan Posted February 22, 2015 Share Posted February 22, 2015 (edited) You mean something like this? The while loop will go through your table and store your rows in an associative array, which in turn you can assign to a variable and echo out any way you need. $dbhost = "localhost"; $dbuser = "username"; $dbpass = "password"; $dbname = "my_database"; $dblink = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); $query = "SELECT * FROM countryTable WHERE country='USA'"; $result = mysqli_query($dblink, $query); while ($row = mysqli_fetch_assoc($result)) { $country = $row["country"]; echo $country; } You might consider looking into basic courses on PHP / SQL. There are some really good ones out there. I particularly like the ones from http://lynda.com Edited February 22, 2015 by marius_haugan Quote Link to comment https://forums.phpfreaks.com/topic/294804-where-help-please/#findComment-1506439 Share on other sites More sharing options...
Solution rbvinc Posted February 22, 2015 Author Solution Share Posted February 22, 2015 Yes it worked with little tweaks. I am good with asp, but some how php syntax is getting me. I will better off soon. THANK YOU ALL FOR YOUR HELP. Quote Link to comment https://forums.phpfreaks.com/topic/294804-where-help-please/#findComment-1506467 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.