How to put data from database in a select box - php

I have a select box to gather the id numbers from the database, it's showing how much is in the database but the options are blank.
This is the code I have. I just want to be able to bring id number from my database in this select box.
php
<?php
$mysqli = new mysqli("localhost", "root", "", "volunteer");
/* check connection */
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM registrations";
$result = mysqli_query($mysqli,$query);
while($rows = mysqli_fetch_assoc($result))
{
echo "<option value=" . $rows['idnumber'] . "></option>";
}
?>

Related

how to get my data shown in my dynamic URL with PHP

How should my test.php get data from a database? My database name is "example" and the things i want to get are test1, test2, test3.
<html>
<a href="test.php?ID=<?php echo $kerko_rezultatin['id_object'];?>">
</a>
</html
First you need to create a connection to you database:
$conn = mysqli_connect("localhost", "username", "pass",
"db_name");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
Then you need to create a query to select data from your database:
$sql = "SELECT *
FROM table
WHERE 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($kerko_rezultatin = mysqli_fetch_assoc($result)) {
echo "<a href='test.php?ID=".$kerko_rezultatin['id_object']."'> </a>"
;}

SQLi Error when trying to echo a certain ID in PHP

I am making a status update page for practicing PHP and MySQL.
I am currently trying to echo the saved status on the page.
The statuses are saved in a row called "Status" and they are all given a certain ID in a row called "statusID."
The problem I am having is which fetch I want to use because converting it into a string using (string)$var doesn't work. ($var is an example).
Also, the $idNum variable is something for later use, shouldn't have anything to do with this.
Here is the code: (Obviously the first variables are censored so none tries to connect to the database, the connection working in the actual code.)
The problem lies in the $fetchRes I believe.
<?php
$idNum = 1;
$servername = "censored";
$username = "censored";
$password = "censored";
$db_name = "censored";
$conn = mysqli_connect($servername, $username, $password, $db_name);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Status FROM SavedStatuses WHERE statusID=1;";
$statusQuery = mysqli_query($conn, $sql);
$fetchRes = mysqli_fetch_assoc($statusQuery);
if($conn->query($sql) == TRUE)
{
echo $fetchRes;
} else {
echo "Failed to retrieve status, error: " . $conn->error;
}
?>
As I mentioned in comments, you are querying twice and not looping over (successful) results.
A "loop" is to use either a while or a foreach. I used a while loop for this example.
From the "official" manual:
http://php.net/manual/en/mysqli-result.fetch-assoc.php
Example:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
So in your case, your code would read as:
Sidenote: Status and status are two different animals, so make sure the letter case matches (in the loop).
<?php
$mysqli = new mysqli("censored", "censored", "censored", "censored");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT Status FROM SavedStatuses WHERE statusID=1";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
echo $row["Status"];
}
/* free result set */
$result->free();
}else{
echo "The query failed: " . mysqli_error($mysqli);
}
/* close connection */
$mysqli->close();
?>

Global variable in query php/mysql

I have a website with 3 dropdown menus that loop through a database in mysql to get 50 bands in a list you can chose. I have made it so if you vote for band number 1, he gets 10 points, number 2, 6 points etc.. Now i have the problem that you can vote for 3 the same bands bcs those dropdown are each looping trough the database. I want to make a global variable so i can put in the query from dropdown 1 of 2 that it doesnt show the chose in dropdown 1.. so like "WHERE NOT LIKE 'variable'"
Here is my code for dropdown 1:
<?php echo '<select name="bands1">';
echo '<option>Kies een band</option>';
$sql="SELECT * FROM bands ORDER BY band";
$result = mysqli_query($db_link, $sql);
if (!$result){
die ("Database connection failed!");
}
while($row = mysqli_fetch_assoc($result)){
echo '<option value="' . $row['BandID'] . '">' . $row['band'] . '</option>';
}
echo '</select>';
?>
this is for the 2 other dropdown menus the same only the select name is different
this is my connection file that i included at the top of the html file:
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'mysql_enquete');
$db_link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Verbindingsfout");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("", $row[0]);
$result->close();
}
?>
Somebody on stackoverflow that can help me?

Why query not working in from my php

I have query, and i have database. When I query in dbforge(mysql) i got result. When from php, i got nothing. Can't understand why? If I write query simple like "select * from table where id = 1", its working.
Below my code:
<?php
$conn = mysqli_connect("localhost", "username", "pass", "db");
ini_set('max_execution_time', 300);
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_query($conn, "SET CHARACTER SET utf8 ");
$result = mysqli_query($conn, "Select distinct level_two FROM reportls._report_sales_report Where level_one = 'Музыка'");
if($result == false)
{
die('INVALID QUERY: '.mysqli_error($conn));
echo "error";
}
while ($data = mysqli_fetch_row($result))
{
if(!empty($data[0]))
$sendBack = $sendBack."<option value=\"$data[0]\">$data[0]</option>";
}
echo ($sendBack);
$sendBack = '';
mysqli_free_result($result);
?>
This code return nothing. But if I paste my query into my dbforge, it returns rows.
Could you help me, or write some hits? What i wrote not correct?

I need to fetch some information from MySQL database and print it

I am trying to fecth some information from my MySQL database called "opskriftreg" and I want it to print it out, basically it has to retrieve 2x recipes from my database that are already created and list them accordingly with the recipes' "title" first then "description"
Here's my code, if this code is bollocks, i'd appriciate if someone could just show me a better example:
<?php
$mysqli = new mysqli ("localhost","root","","opskriftreg");
// Make a MySQL Connection
if (isset($_POST['title']) && isset($_POST['description']))
{
$username= $_POST['title'];
$password= $_POST['description'];
$query = "SELECT *
FROM opskriftreg
WHERE title = '".mysql_real_escape_string($title)."'
AND description = '".mysql_real_escape_string($description)."'";
$result = mysql_fetch_array(mysql_query($query));
# ...
}
else
{
return NULL;
}
?>
Thank you in advance :-)
EDIT:
Code changed to the following - received blank page where nothing is returned nor printed:
<?php
$mysqli = new mysqli("localhost", "root", "", "opskriftreg");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT title, description FROM opskriftreg ORDER by id DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* fetch object array */
while ($row = $result->fetch_row()) {
printf ("%s (%s)\n", $row[0], $row[1]);
}
/* free result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>
echo $result['column_name'];
And mysql_* functions are deprecated, use mysqli instead.
http://nl3.php.net/mysqli_real_escape_string
Try this, look for the changes I've made.
<?php
$mysqli = new mysqli("localhost", "root", "", "opskriftreg");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT title, description FROM opskriftreg ORDER by id DESC";
$statement = $mysqli->prepare($query);
$statement->execute();
$results = $statement->get_result();
while ($result = $results->fetch_assoc()) {
printf ("%s (%s)\n", $result['title'], $result['description']);
}
/* close connection */
$mysqli->close();
?>

Categories