Jump to content

Recommended Posts

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?

 

Here is my dababase structure

-- 
-- 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');

 

And here is my code

$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";

}
}

Link to comment
https://forums.phpfreaks.com/topic/50132-help-selecting-variables-from-database/
Share on other sites

First of all there should be no $ variable sign in

 

<?php
//Select all bulletins for this person's friends
$result1 = mysql_query("SELECT * FROM $table WHERE driverID = $row[friendID]") 
?>

 

should be

 

<?php
//Select all bulletins for this person's friends
$result1 = mysql_query("SELECT * FROM  table WHERE driverID = '$row[friendID]'") 
?>

 

I think there's a problem with this line:

 

$result = mysql_query("SELECT * FROM friends WHERE driverID AND friendID = 5")

 

I think this query is only looking for rows where the friendID is 5, and ignoring the driverID altogether, which is why you're just getting the one row returned - either that or it would return rows where both the driverID *and* the friendID are 5, which would return zero rows.

 

What you might need is something along these lines:

 

$driverID = 5;
$result = mysql_query("SELECT * FROM friends WHERE driverID = '$driverID' OR friendID = '$driverID' ")

 

That should return all rows where either the driverID is 5 or the friendID is 5.

That was it londonjustin!!!

 

Thank you very much.

 

And Trium918, $table is a pre-defined variable, as for this

$table = "bulletins";		// the table that this script will set up and use.

 

I have no database called 'table', so this wouldn't work

$result1 = mysql_query("SELECT * FROM table WHERE driverID = $row[friendID]") 

 

But since the variable is already passed, this is what works

$result1 = mysql_query("SELECT * FROM $table WHERE driverID = $row[friendID]") 

 

Thank you very much for the input, everything is working great now

That works, but now I have this problem

 

I want to limit the query to 5, when i do this

$result1 = mysql_query("SELECT * FROM bulletins WHERE driverID = '$friend_id' ORDER BY 'bulletinID' LIMIT 5") or die(mysql_error()); 

 

It shows 6 results instead of 5 results. Also, I'm trying to view them by bulletinID from newest to oldest, and it don't do it.

 

Did I put this in wrong?

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.