This question already has answers here:
Mysql select count values from a single column
(4 answers)
Closed 2 years ago.
I have been trying this code for some time now, and I cant figure it out.
<?php
$link = mysqli_connect("localhost", "h4g54453g5234", "23j4hbjh243v535", "3j45hv6j3h45v645");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = 'SELECT COUNT(*) FROM users WHERE phone=9876543210 AND status=1';
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo "" . $row['COUNT(*)'] . "";
}
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
table sample
The result of above code will be "4", because the column "status" only 4 data with the number "1".
now I can use echo $row['COUNT(*)']; to display the number "4".
But, I want to show results like following
echo "$status1; (which will show "4")
echo "$status2; (which will show "4")
echo "$status3; (which will show "2")
How can I do that?
Using the above code, I can only show the result of status column with value 1.
SELECT status, phone, COUNT(*) AS count FROM people GROUP BY phone, status
SELECT status, phone, COUNT(*) AS count FROM people WHERE status=3 GROUP BY phone, status
SELECT status, phone, COUNT(*) AS count FROM people WHERE phone='9876543210' GROUP BY phone, status
Related
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..
If I put the name of one table it's work, but if I type * it does not find anything.
I trying to search at all the tables that exist in the specific db,
Basically it is for a search box on the site
i need help please
<?php
$link = mysqli_connect("localhost", "***", "***", "***");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if(isset($_REQUEST["term"])){
$sql = "SELECT * FROM * WHERE food LIKE ?";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "s", $param_term);
$param_term = $_REQUEST["term"] . '%';
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<p><a style='color:red' href='http://hidden.com'>" . $row['date'] . "</a></p>";
}
} else{
echo "<p>No matches found</p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
mysqli_stmt_close($stmt);
}
mysqli_close($link);
?>
You must assigne table name esplicitally the use of * ( select all) is not allowed for table name .. you can use just one name or you can use JOIN for join several tables but in your case, assuming each table have a column named food , you could if you need a query that involve mores table you could use union
SELECT *
FROM table1
WHERE food LIKE ?
UNION
SELECT *
FROM table2
WHERE food LIKE ?
UNION
SELECT *
FROM table3
WHERE food LIKE ?
......
UNION
SELECT *
FROM tablen
WHERE food LIKE ?
";
You can use UNION for distinct result or UNION ALL for get all the result
SQL doesn't support wildcarding on tables.
You can run "SHOW TABLES" first which will give you a list of tables in one query result. Then you can iterate through those and run your query on each table individually.
If you need to do it in one shot, you'll need to create a stored procedure which does the same thing, but would all be run on the server-side
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"
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 4 years ago.
The below query works fine whenever I try it in phpmyadmin:
SELECT CAST(DATETIME AS DATE), Provider, COUNT(Provider)
FROM purchases
GROUP BY CAST(DATETIME AS DATE), Provider
ORDER BY CAST(DATETIME AS DATE) DESC
Nevertheless I always received a boolean when I placed it inside my PHP script:
$sql="SELECT CAST(DATETIME AS DATE), Provider, COUNT(Provider)
FROM purchases
GROUP BY CAST(DATETIME AS DATE), Provider
ORDER BY CAST(DATETIME AS DATE) DESC";
if (mysqli_query($con,$sql))
{
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo $row['CAST(DATETIME AS DATE)'];
echo $row['Provider'];
echo $row['COUNT(Provider)'];
}
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
mysqli_close($con)
;
I really would appreciate some advice about how to tackle this issue.
this code write like below so its work fine
you use mysqli in all query and check if $result has value than print record otherwise error message was print
$result = mysqli_query($con,$sql);
if($result)
{
while($row = mysqli_fetch_assoc($result)){
echo $row['CAST(DATETIME AS DATE)'];
echo $row['Provider'];
echo $row['COUNT(Provider)'];
}
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
mysqli_close($con)
So, I have 14-15 columns (Username, Password, location ) .... and every column has his own user data's.
I want to make a search, echoing those data's in the descending way
Exemple: Column AGE.
John has age 10
Jahon has age 14
Loku has age 20.
I want to echo echo "age" by the highest number
How can this be achieved?
I want to make a top 10 age list, and I want to make them descreasing
List will look like:
Loku: ?php echo $Loku Age?>
Jahon: ?php echo $Jahons Age?>
John: ?php echo $Johns age?>.
but when database updates, the values from list will change too, even usernames.
How? thank you.
Select (username, age) FROM table ORDER BY age DESC LIMIT 10
This way, you select two columns from your table, which is the name of a user and their age.
The result should be 10 users with the highest age.
Try the following data fetch process to retrieve existing data from the database:
Create a connection to MySQL (use your username, password and database name for that, again, read a tutorial)
$conn = new mysqli("localhost", "root", "", "");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Select the data:
$sql = (SELECT (username, age) FROM table ORDER BY age DESC LIMIT 10);
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo $row['username'] . " - " . $row['age'] . "<br>";
}