Display random usernames from mysql database in php echo [duplicate] - php

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I wanna ask how to display database usernames from mysql in php echo
$send = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
while ($show = mysql_fetch_array($send)) {
echo ''.$show['usernames'];
}

This fixes the php error:
$ts = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
while ($show = $ts->fetch(PDO::FETCH_ASSOC)) {
echo ''.$show['usernames'];
}
For sake of completeness you expect only 1 row of results so while-loop could be redundant:
$ts = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
$show = $ts->fetch(PDO::FETCH_ASSOC);
echo $show['usernames'];

$sql = "SELECT * FROM 'usernames` ORDER BY RAND() LIMIT 1' ";
$result = $conn->query($sql);
while($record = mysqli_fetch_array($result)) {
foreach ($record as $d){
echo $d;
}
}

Try this
$query = "SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1"
$send = mysql_query($query);
while ($show = mysql_fetch_array($send)) {
echo ''.$show['usernames'];
}

Related

Create a single comma separated list to echo from mysqli results [duplicate]

This question already has answers here:
How to transform array to comma separated words string? [duplicate]
(4 answers)
Closed 10 months ago.
$get_top10 = mysqli_query($con, "SELECT name, daily_points, daily_win FROM players WHERE daily_points > 0 ORDER BY daily_points DESC LIMIT 10");
I am needing a string like:
name, daily_points, daily_win, name, daily_points, daily_win (etc up to 10 records)
php 7.4.27
mysql 5.6.51-cll-lve
This did the trick, let me know if there is a better or preferred method.
$sql = ("SELECT name, daily_points, daily_win FROM players WHERE daily_points > 0 ORDER BY daily_points DESC LIMIT 10");
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "{$row['name']},{$row['daily_points']},{$row['daily_win']},";
}
}
you can try this code it will concatenate your php variable to html ','
and it's a preferred method
$sql = ("SELECT name, daily_points, daily_win FROM players WHERE daily_points > 0 ORDER BY daily_points DESC LIMIT 10");
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['name'].','.$row['daily_points'].','.$row['daily_win'];
}
}

MySQL randomly selected questions

I can't find what I need. I trying to create at quizzer that takes questions from the database. Currently, it takes all the questions in the database and what I need it to take it randomly 10 questions. I am just starting to learn how to code.
Here are my getting questions code with answers:
<?php
//Set question number
$number = (int) $_GET['n'];
/*
* Get total questions
*/
$query = "SELECT * FROM `klausimai`";
//Get result
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $results->num_rows;
/*
* Get Question
*/
$query = "SELECT * FROM `klausimai`
WHERE question_number = $number";
//Get result
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$question = $result->fetch_assoc();
/*
* Get Choices
*/
$query = "SELECT * FROM `atsakymai`
WHERE question_number = $number";
//Get results
$choices = $mysqli->query($query) or die($mysqli->error.__LINE__);
?>
As mentioned above, limit the klausimai query results to 10 by using the SQL LIMIT statement ($query = "SELECT * FROM klausimai LIMIT 10";):
<?php
//Set question number
$number = (int) $_GET['n'];
/*
* Get total questions
*/
$query = "SELECT * FROM `klausimai` LIMIT 10";
//Get result
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $results->num_rows;
/*
* Get Question
*/
$query = "SELECT * FROM `klausimai`
WHERE question_number = $number";
//Get result
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$question = $result->fetch_assoc();
/*
* Get Choices
*/
$query = "SELECT * FROM `atsakymai`
WHERE question_number = $number";
//Get results
$choices = $mysqli->query($query) or die($mysqli->error.__LINE__);
?>
Your Code good to go, but to print 10 random question fetched directly from your mysql database, Execute the following query :
$query = SELECT * FROM `klausimai`
ORDER BY RAND()
LIMIT 10;
while(row=mysql_fetch_array($query) {
echo $row['question_feild'];
//Replace 'question_feild' it with your question_body column of your table
//assuming you have 2 choices for a particular question
echo $row['choice1']; // Same replace it with your choice/answer columns
echo $row['choice2']; // Same replace it with your choice/answer columns
}
Try this :
/*
* Get Questions
*/
$query = "SELECT * FROM `klausimai` ORDER BY RAND() LIMIT 10";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($result)){
echo "Question number " . $row["question_number"] . "<br/>";
echo "Choices of question are:<br/>";
/*
* Get Choices
*/
$query = "SELECT * FROM `atsakymai` WHERE `question_number` = " . $row["question_number"];
$result = mysqli_query($conn, $query);
$i = 1;
while($row2 = mysqli_fetch_assoc($result)){
echo $i.") ".$row2["choice_text"]."<br/>";
$i++;
}
}

