Fetching data from database to drop down list - php

Sorry for asking a duplicate question. I tried few ways given on this website for fetching data. But any of them did not work. I'll put my coding down below.
<?php
mysql_connect('localserver', 'root', '123');
mysql_select_db('database');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
$sql = "SELECT name FROM company";
$result = mysql_query($sql);
echo "<select name='name'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
echo "</select>";
}
?>
Any help would be greatly appreciated.

I think you could use this code.
Make sure you have the correct credentials.
establish your DB connection :
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT name FROM company";
$result = $conn->query($sql);
$conn->close();?>
Then to view the results in your html:
<select name="name">
<?php
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
?>
</select>
I think this would do the trick.

Related

Populating Php Dropdown list from mysql database [duplicate]

This question already has answers here:
Using PHP to populate a <select></select> dropdown? [duplicate]
(7 answers)
Closed 7 months ago.
am trying to populate a dropdown list from mysql database table
here is the code
<div class="form-group">
Select Make
<?php
include '../db_config/dbcon.php';
$sql = "SELECT * FROM vehicle_details";
$result = mysql_query($sql);
echo "<select name='vehicle_make'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>";
}
echo "</select>";
?>
</div>
This is what is displayed by the code
Where am i going wrong??
This worked too, its a modification of janlindo's above
<div class="form-group">
Select Make <?php
include '../db_config/dbcon.php';
$sql = "SELECT * FROM vehicle_details";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select name='vehicle_make'>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>";
}
echo "</select>";
}
?>
</div>
Depending on whats in your dbcon.php, but here's an example using mysqli_query:
<div class="form-group">
Select Make <?php
// start of dbcon
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
//end of dbcon
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM vehicle_details";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select name='vehicle_make'>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>";
}
echo "</select>";
}
$conn->close();
?>
</div>

Can't echo from database to form

So I want to echo a specific value from my database, but it doesn't wok. I'd like to echo that value into a text form something like this:
<h6 class="brand-before" align="center"><small>Konyhakész fa</small></h6><br>
<input type="text" name="konyha" size="18" align="center" value="<?php echo $konyha; ?>" />
And this is how I connect to MySQL:
<?php
mysql_connect("localhost", "koristuzep", "***") or die("Kapcsolódás az adatbázishoz sikertelen.");
mysql_select_db("koristuzep")or die("Kapcsolódás az adatbázishoz sikertelen.");
$konyha = mysql_query("SELECT * FROM $arak WHERE ID=3")
?>
you should fetch the results first from $konyha as given in this example
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

SELECT command denied to user ''#'localhost' for table 'tasks'

I keep getting that error even though all the privileges have been provided to the localhost. I can insert data from same username/pass but it doesn't retrieve it.
I have already tried the methods of commenting the lines in config.inc.php and executing GRANT.
Here's my code below
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "to_do_list";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT `task`, `date_of_task`, `time_of_task`,`id` FROM `to_do_list`.`tasks`;";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
while($row = mysql_fetch_array($result)){
echo "<tr>".
"<td>" . $row['task'] . "</td>".
"<td>" . $row['date_of_task'] . "</td>".
"<td>" . $row['time_of_task'] . "</td>".
"<td> <button value=\"Edit\" class=\"btn btn-warning\"> </td>".
"<td> <button value=\"Edit\" class=\"btn btn-warning\"> </td>".
"</tr>";
}
mysql_close();
?>
Thanks.
Changing to the following code solved my problem. I think it was maybe due to the conflict of using query as a variable.
$sql = "SELECT task,date_of_task,time_of_task,id FROM to_do_list.tasks;";
$result = $conn->query($sql) or die($sql."<br/><br/>".mysql_error());
if ($result-> num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc())
I do see an extra semicolon at the end of your query:
$query = "SELECT ... FROM `to_do_list`.`tasks`;";
As well the MySQL you are using is deprecated.
You are opening a connection with MySQLi:
$conn = new mysqli($servername, $username, $password, $dbname);
Then you are using mysql_* functions:
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
while($row = mysql_fetch_array($result)){
Change your code to use only MySQLi functions, and the error will disappear.

Why do I get an undefined variable error when trying to use PHP in two sections of a page?

I am trying to use PHP in two different parts of a page. But when I try to load the page I receive the error: undefined variable $conn. Why am I getting this error?
<tbody>
<?php
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\"
class=\"amount-type\"
placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
You use $conn at the top of your script
$result = mysqli_query($conn, $sql);
Yet you define it further down
$conn = mysqli_connect($servername, $username, $password, $dbname);
You'll need to move that section up to the first before you can run any queries.
<?PHP
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<tbody>
<?php
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\" class=\"amount-type\" placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
You're defining and connecting to the database after you try to use it. Move the connection part above the query. If this is the only place that you will be using the connection the page then this is fine. But if other pages or sections of the page will use the connection you should look at placing this in a common file that is included at the top of every page that uses it.
<tbody>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\" class=\"amount-type\" placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
I'd be pretty sure that you need to define $conn before you call it in the top part of the php code.
PHP is loaded and executed sequentially... You are defining $conn after using it. Try doing it before. I.e.: Move the second blob of PHP above the first one.
For more on PHP execution order, check out this question.

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

Categories