populating dropdown list from mysql - php/html - php

Trying to populate a dropdown list from my database - connection is ok and in my mind the code I have should work, but currently getting a blank dropdown...
Have looked at PHP- Fetch from database and store in drop down menu html as well as other tutorials but alas no luck so far!
code is as follows...
<?php
//get constants for database
require("database.php");
// Opens a connection to a MySQL server
$connection = mysqli_connect ($server, $username, $password);
if (!$connection){
die('Not connected : ' . mysqli_error());
}
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error($connection));
}
$query = "SELECT * FROM route WHERE 1";
$result = mysqli_query($connection, $query);
echo '<select name="list" style="width:400px;">';
while($r = mysqli_fetch_assoc($result)){
echo "<option value=".$r['alt']."</option>";
}
echo '</select>';
?>

<option> tag is broken.
Corrected code:
while($r = mysqli_fetch_assoc($result)){
echo '<option value="'.$r['alt'].'">'.$r['alt'].'</option>';
}
Note: You can use single and double quotes either, but, they should be properly closed.

Please have a look on this Select Dropdown Syntax
Syntax For Select Dropdown
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</select>
You have missed a closing of value in option.
<?php
//get constants for database
require("database.php");
// Opens a connection to a MySQL server
$connection = mysqli_connect ($server, $username, $password);
if (!$connection){
die('Not connected : ' . mysqli_error());
}
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error($connection));
}
$query = "SELECT * FROM route WHERE 1";
$result = mysqli_query($connection, $query);
//Changes From Here
?>
<select name="list" style="width:400px;">
<?
while($r = mysqli_fetch_assoc($result))
{?>
<option value="<?echo $r['alt'];?>"><?echo $r['alt'];?></option>
<?}?>
</select>

<?php
//get constants for database
require("database.php");
// Opens a connection to a MySQL server
$connection = mysqli_connect ($server, $username, $password);
if (!$connection){
die('Not connected : ' . mysqli_error());
}
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error($connection));
}
$query = "SELECT * FROM route WHERE 1";
$result = mysqli_query($connection, $query);
echo '<select name="list" style="width:400px;">';
while($r = mysqli_fetch_assoc($result)){
echo "<option value=".$r['alt'].">".$r['alt']."</option>";
}
echo '</select>';
?>
so if your select condition is right/hidden by you then you will have to add a display value between option tag. As per the code you have assigned values in option value tag but not given the values to be displayed. Try the above

Related

Php seven update messed up my program

So recently with the php 7 update they removed all the mysql commands. however one of my programs for my internship was using such commands in one of the pages. however when i change it to mysqli it no longer works like it should. can someone help perhaps?
$connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());
//select MySQLi dabatase table
$db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());
$result = mysqli_query($connection, "SELECT * FROM gebiedsmanagers WHERE Datum >= NOW()");
Pastebin code
With kind regards,
Dayne Tersluijsen
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given:
mysqli_select_db("prodyne", $con);
To
mysqli_select_db($con, "prodyne")
Try this out!
<?php
//MySQLi information
$db_host = "localhost";
$db_username = "username";
$db_password = "password";
//connect to mysqli database (Host/Username/Password)
$connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());
//select MySQLi dabatase table
$db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());
//fetch information from your database
$result = mysqli_query($connection, "SELECT * FROM gebiedsmanagers WHERE Datum >= NOW()");
while($row = mysqli_fetch_array($result))
{
$counter ++;
?>
<tr><td><?php echo date('d-m-Y', strtotime($row['Datum']));?></td><td>
<?php echo $row['Voor1500']; ?></td><td><?php echo $row['Na1500']; ?>
</td></tr>
<?php
if($counter >= 120) {
break;
}
I hope this has helped you.

Show all tables within given MySQL database

I'm trying to show all tables within a given MySQL database with php.
I'm very new to all this though and can't find a solution for it. Keeps giving an error 'no found file or directory'.
Anyone who can point out my mistakes here please?
Much appreciated!
<?php include "../inc/dbinfo.inc"; ?>
<html>
<body>
<h2>LIST TABLES FROM DATABASE</h2>
<?php
// Create connection
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
// Check connection
if ($conn->connect_error) {
die("Connection with the database failed: </br>" . $conn->connect_error);
}
echo "Connection established with the database! </br>";
// SQL to show tables
$sql = "SHOW TABLES FROM paperlessdb";
$result = mysql_query($sql);
if (!$result) {
echo "Database error, could not list tables.\n</br>";
echo 'MySQL error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "- {$row[0]}\n </br>";
}
mysql_free_result($result);
?>
First make up your mind, either use mysqli procedural or object orientated. Not a combination of both because its confusing. To avoid that all together use pdo instead.
Now properly connect to the database, you can select the database when connecting to it automatically:
const DB_DATABASE = 'paperlessdb';
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
// Check connection
if ($conn->connect_error) {
die("Connection with the database failed: </br>" . $conn->connect_error);
}
if($result = $conn->query('SHOW TABLES')){
while($row = $conn->fetch_array($result)){
$tables[] = $row[0];
}
}
print_r($tables);
Use below query,
$sql = "SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'paperlessdb'";
We are fetching the data from information_schema db which stores the meta data about our database.
You are using mysqli to connect to the database but use the depreciated mysql to query the database.
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)){}
mysql_free_result($result);
You should use mysqli_query() and mysqli_fetch_array() instead.
It'a a bit more complex but mysql is decrecated and remove as PHP 7 so no choice to jump ahead. Check out PDO ass well. I personally go for mysqli but most say pdo is more intuitive.
It should look more something like:
$result = mysqli_query($conn,$sql);
if(!$result){
die('MySQL error: ' . mysqli_error($conn));
}
while ($row = mysqli_fetch_row($result)) {
echo "- {$row[0]}\n </br>";
}

