Sakshi Sharma
Sakshi Sharma

Reputation: 1472

getting names in array for notification system

hello i m trying to get names in array from the database for my notification system. such that i have taken 3 conditions . and in 2nd and 3rd i require names in array , can anyone help me out with the code . also in the $result query i m not able to avoid results of same user ie i suppose john commented on my status twice then only one result should be selected , i used distinct for that but no success .

so basically there are two porblems

  1. getting more then one name from database using array
  2. avoiding same user name result in selecting username for the array .

can anyone help , its a big module stuck very bad. thanks

<?php
include_once("includes/connection.php");
$result = mysql_query("SELECT distinct * from notification inner join  users_profile on notification.owner_user_id=users_profile.uid where notification.user_id=3 and notification.user_id!=notification.owner_user_id order by notification.time_stamp Desc");
while ($row=mysql_fetch_array($result)) {
    $nfname[]=$row["fname"];
    $npicid[]=$row["profile_pic"];
    $sql2 =mysql_query( "SELECT COUNT(type_id) FROM notification where type_id='wall_comments' and user_id='3' and is_seen='0'"); 
    while($row2=mysql_fetch_array($sql2)) {
        $req_pending=$row2['COUNT(type_id)'];
        $req_pending1=$row2['COUNT(type_id)']-3;
        if ($req_pending==1) {
            $result="$fname shared view on your Status Update";
        }
        elseif ($req_pending==3) {
            $result="$nfname shared views on your Status Update";
        }
        else  {
            $result="$nfname  and $req_pending1 shared their views on your Status Update";
        }
    }
}
?>

Upvotes: 0

Views: 124

Answers (2)

Sakshi Sharma
Sakshi Sharma

Reputation: 1472

hello thanks for helping guys . i solved my question @sadmircowave thanks for suggesting or die("query failed:" . mysql_error() ); . and the thing i did is i changed $result to $sql1 and while ($row=mysql_fetch_array($result)) to ($row=mysql_fetch_array($sql1)) . i think taking same name might have caused the problem , and the array

Upvotes: 0

sadmicrowave
sadmicrowave

Reputation: 40952

I'll respond to your questions in order:

Question 1: "is thr any way by which i can only get one name out of two of two records of same name"

Yes. If you have multiple records in your data table with the same name then you can use the distinct sql command to select only 1 of those records. However, when selecting * you run the risk of other fields in your records being unique and therefore they will be selected also. Here is an example:

Data in my table:

Username | Created #<--field names

JohnDoe | 2012-01-12 10:03:42

JohnDoe | 2012-04-12 14:24:55

Method 1

"SELECT DISTINCT * FROM MYTABLE" 

will result in both records being retrieved since the created date is unique for each record.

Method 2 #<--correct method

"SELECT DISTINCT Username FROM MYTABLE" 

will result in only 1 instance of "JohnDoe" being returned.

Question 2: i am getting Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\wamp\www\mihubs\test.php on line 5 error when using the array

This is most likely caused by your mysql_query() resulting in a failure. After your query add or die("query failed:" . mysql_error() ) like so:

 $myresults = mysql_query("SELECT DISTINCT Username FROM MYTABLE") 
              or die("query failed:" . mysql_error() );

This will ensure that your code does not continue when an error has been encountered. Rather it will bark the error out to the screen and you can see where your problem is.

Alternatively, if you don't want errors to print to the screen and stop your site functionality you can drop the or die() and wrap your proceeding while statement (or whatever you have after your select that uses your results in if( $myresults ){. This also ensure that the block will not be executed if you mysql_query() encounters an error.

Upvotes: 1

Related Questions