Populating the values from mysql in a dropdown list - php

the code works but the values are not populated from my database table. it shows an empty dropdown. Please give me some suggesstions friends.
Thanks in Advance...
enter code here
<?php
$mysql_hostname = "host";
$mysql_user = "uname";
$mysql_password = "pass";
$mysql_database = "dbname";
$prefix = "";
$db = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Could not connect database");
//echo "hurray!!!connection successful";
//$query = ("SELECT teacherid,fname from tablename") ;
?>
<table>
<tr>
<td>teacher name</td>
<td><select name="teacherr_name">
<?php
$query = 'SELECT teacherid,fname FROM teacher_master';
$result = mysql_query($query, $db) or die(mysql_error($db));
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="' . $row['teacherid'] . '"> ' . $row['fname'] . '</option>';
}
?>
</select></td>
</tr>
</table>
</html>

Have you selected the database to work with??? You can do this by
mysql_select_db($mysql_database);

You also need to select your database
$db_selected = mysqli_select_db($mysql_database, $db);
Add this line after you made the connection. Also convert all your mysql statements to mysqli. For more details see this

You forgot to select the database, after you've connected to the database server:
mysql_select_db($mysql_database, $db);
needless to say that this is the old way of doing it and it will be depricated...

maybe...
while ($row = mysql_fetch_array($result))
{
echo '<option value="' . $row['teacherid'] . '"> ' . $row['fname'] . '</option>';
}
Even if mysql command are deprecated you can try using the mysql_fetch_array to obtain a "row" and access it array style. You need first to check what you have at db, that's for sure.

select database from where you want to fetch data
mysql_select_db('database name','connection variable');

Related

creating a filled drop down list from phpmyadmin

I need to create a filled drop down list which is linked to my table called called 'dog' in phpmyadmin. I need a user to be able to select a dog from the list and press search and the results show up. And all information be stored in my table in phpmyadmin. This is what I have so far:
It doest work its just an empty drop down and it does nothing. Please help.
<?php
// set up connection parameters
$dbHost = 'hostnamegoeshere';
$databaseName = 'databasenamehere';
$username = 'usernamehere';
$password = 'passwordhere';
// make the database connection
$db = new PDO("mysql:host=$dbHost;dbname=$databaseName;charset=utf8","$username", "$password");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // enable error handling
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); // turn off emulation mode
?>
//Return an error if bad connection
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ')'
.$mysqli->connect_error);
}
//query database for results
$query = $mysqli->query("SELECT * FROM 'dog'");
?>
<h3> Search dogs</h3>
<select>
<?php
$stmt = $mysqli->prepare($query);
$stmt->execute();
$res = $stmt->get_result();
while($dropdown = $res->fetch_array(MYSQLI_ASSOC)) {
echo '<option value="' . $dropdown['dog'] . '"></option>';} ?>
</select>
I will be very grateful for any help
USE THIS
$res = $stmt->get_result();
$res =$res->fetch_array(MYSQLI_ASSOC)
foreach($res as $a){
echo '<option value="' . $a['dog'] . '">'. $a['dog'] .'</option>';} ?>
}
AT PLACE OF
$res = $stmt->get_result();
while($dropdown = $res->fetch_array(MYSQLI_ASSOC)) {
echo '<option value="' . $dropdown['dog'] . '"></option>';} ?>
Number of issues in your CODE
1) Wrap off quotes form table name(SELECT * FROM 'dog')quotes
2) Don't use prepare and query at one time.($mysqli->query,$mysqli->prepare)
3) Add text to your option(<option value="value"></option>)
4) You are mixing mysqli with pdo
You code would be
Create you connection with mysqli
$mysqli=mysqli_connect($dbHost,$username,$password,$databaseName);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ')'
.$mysqli->connect_error);
}
<h3> Search dogs</h3>
<select>
<?php
$stmt = $mysqli->query("SELECT * FROM dog");// wrap off and use only query to run
$stmt->execute();
$res = $stmt->get_result();
while ($dropdown = $res->fetch_array(MYSQLI_ASSOC)) {
echo '<option value="' . $dropdown['dog'] . '">'.$dropdown['dog'].'</option>';// add text to dropdown
}
?>
</select>

populate dropdown list with database value

