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)
Related
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
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..
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:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
im having trouble with a seemingly correct query for mysql database. The Query in question is:
"SELECT * FROM Users WHERE Email =".$email.";". The Query itself is
executing fine but the $result that is returned back is false (if i
replace "Email =".$email."" with "Id = 1" it works and returns a
value).
if($emailCheck = TRUE){
echo "<script type='text/javascript'>alert('Email check true.');</script>";
$sql = "SELECT * FROM Users WHERE Email =".$email.";";
echo $sql;
$result = $conn->query($sql);
if ($result){
$row = mysqli_fetch_array($result) ;
echo "<script type='text/javascript'>alert('".(string)$row['FirstName']."');</script>";
} else { echo "<script type='text/javascript'>alert('bad result');</script>";}
}
Some info:
$emailCheck = TRUE is working fine.
When using "Id = 1" instead of "Email =".$email."" everything works
echo $sql; returns "SELECT * FROM Users WHERE Email =zxzx#hotmail.com;"
any help why $result is returned false when using "Email =".$email.""?
This is because if you are using id then it is integer so no need to put it in quotes('') But if you use email then it is string so you need to write it in quotes('') as follow
$sql = "SELECT * FROM Users WHERE Email ='" . $email . "'";
This question already has answers here:
why this mysql query is not working?
(7 answers)
Closed 8 years ago.
Please help me regarding the specified problem:
The code section:
$result = mysql_query("SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,
UNIX_TIMESTAMP(throughdate) AS throughdate FROM events where
id='$_GET[id]' ORDER BY eventdate");
// the above query is not working
if (! $result) {
echo mysql_errno() . ": " . mysql_error(). "\n";
}
if ( mysql_num_rows($result) == 0 ) {
print "<p>No events right now.</p>\n";
}
else {
$lasteventmonth = '';
while ($row = mysql_fetch_array($result)) {
$eventmonth="";
$eventmonth = date("F Y",$row['eventdate']);
if ($lasteventmonth != $eventmonth) {
print "<p style='font-size: 18px;'><b>$eventmonth</b></p>";
}
$lasteventmonth = $eventmonth;
showEvent($row);
}
}
?>
........................
........................//other codes
when the code evaluates as follows:
No events right now.
But specific id is present in the database and if $_GET['id'] is echoed in the page the value is shown.
what is id in id='$_GET[id]' at the beginning?
If you have a query http:// ... ?id=123, I would put id in quotes. Having said that, better like this:
$id = mysql_real_escape_string($_GET['id']); // safe against SQL injection
$sql = "SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate, UNIX_TIMESTAMP(throughdate) AS throughdate FROM events where id='$id' ORDER BY eventdate";
$result = mysql_query($sql);
If you are still getting trouble, use echo to check the variables $id and $result before the query runs; then you will have a clearer idea why it is not running the query you expect.
I am sure id=$_GET[id] is checking an int versus an int where you have it checking an int vs a string. Remove the single quotes around $_GET['id'] and try again. The single quotes define it as a string rather than an int.