Jump to content

where =? help please


rbvinc
Go to solution Solved by rbvinc,

Recommended Posts

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";

 

 

 

 

 

Link to comment
Share on other sites

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';
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by marius_haugan
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.