Jump to content

Getting value's of dropdown from MySql not working


brokenglass
Go to solution Solved by Barand,

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

Link to comment
Share on other sites

  • Solution

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

Edited by Barand
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

 

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

 

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)

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.