Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am trying to use code from the following old question:
MySQL: Count occurrences of distinct values
My query is as follows:
$result = $db->query("SELECT name,COUNT(*) as cnt FROM `table` GROUP BY name ORDER BY cnt DESC");
$row = mysqli_fetch_array($result);
var_dump($row);
In phpmyadmin this code will output all the name and COUNT columns. When I run var_dump($row) it will only have one row for me to work with (the first one, ie the one with the most occurrences), which I can't figure out why. Any ideas? Thanks.
From the documentation on mysqli_fetch_array:
Fetch a result row as an associative, a numeric array, or both
As you found out, it does just that: it fetches a row.
Maybe you were expecting the behaviour of mysqli_fetch_all?
Fetches all result rows as an associative array, a numeric array, or both
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have already searched for possible answers but did not find any who helped me further.
I have two tables. One named users, where the user details are saved like: Name, ID, etc.
Second one is a table where the payments are stored.
Payments:
payment_id
date
amount
user_id
completed
Now I try to get all the payments of a user:
SELECT COUNT(amount) FROM ...
I already have tried this but did'nt gave me the right amount that has been paid.
SELECT
g.name,
g.user_id,
COUNT(m.amount) AS totalAmount
FROM users AS g
LEFT JOIN payments AS m ON g.user_id = m.user_id
GROUP BY g.user_id
Do you just want... SUM(m.amount) instead of COUNT(m.amount) ?
COUNT(m.amount) gives you the number of rows that were aggregated (that means the total number of payments).
SUM(m.amount) is the cumulated value of field m.amount, that is the total amount.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
SOLVED
Rows 1, 4, and 10 have a field with a single quote character in them, so I was SQL injecting myself. More reason to switch to prepared statements!
I have a very simple bulk UPDATE query sent from my PHP form which, oddly enough, only affects certain rows. Rows 1, 4, and 10 doesn't get updated; but all other rows get updated.
Here is the query:
$query = "SELECT * FROM SkillDescriptions";
$result = mysql_query($query);
for ($i=1; $i<=mysql_num_rows($result); $i++){
$skillKey = 'Skill' . (string)$i;
$categoryKey = 'Category' . (string)$i;
$descriptionKey = 'Description' . (string)$i;
$sql="UPDATE SkillDescriptions SET
Skill='$_POST[$skillKey]',
Category='$_POST[$categoryKey]',
Description='$_POST[$descriptionKey]'
WHERE id='$i'
";
$result2=mysql_query($sql);
}
I've got a parallel form that does the exact same PHP form processing as this one but has a different database table, which works properly: so the problem most likely lies in the database table (configuration?) and not the code.
Why do only certain rows get updated, in a seemingly random pattern?
UPDATE
Before:
http://i.stack.imgur.com/2hk2l.png
After: (I appended test to the columns)
http://i.stack.imgur.com/ObMn5.png
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am using Yii, I want to select all records of a model except the last 2 records, but I got an error that there is syntax error, here is my code:
$c = new CDbCriteria();
$c->select = "*";
$c->condition = "idNewsEvents!=(SELECT MAX(idNewsEvents) FROM newsevents) AND idNewsEvents!=(SELECT MAX(idNewsEvents)-1 FROM newsevents))";
$model2 = Newsevents::model()->findAll($c);
This one considers, that idNewsEvents might not be sequential (due to deletes or whatever).
$c = new CDbCriteria();
$c->select = "*";
$c->condition = "idNewsEvents NOT IN (SELECT idNewsEvents FROM newsevents ORDER BY idNewsEvents DESC LIMIT 2)";
$model2 = Newsevents::model()->findAll($c);
P.S.: Not sure if this is correct yii syntax, I'm not familiar with it, but I'm very familiar with MySQL. Anyway, you get the idea I hope.
Two checking can be done in a single checking
$c->condition ="idNewsEvents < (SELECT MAX(idNewsEvents)-1 FROM newsevents)";
Example query
SELECT * FROM `users` WHERE user_id < (SELECT MAX(user_id)-1 FROM users)
it should be:
$c->condition = "idNewsEvents!=(SELECT MAX(idNewsEvents)
FROM newsevents) AND idNewsEvents!=(SELECT MAX(idNewsEvents)-1 FROM newsevents)";
There were just an extra bracket at end and a very tired eyes to notice that :( .
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to match a date from my database to a PHP variable that contains date data that was converted to a string. However my query is not returning any results.
The database field that contains the date data is 'datetime'.
PHP Code:
$todays_date = strtotime("today");
$converted_todays_date = date("m/d/Y", $todays_date);
$dates_sql = "SELECT UNIX_TIMESTAMP(datetime) AS tstamp FROM employee_datetable WHERE STR_TO_DATE(datetime, '%m/%d/%Y') = '$converted_todays_date'";
$result = $usermysqli->query($dates_sql);
// Additional Code that I forgot to add before
while($row = $result->fetch_array()) {
$FormattedPhpDate = date('M d, Y', $row['tstamp']);
echo "<th><div id=day" . $FormattedPhpDate . "</div></th>";
}
Right now there is no date being outputted. I actually put in the wrong part of the code before.
Since you are using DateTime Field you can simply send date in query as date(Y-m-d H:i:s); from PHP to match it exactly. If you only want to compare only date part then use Date() mySql function to perform the comparison.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i am trying to update fields but only if the values hold in the sessions are not null. here is my code.
$query='update trac_patientprofiletable
set name= COALESCE(name,"'.$name.'"),
date_entered= COALESCE(date_entered,"'.$dateEntered.'"),
patientdisease= COALESCE(patientdisease,"'.$_SESSION['input']['sPatientDisease'].'"),
age= COALESCE(age,"'.$_SESSION['input']['sPatientAge'].'"),
weight= COALESCE(weight,"'.$_SESSION['input']['sPatientWeight'].'"),
maritalstatus= COALESCE(maritalstatus,"'.$_SESSION['input']['sPatientMaritalStatus'].'"),
sex= COALESCE(sex,"'.$_SESSION['input']['sPatientSex'].'"),
levelofeduca= COALESCE(levelofeduca,"'.$_SESSION['input']['sPatientEducationLevel'].'"),
diagnosis= COALESCE(diagnosis,"'.$_SESSION['input']['sPatientDiagnosis'].'"),
tbgroup= COALESCE(tbgroup,"'.$_SESSION['input']['sPatientTBgroup'].'"),
cd4count= COALESCE(cd4count,"'.$_SESSION['input']['sPatientCD4Count'].'"),
typeofart= COALESCE(typeofart,"'.$_SESSION['input']['sPatientARTRegimen'].'"),
patientmobileno1= COALESCE(patientmobileno1,"'.$_SESSION['input']['sPatientPhoneNo'].'"),
language= COALESCE(language,"'.$_SESSION['input']['sPatientCallLanguage'].'"),
oiprophylaxis= COALESCE(oiprophylaxis,"'.$_SESSION['input']['sPatientOIProhylaxis'].'"),
eligibleforart= COALESCE(eligibleforart,"'.$_SESSION['input']['sPatientEligibleART'].'"),
dateartinitiated= COALESCE(dateartinitiated,"'.$_SESSION['input']['sPatientARTDate'].'")
where patientid="'.$_SESSION['patientid'].'"';
$result=mysql_query($query) or die ("query error: ".mysql_error());
there is no syntax error .but its not updating the data's in the database. the connection is already established (obviously not shown here). Help please.
Change all your COALESCE parameters to be the other way around:
oiprophylaxis= COALESCE(oiprophylaxis,"'.$_SESSION['input']['sPatientOIProhylaxis'].'")
to
oiprophylaxis= COALESCE("'.$_SESSION['input']['sPatientOIProhylaxis'].'",oiprophylaxis)
COALESCE returns the leftmost non-NULL value, with your current usage it's saying only update oiprophylaxis with the new value if oiprophylaxis is currently NULL.
What you want is the reverse, only update oiprophylaxis with the new value if the new value is not NULL.
Actually this is only part of your problem though, since you're wrapping the values in quotes, and no quoted value is ever going to be NULL. Fortunately that part of your problem will be solved when you take h2ooooooo's advice and use a prepared statement instead.