Jump to content

Getting value's of dropdown from MySql not working


brokenglass

Recommended Posts

Hi guys,

 

Does anyone know why my code isn't working?:

 

 

$servername="127.0.0.1";
$username="root";
$password="";
$dbname="testdb";

$connection = new mysqli($servername,$username,$password);
 
 
$result = "SELECT 'Country' from 'w'";
echo '<select>';
foreach($result as $res) {
   echo '<option value="'.$res['Country'].'">' . $res['Country'] . '</option>';
}
echo '</select>';
}

 

There's not a credential issue, the table is called 'w' and the collumn I'm trying to get the values from is 'Country'

 

Any help is appreciated.

 

Cheers,

Broken

Having defined the query string you need to execute the query then get the results.

 

Also, your mysqli connection needs the dbname as the 4th argument.

$connection = new mysqli($servername,$username,$password,$dbname);
 
 
$sql = "SELECT Country from w ";
$r = $connection->query($sql);
$result = $r->fetch_all(MYSQLI_ASSOC);
echo '<select>';
foreach($result as $res) {
   echo '<option value="'.$res['Country'].'">' . $res['Country'] . '</option>';
}
echo '</select>';

edit: Nearly missed it - don't quote table and column names, and it does help to use meaningful names rather than just w

Having defined the query string you need to execute the query then get the results.

 

Also, your mysqli connection needs the dbname as the 4th argument.

$connection = new mysqli($servername,$username,$password,$dbname);
 
 
$sql = "SELECT Country from w ";
$r = $connection->query($sql);
$result = $r->fetch_all(MYSQLI_ASSOC);
echo '<select>';
foreach($result as $res) {
   echo '<option value="'.$res['Country'].'">' . $res['Country'] . '</option>';
}
echo '</select>';

edit: Nearly missed it - don't quote table and column names, and it does help to use meaningful names rather than just w

Thanks mate, but I get this now:

 

Fatal error: Call to a member function fetch_all() on a non-object in C:\xampp\htdocs\test\index.php on line 16

 

 

see what error you have

$r = $connection->query($sql) or die($connection->error);

That also returns an error.

 

Here's my full code :)

 

 

<?php
$servername="127.0.0.1";
$username="root";
$password="";
$dbname="testdb";

$connection = new mysqli($servername,$username,$password,$dbname);
if($connection->connect_error){
 die("Connection failed  " . $connection->connect_error);
}else{
$sql = "SELECT 'Country' from 'w'";
$r = $connection->query($sql);
$result = $r->fetch_all(MYSQLI_ASSOC);
echo '<select>';
foreach($result as $res) {
   echo '<option value="'.$res['Country'].'">' . $res['Country'] . '</option>';
}
echo '</select>';
}
?>

 

Did you try removing the single quotes as Barand suggested? Try changing this

$sql = "SELECT 'Country' from 'w'";

To this

$sql = "SELECT Country from w";

Thanks man, that now works kind of. It displays the dropdown box with 3 entries (my collumn has 3 entries) but in each collumn it just says Countries rather than the 3 entries in the collumn (English, France, Argentina)

It displays the dropdown box with 3 entries (my collumn has 3 entries) but in each collumn it just says Countries rather than the 3 entries in the collumn (English, France, Argentina)

 

Did you remove the single quotes around 'Country'? The query should be

 

$sql = "SELECT Country from w";

No problem; glad to help  :happy-04:

 

For future reference, if you need to surround a column name with quotes because it's a reserved word in MySQL or maybe the column name contains spaces, you use backticks:

$sql = "SELECT `limit` from w";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.