how to get column values randomly in mysql [duplicate] - php

This question already has answers here:
MySQL select 10 random rows from 600K rows fast
(28 answers)
Closed 4 years ago.
i have a table named quiz ,it have six columns like id,question,option1,option2,option3,answer.i want to loop through all the values by using the following query
$sql = "SELECT id, option1, option2,option3,answer,question FROM quiz";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - question: " . $row["question"]. " " .
$row["option1"]. "<br>";
}
How can i get random values from mysql table everytime i want only 4 values.I am new to development .

welcome to Stackoverflow!
Use this;
$sql = "SELECT id, option1, option2,option3,answer,question FROM quiz ORDER BY RAND() LIMIT 4";
This will get rows 100% randomly! But, it can get you duplicates..

Related

MySQL Select statement not functioning if there are alphabetic characters [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 2 years ago.
Expected result:
Loop through all entries in the checkedout table, and select the entry from the game table where the barcode field is the same.
Actual behaviour / issue:
For the most part, this is working as intended. If I set the barcode field to a numerical value in the game table, and then "checkout" that barcode, everything works as intended. The barcodes I'll be using are in the format of ABC12345678. Once I change the values in the barcode field, in the game table to the alphanumeric version, it no longer runs the secondary select statement and displays this error: Fatal error: Call to a member function fetch_assoc() on boolean which refers to the following line: while ($row2 = $result2->fetch_assoc()) {
Oddly enough, if I run the exact same select statement SELECT * FROM game WHERE barcode = 'ABC12345678' on the MySQL instance, it returns the proper results.
Question
Do I need to be using a different method to select based on the value now being alphanumeric? Do I need to manipulate the data in some way?
Code:
$sql = "SELECT * FROM checkedout";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$userid = $row["userid"];
$barcode = $row["barcode"];
echo "$userid </br>";
echo "$barcode </br>";
$sql2 = "SELECT * FROM game WHERE barcode = " . $barcode . "";
$result2 = $conn->query($sql2);
while ($row2 = $result2->fetch_assoc()) {
$title = $row2["title"];
$console = $row2["console"];
echo "$title </br>";
echo "$console </br>";
}
checkedout table:
game table:

how could i solve this code problem in php [duplicate]

This question already has answers here:
SET a variable in SELECT statement - MySQL
(2 answers)
Closed 4 years ago.
i want to rank my users by score from my 'scores' table with php using mysql . i wrote this query that is actually working in SQL Tab of phpMyAdmin :
SET #rank=0;
SELECT #rank:=#rank+1 AS rank, name, score
FROM scores
ORDER BY score DESC;
but i cant figure it out , how to use it in my php Code . i tried:
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["score"]. $E ." ". $row["name"]. $N ;
echo PHP_EOL;
echo PHP_EOL;
}
} else {
echo "0 results";
}
i think my problem is with my while loop . but i dont know what should i do :(
PleaseHelp(); :(
guys problem was solved . Our Dear Friend #mallik1055 noticed that the problem was with query and im sending multiple request . then i noticed That phpMyAdmin automatically runs multiple query unlike PHP , so i tried mysqli_multi_query and it worked , thank you a lot <3
That should work.
$rank = 0;
$rank = $rank+1;
$sql = "SELECT rank, name, score FROM scores ORDER BY score DESC";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row > 0) {
while($row > 0) {
echo $row["score"].$E." ".$row["name"].$N.;
echo PHP_EOL;
echo PHP_EOL;
}
} else {
echo "0 results";
}
$row["rank"] is an element in your fetched rows, just like "score" and "name"

PHP: Last data row won't show in pdo GET query [duplicate]

This question already has answers here:
MySQL skipping first row
(2 answers)
Closed 5 years ago.
Hi Guys I am trying to get data with pdo from my table but the last inserted row never shows up in the populated table. When I add another row of data then get the data again to populate I then see the data from the row that previously was'nt showing but then the very last inserted row does not show.
Here is my code:
<?php
# This program will allow an admin to see email stats for a certain user
$userid = 1; // admin
$email_to = 2 // employee id
$email_by = 2 // employee id
$sql = "SELECT * FROM users WHERE userid = :user_id AND (person = :email_to OR person = :email_by) ORDER BY date DESC";
$stmt = $this->db->prepare($sql);
$stmt->bindparam(":userid", $userid);
$stmt->bindparam(":email_to", $email_to);
$stmt->bindparam(":email_by", $email_by);
$stmt->execute();
if ($stmt->fetch(PDO::FETCH_ASSOC)) {
echo $stmt->rowCount(); // gives me the expected number of rows
while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<tr><td style="text-align:left;">' . $data['email_subject']
. '</td><td>' . $data['email_date'] . '</td><td>'
. $data['email_time'] . '</td></tr>';
}
} else {
echo "Sorry, no record found.";
}
?>
As commented in the code, the rowCount() does give me the expected number of rows e.g. 10, but in the populated table results I only see 9 rows.
Thanks in advance guys.
It's because you unintentionally skip the first row by the if statement:
if ($stmt->fetch(PDO::FETCH_ASSOC)) { ...
where you assign it to nothing. You may use
do {} while ()
loop instead.

"Trying to get property of non-object" sql error [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
Hi I know other questions already have the answer to this but I don't understand. I've done lots of research and am still confused.
I have a table in my Mysql that I made with PHP My Admin it has three rows (I'll add to these) and 3 columns (id, name and price) I'd like to output the first rows name and id but I keep getting the error
Trying to get property of non-object;
<?php
$db = new PDO('mysql:host=localhost;dbname=sitefiles;charset=utf8mb4','user', 'pass');
$sql = 'SELECT id, name FROM 1/1/16';
$results = $db->query($sql);
if ($results->num_rows > 0) {
while($row = $results->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"];
}
}
?>
How can I get rid of this error? Thank you :)
Your table name seems to be unusual. Try to use back-ticks to quote it:
$sql = 'SELECT id, name FROM `1/1/16`';

how to get a single database value using mysqli (php)? [duplicate]

This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 2 years ago.
I am trying to pull a single value from a database and assign it to a php variable. All of the mysqli functions appear to pull an entire row, while I want one value of that row (ex. ID, name, ect).
This is what I have so far:
$result = mysqli_query($con, "SELECT * FROM test_table WHRE ID='" . $_GET['ID'] . "'");
$row = $result->fetch_assoc();
$test= $row['ID'];
echo $test;
When I run the above I don't get any output; $test is unassigned. What is the correct command to assign a value to my $test variable?
You forgot a E in youy WHERE clause
$result = mysqli_query($con, "SELECT * FROM test_table WHERE ID='" . $_GET['ID'] . "'");
$row = $result->fetch_assoc();
$test= $row['ID'];
echo $test;
If your 'ID' field is a integer, quotes are not necessary.

Categories