Select SQL query is not working in PHP

I am having trouble with an SQL query that I have inserted into a piece of PHP code to retrieve some data. The query itself works perfectly within SQL. I am using the following PHP script.
I have the following objectives:
Connect to the existing database. This part works well.
Get data from the column 'Brand' of the table 'Transport' in $sql. This part is not working at this stage. echo ($sql) returns SELECT Brand FROM Transport WHERE Type = 'car'
Could you please let me know if you see the solution to this issue and if the remaining part of the code is correct. This is my f_sqlConnect()
function f_sqlConnect() {
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!link) {
die('Could not connect: '.mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can not use'.DB_NAME.
': '.mysql_error());
}
}
/*This function cleans up the array to protect against injection attacks */
function f_clean($array) {
return array_map('mysql_real_escape_string', $array);
}
<?php
// Create connection
$link = f_sqlConnect();
// Getting data from the column Brand of the table Transport
$sql = "SELECT Brand FROM Transport WHERE Type = 'car'";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Brand: " . $row["Brand"]. "<br>";
}
} else {
echo "0 results";
}
$link->close();
?>
Here is the code, without seeing your f_sqlConnect(); mothod. This method should return connection string for DB in your case. But you can use following code this must work.
<?php
$servername = "Your_db_host";
$username = "your_db_username";
$password = "your_db_password";
$dbname = "your_DB_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Brand FROM Transport WHERE Type = 'car'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Brand: " . $row["Brand"];
}
} else {
echo "0 results";
}
$conn->close();
?>
NOTE: Object oriented way of mysqli, You can use procedural way too to connect and get data.

dropdown to show table in DB

I am working on the following code where the user chooses a table from a dropdown. On change, it displays the table but at the moment it is echoing some of the code to screen.
<?php
$dbh = "localhost";
$dbn = "dbname";
$dbu = "dbuser";
$dbp = "dbpassword";
$conn = mysql_connect($dbh,$dbu,$dbp) or die("Unable to connect do database.");
mysql_select_db($dbn, $conn) or die("Unable to select database.");
$result = mysql_query("SHOW TABLES FROM $dbn") or die("Cannot list table names.");
echo "
<form name=\"table_browser\" action=\"".$PHP_SELF."\" method=\"GET\" >
<select name=\"t\" onChange=\"javascript:submit();\">
<option>Select a table</option>
";
while ($row = mysql_fetch_row($result)){
echo " <option value=".$row[0].">".$row[0]."</option>\n";
}
echo " </select>
</form>\n";
if (!isset($t)){
die("Please select a table");
}
?>
You need to use full php code here
<?php
?>

How can I use the data I get from my database using PHP, MySQL, Bootstrap?

My question simple Ex. Country, --> State --> City.
How to make like above this, I need to implement in my website. Like dropdown menu.
After the select the City its redirect to the City peoples list.
How its will make ???
// Create Connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
trigger_error("Connection failed: " . mysqli_connect_error());
}
//Run Query
$stmt = "SELECT * FROM country";
$result = mysqli_query($conn,$stmt) or die(mysqli_error($conn));
while(list($category) = mysqli_fetch_row($result)){
echo '<option value="'.$category.'">'.$category.'</option>';
}
mysqli_close($conn);
?>
</select>
Any other alternative code ?
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified
<?php
$dbname = "myDB";
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM country';
mysql_select_db($dbname);
$retval = mysql_query( $sql, $conn );
if(! $retval ){
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)){
echo '<option value="'.$row["id"].'">'.$row["name"].'</option>';
}
mysql_close($conn);
?>
http://php.net/manual/en/function.mysql-query.php
example http://www.tutorialspoint.com/mysql/mysql-select-query.htm

Categories