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>"
?>
Related
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.
My goal is to populate an HTML select box from MySQL using a PHP function. I started by putting the code directly on the HTML page, and I got it working.
<label for="product_Category">Product Category</label>
<?
$field_Name = "category_Name";
$table_Name = "Product_Category";
$sql = "Select " . $field_Name . " From " . $table_Name;
$results = mysqli_query($link, $sql);
echo "<select>";
echo "<option value = \" \" >Select Category"</option>;
while($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
echo "<option value = ' " .$row[$field_Name] . "'>" . $row[$field_Name]. "</option>";
}
echo "</select>";
?>
I have multiple select boxes, so I thought it would be easier to create a function and pass the table name, and field name as arguments
I moved the PHP function to its own file, and use an include statement in my HTML. But once I tried to call the PHP function, the select box won't populate. The select box shows up on the form like it should, but its empty.
PHP
<?
function Populate_Select($table_Name, $field_Name){
$sql = "Select " . $field_Name . " From " . $table_Name;
$results = mysqli_query($link, $sql);
echo "<select>";
echo "<option value = \" \" >Select Category </option>";
while($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
echo "<option value = ' " .$row[$field_Name] . "'>" . $row[$field_Name]. " </option>";
}
echo "</select>";
}
?>
DB Config
<?
$host = "localhost";
$db_userName = "root";
$db_Password = "root";
$db_Name = "mydb";
$link = mysqli_connect($host, $db_userName, $db_Password, $db_Name);
if (!$link){
die("Database Connection failed " . mysqli_connect_error);
}
?>
HTML Code
<? include 'PopulateSelect.php' ?>
<? include 'DB_Config.php ?>
<!--Rest of HTML CODE-->
<label for="product_Category">Product Category</label>
<? Populate_Select('Product_Category', 'category_Name'); ?>
Where did I go wrong when I called the function?
Is it better practice to use a function or am I better off just writing separate code for each select box?
The problem is that your $link variable is not defined. You need to create a class, OOP makes life easier, like this:
//myclass.php
class MyClass {
private $link;
function __construct() {
$host = "localhost";
$db_userName = "root";
$db_Password = "root";
$db_Name = "mydb";
$this->link = mysqli_connect($host, $db_userName, $db_Password, $db_Name);
if (!$this->link) {
die("Database Connection failed " . mysqli_connect_error);
}
}
function __destruct() {
mysqli_close($this->link);
}
function Populate_Select($table_Name, $field_Name) {
$sql = "Select $field_Name From $table_Name";
$results = mysqli_query($this->link, $sql);
$select = "<select>";
$select .= "<option value=''>Select Category</option>";
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
$select .= "<option value='$row[$field_Name]'>$row[$field_Name]</option>";
}
$select .= "</select>";
return $select;
}
}
Then you can call this on your other pages by including that .php file and accessing the class, like this:
//your some_html.php
include 'myclass.php';
$obj = new MyClass();
$select = $obj->Populate_Select("your_table_name", "your_field_name");
echo $select;
It looks like your $link object is not available in the function you declared. Try passing the $link object to the function.
It could look something like this:
function Populate_Select($link, $field_Name, $table_Name){ ... }
You could also pass the connection as a reference to the function.
function Populate_Select($field_Name, $table_Name, &$link){ ... }
And then when you call it, add $link as the third argument. Otherwise, you're looking for a global variable inside of a function.
I have several "providers" with website urls listed in a table. I am wondering how I would link their websites that are listed in the table to make them live urls.
<?php if($Website){
echo "<div class='providerData1'>Website: </div> <div class='providerData1 providerData2'>" . $Website . "</div><br />"; }
?>
Any ideas? Thank you. :)
Base yourself on the following: (details can be found in comments)
<?php
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";
$db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db->connect_errno > 0) {
die('Connection failed [' . $db->connect_error . ']');
}
if($db->connect_errno > 0){
die('Unable to connect [' . $db->connect_errno . ']');
}
// Use
// $sql = mysqli_query($db, "select * from tablename ");
// Or select particular columns
$sql = mysqli_query($db, "select link,name from tablename LIMIT 1");
// You can remove LIMIT 1 if you want to show them all
echo "Pages (found):";
echo "<hr>";
while ($row = mysqli_fetch_array($sql)){
$name= $row['name'];
$Website = $row['link'];
echo $name;
echo "<br>";
echo "<div class='providerData1'>Website: </div> <div class='providerData1 providerData2'><a href='$Website'>" . $name . "</a></div><br />";
}
echo "<br>";
?>
In place of the entire while loop, you can replace it with:
Sidenote (having a URL for the link column)
echo '<table><tr><th>Name</th><th>Page</th><th>Link</th></tr>';
while ($row = $result->fetch_assoc()){
echo '<tr><td>'.$row["name"].'</td>';
echo '<td>'.$row["link"].'</td>';
echo '<td>Link</td></tr>';
}
echo '</table>';
and display your links neatly in an HTML table.
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);
I have a form where I am getting values from previous page in $GET. I want to fetch around 3 different values from database and display them in textbox. How will I do it? Following is my code for getting data from database.
<?php
require_once('config.php');
$id = $_GET['id'];
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$query = "select question,price,sequence from questions where status = 1 and qid =".$id;
//echo $query;
$result=mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$num_rows = mysql_num_rows($result);
if($num_rows==0) {
echo '<center><font color="red"><b>No record found!!</b></font></center>';
}
else {
$row = mysql_fetch_array($result);
echo $row['question'];
echo $row['sequence'];
echo $row['price'];
}
?>
Thanks
Pankaj
<textarea><?php echo htmlentites($row['question']);?></textarea>
or
<input type="text" value="<?php echo htmlentites($row['question']);?>" />
Depending on your fancy.