Working on a project where I am going to extract from my database and show the pool name in a form select. But very unsure how to go further than this.
$con=mysqli_connect("localhost","root","","nih_bw");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "Select name from pools";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
mysqli_close($con);
You can do something like this:
<?php
$con=mysqli_connect("localhost","root","","nih_bw");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"Select name from pools");
echo "<select name='mypool'>";
$default_name = "foo_bar";
while($row = mysqli_fetch_array($result)){
$opt_name = $row['name'];
$str_selected = "";
if($opt_name == $default_name){
$str_selected = "selected";
}
echo "<option value='".$opt_name."' ".$str_selected." >" . $opt_name. "</option>";
}
echo "</select>";
mysqli_close($con);
?>
You can see an introductory article here:http://www.w3schools.com/Php/php_mysql_select.asp
Hope this helps.
Try using your query like this:
<select name="batch">
<option value="">Select One</option>
<?php
$pd=$dbh->prepare("SELECT * FROM `pools`");
$pd->execute();
foreach($pd->fetchAll() as $rw) :
?>
<option value="<?php echo $rw['id'];?>"><?php echo $rt['name'];?></option>
<?php endforeach; ?>
</select>
It is in pdo, but you will get my logic.
I think this will work for you. (Code is not tested)
<?php
$record = array();
$con=mysqli_connect("localhost","root","","nih_bw");
// Check connection
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "Select name from pools";
if(!$result = mysql_query($sql,$con)){
die('There was an error running the query [' . $db->error . ']');
}
else{
while($row = mysql_fetch_assoc($result)){
$record[] = $row;
}
}
mysqli_close($con);
?>
<select>
<?php
foreach($record as $value){
echo "<option>".$value."</option>";
}
?>
</select>
Related
For the code below added, i am not getting any result printed.
$con = #mysqli_connect("localhost","root","","temp");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query="SELECT * FROM `login`";
echo $query;
$result=#mysqli_query($query) or die(mysql_error());
while($row=mysqli_fetch_array($result))
{
echo $row["username"];
}
Try the below code it will work
//conection:
$con = mysqli_connect("localhost","root","","temp") or die("Error " . mysqli_error($con));
//consultation:
$query = "SELECT * FROM login" or die("Error in the consult.." . mysqli_error($con));
//execute the query.
$result = $con->query($query);
//display information:
while($row = mysqli_fetch_array($result)) {
echo $row["username"] . "<br>";
}
Use this code as it is.
$con=mysqli_connect("localhost","root","","temp");
$result = mysqli_query($con,"SELECT * FROM login");
while($row = mysqli_fetch_array($result))
{
echo $row["username"];
}
// use this code and plz check your db name
$host='localhost';
$user='root';
$pass='';
$db_name='temp';
$con=mysqli_connect($host,$user,$pass,$db_name);
if($con)
{
echo "db connect succecssfully";
}
$slt="select * from login";
$query=mysqli_query($slt,$con);
while($row=mysqli_fetch_array($query))
{
echo $row["username"];
}
<?php
$con=mysqli_connect("localhost","root","","temp");
// Here localhost is host name, root is username, password is empty and temp is database name.
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$result = mysqli_query($con,"SELECT * FROM login");
while($row = mysqli_fetch_array($result)) {
echo $row["username"] . "<br>";
}
mysqli_close($con);
?>
Use this. it may solve your problem.
//connection
$con = mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
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.';
}
<h1>Matches</h1>
<?php
$con=mysqli_connect("localhost","root","","esports");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM NA");
while($row = mysqli_fetch_array($result))
{
echo $row['team1'] . " vs. " . $row['team2'];
echo "<br>";
}
mysqli_close($con);
?>
I'm trying to use this to get information from my MySQL Database and then display it onto a webpage using an HTML Table. I'm kind of new to this, so how would I go about doing this? Any help is appreciated.
To connect to MySQL database you need to use mysql_connect and select the database with mysql_select_db and finally do a mysql_fetch_array
<?php
$con =mysql_connect("localhost","root","","esports");
mysql_select_db('your data base');
$result = mysql_query($con,"SELECT * FROM NA");
while($row = mysql_fetch_array($result))
{
echo '<pre>';
print_r( $row);
echo '</pre>';
}
mysql_close($con);
?>
There doesn't seem to be any problem with your query, so if you want to just format it as a table, you could do
echo "<table border=1>";
while ($row = mysql_fetch_array($result)
{
echo '<tr><td>';
echo $row['team1'];
echo '</td><td>';
echo $row['team2'];
echo '</td></tr>';
}
mysql_close($con);
Version 4:
I have taken away the pull down menu for now, I just want the info to be posting correctly.
As it shows with the first line fab1 shows #2, in the second line it shows 1, --None-- instead of 2, Andy Khal. If anyone can figure out why, it be appreciated. I've done about as much as I can to figure this out and I'm lost.
<?php
// Connect to the database.
require_once('tb/connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno()) {
die("MySQL failed to connect: " . mysqli_connect_error());
}
// Create the SQL query
$testbed = "SELECT * FROM testbed";
$user = "SELECT * FROM user";
// Execute the SQL query and store the result set in
// the $result variable.
$testbed = mysqli_query($dbc, $testbed) or die("Failed to execute query on tesbed table: " . mysqli_error($dbc));
$user = mysqli_query($dbc, $user) or die("Failed to execute query on user table: " . mysqli_error($dbc));
// Read the results.
$row = mysqli_fetch_assoc($testbed);
if(!$row)
{
echo 'Query failed<br />';
}
else
{
echo "Query for Testbed Fabricator is : " . $row["fab1"] . "<br />";
}
$row = mysqli_fetch_assoc($user);
if(!$row)
{
echo 'Query for Testbed Fabricator failed<br />';
}
else
{
echo "Query for User ID # is : " . $row["userid"], $row["user"] . "<br />";
}
// Free the result set.
mysqli_free_result($testbed);
mysqli_free_result($user);
?>
Yes it is. You can specify the selected value with the selected keyword as a html attribute.
<option value="Username" selected>Username</option>
That makes this:
while($row = $result->fetch_assoc())
{
$user = $row['user'];
echo '<option value="' . $user . '"';
if($user is known)
{
echo ' selected';
}
echo '>' . $user . '</option>\n';
}
Update
In this snipped $choosen is the selected user's name.
echo'<div id="fab1">';
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$mysqli->select_db('user');
$result = $mysqli->query("SELECT * FROM user");
echo "<select name='fab1'>\n";
while($row = $result->fetch_assoc())
{
echo '<option value="' . $row['user'] . '"';
if($row['user'] == $choosen)
{
echo ' selected';
}
echo '>' . $row['user'] . '</option>\n';
}
echo "</select>\n";
echo '</div>';
I am trying to make the different the different rows have line breaks but its not working.
How is this done!? Please check my code below
Thanks guys!
James
<?php
$conn = mysql_connect("", "", "");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
{
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
}
if (!mysql_select_db("")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT name,lastname,email
FROM test_mysql
WHERE name LIKE '$search%' AND lastname LIKE '$searchterm'";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["name"];
echo $row["lastname"];
echo $row["email"];
}
mysql_free_result($result);
?>
<?php echo $row["name"];?>
<br>
<?php echo $row["lastname"];?>
<br>
<?php echo $row["email"];?>
Beats me what you find so hard about it:
while ($row = mysql_fetch_array(...)) {
echo ...
echo '<br>';
}