I was hoping someone could give me a hand on how I write this correctly.
$details_quey = mysql_query("SELECT * FROM `accounts` WHERE ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true' AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ", $CON )
Any help much appreciated.
SELECT * FROM `accounts`
WHERE
(ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true')
AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token'
Guessing a bit, but I think that is what you want.
Assuming you want anything matching any one of the ORs and all of the ANDs
AND has precedence over OR in a SQL Query. Therefore, your query valuates as such right now:
SELECT * FROM `accounts` WHERE
ONLINE = 'true' OR
FORCE_AWAY = 'true' OR
( AWAY = 'true' AND
FORCE_OFFLINE = 'false' AND
TOKEN = '$con_token' );
Simply use ( ) to group conditions which should be evaluated together:
SELECT * FROM `accounts` WHERE
(ONLINE = 'true' OR
FORCE_AWAY = 'true') OR
(AWAY = 'true' AND
FORCE_OFFLINE = 'false') AND
TOKEN = '$con_token' );
"SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true') AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ",
Just use some braces to keep the and or logic seperate. Check if the person is online or force away or away AND that the forceoffline is false and token value = con token.
Not sure what you're looking for, but brackets sure will help for the OR's:
$details_quey = mysql_query("SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true') AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ", $CON )
It's hard to tell what exactly you're trying to accomplish, but assume you want:
(online = true OR force_away=true OR away=true)
AND
force_offline=false AND token=$con_token
Your ORs need to be grouped together in parenthesis or the ANDs won't make much sense.
SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true' AND FORCE_OFFLINE = 'false') AND TOKEN = '$con_token
Related
I am trying to get the following SQl working:
SELECT * FROM tc_appointment as tcAppointment, tc_message as tcMessage
WHERE tcAppointment.counsellor_id = 502
AND
(
(tcAppointment.tc_message_id = tcMessage.id AND tcMessage.marked_read = false AND tcMessage.sender_id != 502)
OR
(tcAppointment.cancelled = true AND tcAppointment.cancelled_acknowledged = false)
OR
(tcAppointment.confirmed = false)
);
The tcAppointment.tc_message_id is null in the only tc_appointment entry in the table. I am trying to get it to return as the counsellor_id = 502 and the second OR statment is true
I seem to be getting stuck because of the tc_message as tcMessage clause and the first AND / OR statement but I'm not sure why. Can anyone point me in the right direction please?
Try Joining the 2 tables and use the 'Where':
SELECT * FROM tc_appointment as tcAppointment
LEFT JOIN tc_message as tcMessage
ON
((tcAppointment.tc_message_id = tcMessage.id AND tcMessage.marked_read = false AND tcMessage.sender_id != 502)
OR
(tcAppointment.cancelled = true AND tcAppointment.cancelled_acknowledged = false)
OR
(tcAppointment.confirmed = false)
)
WHERE tcAppointment.counsellor_id = 502
I want the actual field updated to change depending on a condition. Existing examples I have come across only allow the value of the update to be changed, not the field. This is what I have tried:
UPDATE conversations
CASE WHEN conv_author_id = $uid THEN SET conv_viewed_author = $d
ELSE SET conv_viewed_recipient = $d END
WHERE conv_id = $id
However this gives me a syntax error.
How do I set the actual field updated to be conditional?
This should do the trick:
UPDATE conversations
SET
conv_viewed_author = IF(conv_author_id = $uid, $d, conv_viewed_author),
conv_viewed_recipient = IF(conv_author_id = $uid, conv_viewed_recipient, $d)
WHERE
conv_id = $id;
You can always do something like this:
UPDATE conversations
SET
conv_viewed_author = (
CASE conv_author_id
WHEN $uid THEN $d
ELSE conv_viewed_author
END
),
conv_viewed_recipient = (
CASE conv_author_id
WHEN $uid THEN conv_viewed_recipient
ELSE $d
END
)
WHERE conv_id = $id
I'm trying to add a default image in a query, if the file does not exist.
I succeeded when the query doesn't have a while loop. But since i need to loop this values. i would like to add the default image to the query.
I wont get any errors, its just printing out some random info from my DB.
function fetch_tweets($uid){
$uid = (int)$uid;
$query = $this->link->query
("SELECT user.id, user.email, user.username, tweets.message, tweets.date,
userdetails.profile_img, userdetails.firstname, userdetails.lastname,
following.id, following.user_id, following.follow_id
FROM user
LEFT JOIN
following ON user.id = following.user_id
JOIN userdetails ON user.id = userdetails.user_id
JOIN tweets ON userdetails.user_id = tweets.user_id
WHERE user.id='{$uid}'
OR
user.id IN (SELECT follow_id
FROM following
WHERE
following.user_id = '{$uid}' )
GROUP BY tweets.date ORDER BY tweets.date DESC "
);
$tweet = array();
while(($row = $query->fetch(PDO::FETCH_ASSOC)) !==FALSE) {
$tweet[] = $row;
$tweet['profile_img'] = (file_exists("img/{$uid}.jpg")) ?
"img/{$uid}.jpg" : "img/default.jpg" ;
}
return $tweet;
}
You're using the variable $tweet in the wrong way in your loop.
The following line will add a new element to the array $tweet:
$tweet[] = $row;
And the following line updates the element called "profile_img" in your $tweet array:
$tweet['profile_img'] = "...";
But I think, what you want is something like this:
while(($row = $query->fetch(PDO::FETCH_ASSOC)) !== FALSE) {
// Update the value for profile_img in the row
$row['profile_img'] = file_exists("img/{$uid}.jpg") ? "img/{$uid}.jpg" : "img/default.jpg" ;
// Add the manipulated row to the $tweet-array
$tweet[] = $row;
}
Please test using a debugger like xdebug (maybe a bit complicated to set up if you're not familiar with PHP) or just use var_dump();. You would've found that out pretty soon ...
Hi how can I make this query work. I want a condition on where clause, that if #BACHNUMB = '',
then WHERE is (h.sopnumbe = #SOPNUMBE) Else WHERE is (h.bachnumb = #BACHNUMB). Thanks in advance.
WHERE
CASE(#BACHNUMB)
WHEN '' THEN (h.sopnumbe = #SOPNUMBE)
ELSE
(h.bachnumb = #BACHNUMB)
END
Simply recreate the logic through different syntax:
WHERE
(#BACHNUMB = '' AND h.sopnumbe = #SOPNUMBE)
OR
(#BACHNUMB != '' AND h.bachnumb = #BACHNUMB)
END
(#BACHNUMB = '' and h.sopnumbe = #SOPNUMBE) or (#BACHNUMB != ' and 'h.bachnumb = #BACHNUMB)
busy for several hours now, to find out what i am doing wrong.
In an update sql value becomes empty??
if(ctype_digit($_POST['id'])
AND
$_SESSION['captain'] == 1
AND
$_SESSION['team'] == $_POST['id'])
{
$teamtekst = $_POST['value'];
// update text
$q = "UPDATE teams
SET
teamtekst = '".mysql_real_escape_string($teamtekst)."'
WHERE
team_id = '".$_POST['id']."'
LIMIT 1";
$exec = mysql_query($q);
if(mysql_affected_rows($exec) == 1)
echo'ok';
else
echo $q.' '.$_POST['value'];
}
else
{
echo 'Fout in gegevens? Tekst niet opgeslagen!'; // faultmessage
}
It echo's: UPDATE teams SET teamtekst = '' WHERE team_id = '29' LIMIT 1 test
so test is the real value of $_POST['value'] but does not show up in sql statement?
thanks in advance for any help!
its not an sql error , $_POST['value'] is not set
use var_dump($_POST) to know what's inside the $_POST array and check if the $_POST['value'] is already set before