Global variable in query php/mysql - php

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?

Related

How to put data from database in a select box

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

Does not fetch all products from db

I currently downloaded MAMP on my mac and imported an SQL file containing 589 products with their own product_id.
That worked fine and I can see all the products in "Browse".
I then tested the connection to this db. That worked aswell.
DEFINE('DB_USERNAME', 'root');
DEFINE('DB_PASSWORD', 'root');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_DATABASE', 'testproducts');
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().')'.mysqli_connect_error());
}
echo 'Connected successfully.';
I then tried to fetch all the products to see if that worked by creting a variable $num to see how many got fetched, but then I only get 295 products with this code:
$res = $mysqli->query("SELECT * FROM ac_product");
$num = 0;
foreach ($res as $r) {
$row = mysqli_fetch_array($res);
$pid = $row['product_id'];
echo "</br>". $pid;
$num++;
}
echo "total: ".$num;
$mysqli->close();
Am I missing something from this? Thought this would just fetch all and not have a limit.
Anyone who have some knowledge of this? Thanks

Why won't this php SELECT FROM query work?

Here is my simple php code:
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "********"; //hiding my password
$dbname = "course";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name FROM tutors";
$result = $conn->query($sql);
if( $result === true ) {
echo "good";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
while($row = $result->fetch_assoc()) {
echo $row["name"];
}
?>
</body>
</html>
In my database called "course", I have a table called "tutors" which has a column called "name". I have two entries in that table with the names "deep thought" and "pyrenees" respectively.
When this code runs however, the only thing that prints out is:
Error: SELECT name FROM tutors
It is supposed to simply print out the two names that I mentioned before.
Does anyone know why this happens? I know for a fact that I have the two entries in my table!
I think the word "name" is a MySQL reserved word. Wrap your query variables in a tilde backticks like this:
$sql = "SELECT `name` FROM `tutors`";
This helps to escape those values from MySQL thinking you're trying to referencing a built in variable.
Why not use mysqli like so:
function getFollowers($link, $userid)
{
$sql = "SELECT users.id, username, profileImg FROM following INNER JOIN users ON users.id = following.userid WHERE followid = " . $userid;
$result = mysqli_query($link,$sql);
$resultsArray = [];
while($row = mysqli_fetch_assoc($result)) {
$resultsArray[] = $row;
}
mysqli_free_result($result);
return $resultsArray;
}
This is just a clean example, I am sure you get the idea.
Here is what $link is
function connection()
{
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'databaseTable');
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
return $link;
}
Or without the methods:
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'databaseTable');
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT name FROM tutors";
$result = mysqli_query($link,$sql);
$resultsArray = [];
while($row = mysqli_fetch_assoc($result)) {
echo $row["name"];
}
mysqli_free_result($result);
To check if the query was successful you can do this:
if (mysqli_num_rows($result) > 0)
{
//has rows, so whatever you want with them.
}
You put the condition after defining $result.

populating dropdown list from mysql - php/html

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

MySqli Select is not working

I couldn't figure out why it has some problem like this. The error displayed mysqli_fetch_assoc() expects parameter 1 to be mysqli_result.
My code is like this. I don't know which one is wrong.
Have a look at my con.php.
<?php
$conn = mysqli_connect('localhost', 'username', 'password');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
and this is my page.php
<?php
include 'con.php';
$link = mysqli_connect('localhost', 'username', 'password', 'username') or die("Error " . mysqli_error($link));
$query = " SELECT thread_id, thread_name, thread_date
FROM forum_thread
ORDER BY thread_date";
$result = mysqli_query($link,$query);
while ($row = mysqli_fetch_assoc($result)) {
$thread_id = $row ['thread_id'];
$thread_name = $row['thread_name'];
$thread_date = $row['thread_date'];
echo "$thread_id, $thread_name, $thread_date";
Any ideas? Appreciate any answers from you. Cheers!
I think I figured it out:
Change $result = mysqli_query($link,$query);
to $result = mysqli_query($conn,$query); since $conn is your DB connection, not $link
as per what you posted above
<?php
$conn = mysqli_connect('localhost', 'username', 'password', 'database_name');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
and remove:
$link = mysqli_connect('localhost', 'username', 'password', 'username') or die("Error " . mysqli_error($link));
since you're already loading your DB con with include 'con.php';
Or this way:
<?php
$conn = mysqli_connect('localhost', 'username', 'password', 'database_name') or die("Error " . mysqli_error($conn));
$query = " SELECT thread_id, thread_name, thread_date
FROM forum_thread
ORDER BY thread_date";
$result = mysqli_query($conn,$query);
while ($row = mysqli_fetch_assoc($result)) {
$thread_id = $row ['thread_id'];
$thread_name = $row['thread_name'];
$thread_date = $row['thread_date'];
echo "$thread_id, $thread_name, $thread_date";
The error message indicates the query did fail for some reason. Instead of just using $result test to see if it's valid and if not output the error to the screen or a log file:
if ($result) {
// continue processing $result
}
else {
echo mysqli_error($link);
}
Edit:
The 4th parameter in the mysqli_connect should be the name of the database, "abc" or whatever.
Try to use following code
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli- >connect_error;
}
Here when you try to get connection object itself you can identify the problem if something goes wrong.

Categories