Echo the 2 last columns of tabel

I have a table and i want to echo the 2 last rows of my tabel, I used the below code but just the last one showed, what is the problem.
$result1 =(mysql_fetch_array(mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")));
Print $result1['time'];
mysql_fetch_array = 1 fetch.
do it again for fetching 2nd result.
Also, use mysqli eh.
You're doing mysql_fetch_array only one time, so it gets the first element. If you want to get all the elements, then do it again, or use a loop.
Something like:
$query = "SELECT * FROM $table ORDER BY id DESC LIMIT 2";
while($row = mysql_fetch_array(mysql_query($query) )
{
echo $row['time'].'<br>';
}
For 2 Or more rows you need to loop it
$sql = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")
while($row=mysql_fetch_array($sql))
{
echo $row['time']."<br>";
}
$query = mysqli_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2");
while ($result = mysqli_fetch_array($query)) {
echo $result['time'];
}
Gives out every return of your database (2 in this case). You should use mysqli_-functions.
Maybe you should try like this, since mysql_fetch_array returns only one row
while ($row = mysql_fetch_array($yourQuery)) {
echo $row["yourAlias"];
}
Further details here : http://fr2.php.net/manual/en/function.mysql-fetch-array.php
My solution:
$limit = 2;
$sql = "SELECT * FROM $table ORDER BY id DESC LIMIT $limit";
$result = mysql_query($sql);
$array = array(); $i = 0;
while($row = mysqli_fetch_array($result))
{
$array[$i] = $row;
$i++;
}
var_dump($array[0]);
var_dump($array[1]);

display the mysql count function? [duplicate]

This question already has answers here:
SELECT COUNT(*) AS count - How to use this count
(5 answers)
Closed 1 year ago.
$query = "select count(*)
from relationships
where leader = 'user_id'";
$result = mysql_query($query);
how can i display the count? thanks
$count = mysql_fetch_array($result);
echo $count[0];
$query = "SELECT COUNT(*) AS total FROM table";
$result = mysql_query($query);
$values = mysql_fetch_assoc($result);
$num_rows = $values['total'];
echo $num_rows;
use $row = mysql_fetch_array($result) and access it by index 0: $row[0]
use an alias in your query ("select count(*) as cnt from ...") and $row = mysql_fetch_assoc($result) and access it by name: $row['cnt']
$abc="SELECT count(*) as c FROM output WHERE question1=4";
$result=mysqli_query($conn,$abc);
if($result)
{
while($row=mysqli_fetch_assoc($result))
{
echo $row['c'];
}
}
Its work completely in this it count the number of occurrences of 4 in the column

MySQL query within another query's while loop in PHP

I have the following code:
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($result = mysql_fetch_array($query)) {
extract($result);
if ($activity_type == "discussion") {
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($result = mysql_fetch_array($query)) {
extract($result);
echo $discussion_user . " said:<br>" . $discussion_text . "<br>";
}
} elseif ($activity_type == "file") {
}
}
But it just returns the last row. My goal is to have a chronological list of "activities" each displayed slightly differently depending on their type.
Your using $query and $result twice so the second loop is overwriting the result of the first and stopping...
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
and
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
same with $results var...
I would suggest you change to $query and $query2 but best to use something like
$activies = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($activity = mysql_fetch_array($activies)) {
and
$discussions = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($discussion = mysql_fetch_array($discussions)) {
I would also avoid using extract - as you might be overwriting vars your not expecting to...
You have to create another connection to the database so that you can run them at the same time.
OR
You can load the results of the first one into an array, and then just loop through the array.

Categories