Jump to content

Simple Code Snippet (i think)


timmah1

Recommended Posts

I am doing a website similiar to MySpace, but for a certain group of people.

It has a friends list and bulletins.

 

I can login and see bulletins from people on my "friends" list, but I cannot see my own.

 

This is the code that I am using. What am I doing wrong?

 

Any help would be greatly appreciated.

 

Maybe this might help better.

When I login, my driverID is 5, one of my friends is driverID 6. Both of these driverID have posted a bulletin, but when I echo the driverID, I only receive the 6, when I should be getting the 5 also.

 

<?
	  include "dbConfig.php";	
$server = "xxx";	// server to connect to.
$database = "xxx";	// the name of the database.
$db_user = "xxx";	// mysql username to access the database with.
$db_pass = "xxx";	// mysql password to access the database with.
$table = "bulletins";		// the table that this script will set up and use.

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

$friend = "SELECT * FROM friends WHERE driverID = '5' AND approved = 'y' GROUP BY 'driverID' ";
$result8 = mysql_query($friend);
$numberall = mysql_Numrows($result8);
if ($numberall==0) {
    echo ""; 
}
while($row8 = mysql_fetch_array( $result8 )){

$bulletin = "SELECT * FROM $table ORDER BY bulletinID DESC LIMIT 5";
$result7 = mysql_query($bulletin);
$numberall = mysql_Numrows($result7);

while($row7 = mysql_fetch_array( $result7 )){
if ($row7[driverID]==$row8[friendID]) {
echo "$row7[driverID]";
}
}
}
?>

Link to comment
Share on other sites

Thanks.

 

I altered the code so that it's more legible, and I thought it would function, but I'm still getting the same result.

 

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// Select all friends for this person
$result = mysql_query("SELECT * FROM friends WHERE driverID AND friendID = 5") 
or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
if ($row[approved]=='y') {

//Select all bulletins for this person's friends
$result1 = mysql_query("SELECT * FROM $table") 
or die(mysql_error()); 
while($row1 = mysql_fetch_array( $result1 )) {

print "$row1[driverID]<br>";
}
}
else
{
echo "No Friends";

}
}

Link to comment
Share on other sites


// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error());

// Select all friends for this person
$result = mysql_query("SELECT * FROM friends WHERE driverID = '5' AND friendID = '5'") or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
  if ($row[approved]=='y') {

    //Select all bulletins for this person's friends
    $result1 = mysql_query("SELECT * FROM table") or die(mysql_error()); 
    while($row1 = mysql_fetch_array( $result1 )) {

      print "$row1['driverID']<br>";

    }

  }
   else
  {

    echo "No Friends";
  }

}

Link to comment
Share on other sites

OK, I appreciate the help, I really do, but that's not the issue

 

$table is pre-defined

$server = "localhost";	// server to connect to.
$database = "xxx";	// the name of the database.
$db_user = "xxx";	// mysql username to access the database with.
$db_pass = "xxx";	// mysql password to access the database with.
$table = "bulletins";		// the table that this script will set up and use.

 

I can select the tables, I can pull out the information, what it's not doing is grabbing and showing my id as well as the others that are approved on my list.

 

My driverID is 5, on my list is driverID 6, when I query the bulletins database, it should pull everything from driverID 5 and driverID 6, but it's only showing driverID 6.

 

How do I get it to show my driverID as well?

Link to comment
Share on other sites

My current code

<?
include "dbConfig.php";	
$server = "localhost";	// server to connect to.
$database = "xxx";	// the name of the database.
$db_user = "xxx";	// mysql username to access the database with.
$db_pass = "xxx";	// mysql password to access the database with.
$table = "bulletins";		// the table that this script will set up and use.

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// Select all friends for this person
$result = mysql_query("SELECT * FROM friends WHERE driverID AND friendID = 5") 
or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
if ($row[approved]=='y') {

//Select all bulletins for this person's friends
$result1 = mysql_query("SELECT * FROM $table WHERE driverID = $row[friendID]") 
or die(mysql_error()); 
while($row1 = mysql_fetch_array( $result1 )) {

print "$row1[driverID]<br>";
}
}
else
{
echo "No Friends";

}
}


?>

 

My db structure

friendID 	varchar(255) 	latin1_swedish_ci 	
driverID 	varchar(255) 	latin1_swedish_ci 	
approved 	varchar(10) 	latin1_swedish_ci 

 

Not sure how to do a var dump

Link to comment
Share on other sites

please try this and tell me what you get...

 

 

 


<?
include "dbConfig.php";	
$server = "localhost";	// server to connect to.
$database = "xxx";	// the name of the database.
$db_user = "xxx";	// mysql username to access the database with.
$db_pass = "xxx";	// mysql password to access the database with.
$table = "bulletins";		// the table that this script will set up and use.

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error());

// Select all friends for this person
$result = mysql_query("SELECT * FROM friends WHERE driverID = '5' AND friendID = '5'") or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
  if ($row['approved']=='y') {

    $friend_id = $row['friendID'];
    //Select all bulletins for this person's friends
    $result1 = mysql_query("SELECT * FROM bulletins WHERE driverID = '$friendID'") or die(mysql_error()); 
    while($row1 = mysql_fetch_array( $result1 )) {

      print "$row1[driverID]<br>";

    }
  }
  else
  {
    echo "No Friends";
  }

}

?>

Link to comment
Share on other sites

possibly change this

 

$result = mysql_query("SELECT * FROM friends WHERE driverID = '5' AND friendID = '5'") or die(mysql_error()); 

 

 

to this...

 

$result = mysql_query("SELECT * FROM friends WHERE friendID = '5'") or die(mysql_error()); 

Link to comment
Share on other sites

With this code, it just prints out '5', but not the 6

 

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error());


// Select all friends for this person
$result = mysql_query("SELECT * FROM friends WHERE friendID = '5'") or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
  if ($row['approved']=='y') {

    $friend_id = $row['friendID'];
    //Select all bulletins for this person's friends
    $result1 = mysql_query("SELECT * FROM bulletins WHERE driverID = '$friend_id'") or die(mysql_error()); 
    while($row1 = mysql_fetch_array( $result1 )) {

      print "$row1[driverID]<br>";

    }
  }
  else
  {
    echo "No Friends";
  }

}

?>

Link to comment
Share on other sites

exactly

 

 

you need to define the variable of the users id and then have your table set up so his id can be associated with his friends and vice versa, im not sure if it is set up like that without seeing more info about the db

 

in phpmyadmin, click on the database name. then click export and click go. this will give you the sql to build your db, copy and paste and i will see what i can figure out for you

Link to comment
Share on other sites

When someone logs in, the session is the driverID, which in this case is '5'

So the actual code reads like this:

$result = mysql_query("SELECT * FROM friends WHERE driverID = '$row[driverId]'") or die(mysql_error());

 

Here is my database:

-- 
-- Table structure for table `friends`
-- 

CREATE TABLE `friends` (
  `ID` int(11) NOT NULL auto_increment,
  `friendID` varchar(255) NOT NULL default '',
  `driverID` varchar(255) NOT NULL default '',
  `approved` varchar(10) NOT NULL default '',
  KEY `ID` (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=112 ;

-- 
-- Dumping data for table `friends`
-- 

INSERT INTO `friends` VALUES (88, '6', '5', 'y');
INSERT INTO `friends` VALUES (106, '5', '6', 'y');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.