I want to populate a drop down list with data from a specific field in the database. Here is my sample code
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("disertation ", $con);
$results = mysql_query("SELECT name FROM user_parent;");
?>
<select name="name">
<option value="name">Select one</option>
<?php
while($row=mysql_fetch_array($results))
{ echo '<option value=" ' . $row['name'] . ' ">' . $row['name'] . '</option>'; }
?>
</select>
It's currently displaying nothing from db, any help?
Try this, some white space in your code mysql_select_db("disertation", $con);
mysql_select_db("disertation", $con);
$results = mysql_query("SELECT name FROM user_parent") or die (mysql_error());
Make your mysql_fetch_array call read:
mysql_fetch_array($results, MYSQL_ASSOC)
Without MYSQL_ASSOC you can't refer to column names in $row.
Also, consider using MYSQLI or PDO. MYSQL is considerably outdated.
I would recommend you to use mysqli instead of mysql
<?php
$con=mysqli_connect("localhost","root","","disertation ");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$resource= mysqli_query($con,"SELECT * FROM user_parent");
echo "<select class="name"><option value="name">Select one</option>";
while($result = mysqli_fetch_array($resource)){
echo '<option value="'.$result["name"].'">'.$result["name"].'</option>';
}
echo "</select>";
mysqli_close($con);
?>
To answer your question, directly, you should be first checking if there are any errors (mysql_error()) and then checking there are some results (mysql_num_rows) - these make for easier debugging of your code, since it will tell you what is wrong.
Try this;
<?php
// Connect with user
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Select database
mysql_select_db("disertation", $con);
// Run query
$results = mysql_query("SELECT `name` FROM `user_parent`;") or die (mysql_error());
// Check for no results
if (mysql_num_rows($results) == 0)
{
echo 'There are no options for you to select.';
}
else
{
// If results, loop them.
// If the names are user input, make sure they're displayed in non-raw form
echo '<select name="name">
<option value="name">Select one</option>';
while($row = mysql_fetch_assoc($results))
{
$name = htmlentities($row['name'], ENT_QUOTES, "UTF-8");
echo '<option value=" ' . $name . ' ">' . $name . '</option>';
}
echo '</select>';
}
Will edit with a mysqli_ solution, if that is an option for you, since mysql_ is deprecated and will be dropped from PHP support, sooner or later.
MySQLi solution;
<?php
// Connect to database;
$mysqli = new mysqli("localhost", "my_user", "my_password", "data_base");
if (mysqli_connect_errno())
{
die("Connect failed: " . mysqli_connect_error());
}
$result = $mysqli->query("SELECT `name` FROM `user_parent`");
if ($result->num_rows > 0)
{
echo '<select name="name">
<option value="name">Select one</option>';
while($row = $result->fetch_assoc)
{
$name = htmlentities($row['name'], ENT_QUOTES, "UTF-8");
echo '<option value=" ' . $name . ' ">' . $name . '</option>';
}
echo '</select>';
$result->close();
}
else
{
echo 'There are no options for you to select.';
}

PHP array error in displaying a table

I am trying to display all the data from a mysql table into a html table and the PHP is getting the data fine but the table isn't displaying it properly this is my code:
<table border="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Phone Number</td>
<td>Email</td>
<td>Time</td>
<td>Number Of People</td>
<td>Time Placed</td>
</tr>
<?php
$host = "localhost";
$database = "reservation";
$user = "root";
$pass = "root";
//connection to the database
mysql_connect($host, $user, $pass)
or die ('cannot connect to the database: ' . mysql_error());
//select the database
mysql_select_db($database)
or die ('cannot select database: ' . mysql_error());
$sql = "SELECT * FROM reservation ORDER BY timeplaced";
$result = mysql_query($sql);
while($data = mysql_fetch_row($result)){
echo("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td></tr><td>$data[3]</td></tr><td>$data[4]</td></tr><td>$data[5]</td></tr><td>$data[6]</td></tr>");
}
?>
</table>
Please help.
$sql = "SELECT * FROM reservation ORDER BY timeplaced";
$result = mysql_query($sql);
while($data = mysql_fetch_row($result)){
echo("<tr>
<td>$data[0]</td>
<td>$data[1]</td>
<td>$data[2]</td>
<td>$data[3]</td>
<td>$data[4]</td>
<td>$data[5]</td>
<td>$data[6]</td>
</tr>");
}
when you use TDs in TR, they all must be same numbers, or use colspan, try the above echo statement, it will display your table properly.
When you are echoing array items it should be
echo "<tr><td>" . $data[0] . "</td></tr>";
echo "<tr><td>{$data[0]}</td><td>{$data[1]}</td><td>{$data[2]}</td></tr><td>{$data[3]}</td></tr><td>{$data[4]}</td></tr><td>{$data[5]}</td></tr><td>{$data[6]}</td></tr>";
Use braces.

Unable to populate list from databae

I am trying to retreive data from a database and populate a list from it, there doesnt seem to be errors in my code and still the list is not populating, my code
<?php
$hostname = "localhost";
$username = "username";
$password = "password";
$dbase = "db";
$link = # mysql_connect($hostname, $username, $password);
$db_selected = # mysql_select_db($dbase);
?>
<?php
include("scripts/dbconnect.php");
$query="select class from school";
$result=mysql_query($query);
$numrows=mysql_num_rows($result);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<option value="'.$row['class'].'">'.$row['class'].'</option>';
}
?>
any help much appreciated, thanks
Options must be wrapped with select tag:
echo "<select name='class'>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<option value="'.$row['class'].'">'.$row['class'].'</option>';
}
echo "</select>";
Additionally do:
if (!$link) {
die('Could not connect: ' . mysql_error());
break;
}
To see if there are any errors of mysql connection.
You need to echo a select element.
<?php
include("scripts/dbconnect.php");
$query="select class from school";
$result=mysql_query($query);
$numrows=mysql_num_rows($result);
echo "<select>"
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<option value="'.$row['class'].'">'.$row['class'].'</option>';
}
echo "</select>"
?>

display all data from my User table in my database

My question is how to display all data from my users table in my database?
I have this.
$loop = mysql_query(“SHOW users FROM $dbname”) or die (‘cannot select tables’);
You want to SELECT the users, not SHOW them.
Basic SQL loop example:
$sql = mysql_query("SELECT * FROM `users`");
while ($row = mysql_fetch_object($sql)) {
echo $row->id . ' ' . $row->nickname . '<br />';
}
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$result = mysql_query(“SELECT * FROM Users”, $link) or die (‘cannot select tables’);
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}
mysql_free_result($result);
$loop = mysql_query('SELECT * FROM `users`') or die();
You don't want your database name to be in the query, and you want to be using SELECT
If you haven't connected to the database earlier then you need to add this before your query:
mysql_connect($mysql_host, $mysql_user, $user_password);

Categories