how to use coalesce mysql [closed] - php

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.

Related

MySQL: Count occurrences of distinct values error [closed]

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

Using PDO and Between [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
Ok, I've got a quick question. I'm using PDO to communicated with a MySQL db. Using BETWEEN with two dates as the variable is easy:
$db->query("SELECT * FROM awesome WHERE date BETWEEN :start AND :end");
What I don't think is easy is when I have one date and two columns. This doesn't work, no matter how loudly I cuss:
$db->query("SELECT * FROM awesome WHERE :one_date BETWEEN start_col AND end_col");
Is there a way to use BETWEEN without reverting to something awful like...?
$db->query("SELECT * FROM awesome WHERE '$one_date' BETWEEN start_col AND end_col");
Or should I just stick to not using BETWEEN in this case?
$db->query("SELECT * FROM awesome WHERE start_col<=:one_date1 AND end_col>=:one_date2")
Thanks!
BETWEEN only works with one column and two values.
But your last approach should work. That's at least the way to go
$db->query("SELECT * FROM awesome" .
" WHERE start_col <= :one_date1 AND end_col >= :one_date2"
);
When using named parameters you should also be able to use the same name twice and only need to bind once.
Update
Well, just tested it and worked for me with " WHERE 'value' between col1 and col2". (tested postgres with datetime, mysql with integer)

PHP MYSQL: Match Date in MYSQL Query to PHP variable that holds a date [closed]

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.

Select all columns but unix_timestamp createdate [closed]

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
Using PHP/MySQL
I need to select all the columns in the table, but convert the column 'createdate', which is saved as a current_timestamp to a unix_timestamp within the query.
You can use MySQL's UNIX_TIMESTAMP() function:
SELECT *, UNIX_TIMESTAMP(createdate) AS unix_createdate FROM my_table
This can be achieved with the UNIX_TIMESTAMP() function built in MySQL.
mysql> SELECT UNIX_TIMESTAMP();
-> 1196440210
mysql> SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
-> 1196440219
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp

Why when i select count with php i get just 1 instead of .. 55 for example || PHP Count, From last 100 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
$nr333 = mysql_query("SELECT COUNT(*) AS cnt FROM (
SELECT * FROM games
WHERE human = '".mysql_real_escape_string($_GET[human])."'
ORDER BY id DESC LIMIT 100
) tmp WHERE changed = 'y'", $link) or die(mysql_error());
$frecventa333 = mysql_num_rows($nr333);
so bassicaly i dont get any error but .. instead of getting the real number i get just 1:|
http://s017.radikal.ru/i414/1310/a2/37958f7cdb48.png
That's because COUNT returns only one row, always. But in that row you'll find field with all rows counted, in one integer.
Try to fetch that row.
And next thing you should do is checking PDO extension. It's better than deprecated mysql_* functions and isn't so hard to learn.

Categories