PHP inbox System - php

I have a problem with creating an inbox System.
What i'm trying to do is in "ViewMessages.php" I am trying to pull information from a MYSQL Table to show messages.
My First Statement is:
$MessageQuery = mysql_query("SELECT * FROM messages WHERE ToUName='$ToUName' AND FromUName='$FromUName'");
But I realised a flaw, it will only show messages sent 1 way. I Tried something like:
$MessageQuery = mysql_query("SELECT * FROM messages WHERE ToUName='$ToUName' AND FromUName='$FromUName' OR FromUName='$ToUName' AND ToUName='$FromUName'");
This failed. Can anyone shed some light to show both messages from both parties?

How about union?
$MessageQuery = mysql_query("(SELECT * FROM messages WHERE ToUName='$ToUName' AND FromUName='$FromUName') UNION (SELECT * FROM messages WHERE FromUName='$ToUName' AND ToUName='$FromUName')");
Note: If you need messages in any particular order, you can use ORDER BY at the end of the query, hoping you have something like message_id or timestamp attached to each.

SELECT *
FROM messages
WHERE '$ToUName' in (ToUName, FromUName)
OR '$FromUName' in (ToUName, FromUName)
or if you prefer columns listed first in your query
SELECT *
FROM messages
WHERE ToUName in ('$ToUName', '$FromUName')
OR FromUName in ('$ToUName', '$FromUName')

You have boolean operators mixed up, try put some () into that.. (a AND b) OR (c AND d).
Also what you'd acomplish is getting all messages between you and the one other contact. Are you aware of that?

Try This:
$MessageQuery = mysql_query("SELECT * FROM messages WHERE ToUName='$ToUName' || FromUName='$FromUName'");

Related

Moodle no value in table but the query result count returns 1

I am using Moodle 2.9.1. I have a query to fetch the last record from the table:
My query as follows:
$qstndetails = $DB->get_record_sql('SELECT * FROM {epoll_questions} WHERE status=? AND courseid=? ORDER BY id DESC LIMIT 0,1',array(2,$curseId));
I am taking the count as
echo count($qstndetails);
I am getting the count as 1 in the case of result is there.
But in the case of result is not there also I am getting count as 1.
I had tried print_r($qstndetails) but nothing showing. But the count show as 1.
The expecting count is 0 when there is no result.
Why I am getting like this?
When developing, always have debugging switched on. This would probably have displayed an error message for the first code. Go to site admin -> development -> debugging, then debug messages = developer and switch on display debug messages.
Also LIMIT isn't an SQL standard. Moodle works with several databases so try to keep the SQL generic.
Also I would suggest using named parameters rather than ?. It makes the code easier to read but they can also be used in any order.
and finally, you can use IGNORE_MULTIPLE to get the first record.
So the code should be something like this:
$sql = "SELECT *
FROM {epoll_questions}
WHERE status = :status
AND courseid = :courseid
ORDER BY id DESC";
$params = array('status' => 2, 'courseid' => $curseid);
$qstndetails = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE);
Need to change the query to
$qstndetails = $DB->get_records_sql('SELECT * FROM {epoll_questions} WHERE status=? AND courseid=? ORDER BY id DESC LIMIT 0,1',array(2,$curseId));
Now working!!

How to use LIKE operator against multiple values

I have this query :
select * from users where mailaddress
NOT like '%banned_domain1.com%'
AND mailaddress NOT like '%banned_domain2.com%'
AND mailaddress NOT like '%banned_domain3.com%' ;
I want to make it more simple , I executed this query from command line :
select * from users where mailaddress
NOT like ('%banned_domain1.com%','%banned_domain2.com%','%banned_domain3.com%') ;
I got MySQL error :
ERROR 1241 (21000): Operand should contain 1 column(s)
You can use NOT REGEXP
SELECT * FROM users WHERE mailaddress NOT REGEXP 'banned_domain1.com|banned_domain2.com|banned_domain3.com';
See live demo
Instead of "Like" use "In" and format the email address like this:
select * from users where SUBSTR(mailaddress, INSTR(mailaddress, '#') + 1)
NOT IN ('banned_domain1.com','banned_domain2.com','banned_domain3.com');
The SUBSTR will remove the # and anything preceding it, leaving only the domain name then you can do a perfect comparison without wildcards using IN.
Cheers!
you have to mention the column every time
select * from tasks where title NOT LIKE '%eating lunch%' AND title NOT LIKE '%eating breakfast%' AND title NOT LIKE '%a new task%'
however as Bruno Domingues said use NOT IN that will be more easy
You cannot simplify your query. You need one LIKE per condition.

Multiple MYSQL Statements executed as a single statement

