OK, I know this is a simple thing but I am *VERY* new at MySQL, my second day at it in fact. I'm going to buy a book tomorrow but in the mean time, I am hoping someone could answer a simple question.
This is what I want to do; if the name "Ash" is present in the "recipient" column, then I want it to return true, else return false.
Here is what my Messages table may look like:
MESSAGES
+------+--------+-------------+----------------+
|??ID??|??FROM??|??RECIPIENT??|??MESSAGE???????|
+------+--------+-------------+----------------+
|???1??|??Jim???|??Lisa???????|??I?love?you????|
+------+--------+-------------+----------------+
|???2??|??Lisa??|??Jim????????|??F.?Off!???????|
+------+--------+-------------+----------------+
|???3??|??Jim???|??Lisa???????|??Whatever!?????|
+------+--------+-------------+----------------+
|???4??|??Will??|??Ash????????|??How?are?you???|
+------+--------+-------------+----------------+
|???5??|??Ash???|??Andy???????|??Hi????????????|
+------+--------+-------------+----------------+
|???6??|??Andy??|??Ash????????|??Hello?Ash?????|
+------+--------+-------------+----------------+
|???7??|??Donna?|??Ash????????|??Word?Up?Dog???|
+------+--------+-------------+----------------+
As you can see from the example above, "Ash" has received 3 messages and "Donna" has received none. I want to use this as part of a Private messaging system I am trying to build on a test site. The idea is that members can leave messages for other members, and when someone logs in, i.e. "Ash", he sees that there is a message waiting for him.
I can use the following code to get a list of everything in the database, but I need a test to see if "Ash" has a message waiting for him.
Question
AshMan
OK, I know this is a simple thing but I am *VERY* new at MySQL, my second day at it in fact. I'm going to buy a book tomorrow but in the mean time, I am hoping someone could answer a simple question.
This is what I want to do; if the name "Ash" is present in the "recipient" column, then I want it to return true, else return false.
Here is what my Messages table may look like:
MESSAGES
+------+--------+-------------+----------------+
|??ID??|??FROM??|??RECIPIENT??|??MESSAGE???????|
+------+--------+-------------+----------------+
|???1??|??Jim???|??Lisa???????|??I?love?you????|
+------+--------+-------------+----------------+
|???2??|??Lisa??|??Jim????????|??F.?Off!???????|
+------+--------+-------------+----------------+
|???3??|??Jim???|??Lisa???????|??Whatever!?????|
+------+--------+-------------+----------------+
|???4??|??Will??|??Ash????????|??How?are?you???|
+------+--------+-------------+----------------+
|???5??|??Ash???|??Andy???????|??Hi????????????|
+------+--------+-------------+----------------+
|???6??|??Andy??|??Ash????????|??Hello?Ash?????|
+------+--------+-------------+----------------+
|???7??|??Donna?|??Ash????????|??Word?Up?Dog???|
+------+--------+-------------+----------------+
As you can see from the example above, "Ash" has received 3 messages and "Donna" has received none. I want to use this as part of a Private messaging system I am trying to build on a test site. The idea is that members can leave messages for other members, and when someone logs in, i.e. "Ash", he sees that there is a message waiting for him.
I can use the following code to get a list of everything in the database, but I need a test to see if "Ash" has a message waiting for him.
<?php $connection = mysql_connect ("localhost", "root", "whatever"); if ($connection == false){ ?echo mysql_errno().": ".mysql_error()."<BR>"; ?exit; } $query = "select * from message"; $result = mysql_db_query ("messenger", $query); if ($result){ ?echo "<TABLE Border=1>"; ?echo "<TR><TD><B>Sender</B></TD><TD><B>Recipient</B></TD><TD><B>Message</B></TD></TR>"; ?$numOfRows = mysql_num_rows ($result); ?for ($i = 0; $i < $numOfRows; $i++){ ? ?$sender = mysql_result ($result, $i, "sender"); ? ?$recipient = mysql_result ($result, $i, "recipient"); ? ?$message = mysql_result ($result, $i, "message"); ? ?echo "<TR><TD>$sender</TD><TD>$recipient</TD><TD>$message</TD></TR>"; ?} ?echo "</TABLE>"; } else{ ?echo mysql_errno().": ".mysql_error()."<BR>"; } mysql_close (); ?>Thanks.
Link to comment
Share on other sites
7 answers to this question
Recommended Posts