I've got a bit of a problem with my code. I'm sure that it is something simple, but I just can't figure it out! I have been on tons of forums and have read several books... but every answer that I have worked to has failed. I almost guarantee that it's the way that I am using my syntax (and yes I know... procedural PHP is not really used anymore) but I am really a bit of a newbie to this and I am just trying to pick up the basics before moving onto OOP and PDO connections.
Could you please help me? At the moment I can get the user to select their date from the date picker and the results specifically from that date only will return... only problem is that the event is displaying the event_id as opposed to the name of the event that it relates to (1 = 5km run) for example.
Somehow I need to access the events table and pull the row that relates to that specific event_id.
I have normalized my database, and according to my tutor it looks ok. To give you an idea what it looks like - logins table (all user logins details), results table (a history of submitted events) events table (the events themselves).
On the results table the foreign keys are logins_id and the event_id. The primary key is the results_id in the results table and the only data stored here is the time and data (individual columns).
<?php // -----Stage 1. On submission of the form run the following -----//
if (isset($_POST['submit_d'])) {
$mydate = $_POST ['MyDate'];
$my = preg_replace('/[^a-zA-Z0-9]+/', ' ', $mydate);
if ($mydate) {
$result = mysql_query("SELECT * FROM logins WHERE username = '$username' LIMIT 1");
//This function will take the above query and create an array...
while($row = mysql_fetch_array($result))
{
//With the array created above, I can create variables (left) with the outputted array (right)
$logins_id3 = $row['logins_id'];
}
$sql = "SELECT * FROM results where $logins_id3 AND date = $mydate ";
/* ----- Here is the code that I want to use in conjunction with the above statement --->
$query = "SELECT logins.username,events.event,results.time,results.date,logins.age,logins.gender
FROM logins INNER JOIN results ON logins.logins_id=results.logins_id INNER JOIN events ON results.event_id=events.event_id
ORDER BY time ASC LIMIT 10";
*/
$resultz = mysql_query($sql);
if( mysql_num_rows($resultz) ) {
while ($row = mysql_fetch_array($resultz)) {
echo "<table><tr><th>Username</th><th>Event</th><th>Time (HH:MM:SS)</th><th>Date (YY/MM/DD)</th><th>Age</th><th>Gender</th>
</tr><tr><td>".$username."</td>"
."<td>".$row['event_id']."</td>"."<td>".$row['time']."</td>"." <td>".$row['date']."</td>"."<td>".$row['age']."</td>".
"<td>" $row['gender']."</td></tr></table>";
}
}
}
}
?>
The other thing I would like to do.. although this is not crucial, is to strip special characters from the input. Basically I'm using a jquery calendar picker and I want the user to be able to select their date in the 2014-05-26 format and the php to remove the - before it is submitted to the database, that way it doesn't effect the users experience but it will work with my current code.
Anyways sorry to waffle on, any help on either of these matters would be much appreciated!
Yours Sincerely:
Peter Scales.
You can use a join to get the data that relates to the event ID:
SELECT * FROM results r LEFT JOIN events e ON r.event_id = e.event_id WHERE ...
You can then select where "e.event_id = $event_id"; and the rest of your query logic.
You can also filter out any unwanted characters by using preg_replace: http://ar2.php.net/preg_replace

PHP MySQL While loop for SELECT from two tables?

Hi there i am working on PHP code that is selecting columns from two tables.
Here is my code:
$result2 = mysql_query("SELECT *
FROM `videos`, `m_subedvids`
WHERE `videos.approved`='yes' AND
`videos.user_id`='$subedFOR'
ORDER BY `videos.indexer`
DESC LIMIT $newVID");
while($row2 = mysql_fetch_array($result2))
{
$indexer = addslashes($row2['videos.indexer']);
$title_seo = addslashes($row2['videos.title_seo']);
$video_id = addslashes($row2['videos.video_id']);
$title = addslashes($row2['videos.title']);
$number_of_views = addslashes($row2['videos.number_of_views']);
$video_length = addslashes($row2['videos.video_length']);
}
When i try to print $indexer with echo $indexer; it's not giving me any results.
Where is my mistake in this code?
It seems to me like the key 'indexer' isn't in your results. It's hard to tell, since you haven't listed a definition for your table and you're using SELECT * so we can't see the names.
It makes the program easier to read later, if instead of SELECT *..., you use SELECT col1, col2, .... Yes, SELECT * will save you some typing right now, but you'll lose that time later when you or anyone else who works on your code has to check the table definition every time they work with that line of code.
So, try changing your query to explicitly select the columns you use. If it's an invalid column you'll get an error right away rather than this silent failure you're getting now, and you'll thank yourself later as well.
So long as videos.indexer is a unique field name among all tables used in the query you can change
$indexer = addslashes($row2['videos.indexer']);
to
$indexer = addslashes($row2['indexer']);
You don't need to (or can not) use the table name when referring to the result.

MySQL query to get user where email equals some variable

First of all i don't know where to start doing this. That's why i am posting this question. Maybe you guys go for down-vote or close or flag it.
Here is the problem.
<?php
$gmail = "dipesh#some.com"
$split = explode('#',$service['User']['email']);
?>
The code above outputs an array with two elements inside it: one for dipesh and other for some.com. That is correct as expected.
Now I want a MySQL query that will perform select query to get record for dipesh#.
Example
SELECT * FROM users WHERE users.email LIKE 'dipesh';
The code above returns all the records that contain dipesh, but I want only the ones that contain dipesh#.
Does SELECT * FROM users WHERE users.email LIKE '%dipesh'; work?
If it does then what is the difference between the two?
Thanks
Your PHP code seems unrelated to your problem, but it sounds like you want email = 'dipesh#', which matches dipesh# literally. If you want dipesh# followed by anything, use email LIKE 'dipesh#%'. The % is a wildcard meaning "match zero or more of any character."
try this
SELECT * FROM users WHERE users.email LIKE 'dipesh#';
and if you want to dipesh# followed by anything else
use this
SELECT * FROM users WHERE users.email LIKE 'dipesh#%';
You probably want to try this
"SELECT * FROM users WHERE users.email LIKE 'dipesh#%'";
Hope this helps
SELECT * FROM users WHERE email LIKE 'dipesh#%';

Categories