runnerjp Posted June 2, 2008 Share Posted June 2, 2008 hey guys... i was wondering... im making chnages to my friends list and how could i add the users into my db like this name,name,name,name then call like $query = "SELECT * FROM `friends` WHERE `username`= '$username' ORDER BY username ASC;"; and it comes out with name name name name Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 2, 2008 Share Posted June 2, 2008 Sorry? Are you trying to insert data or select data? Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 2, 2008 Author Share Posted June 2, 2008 sorry i need to do both lol mainly insert data you see i currently do this $query = "INSERT INTO friend_requests (username ,by_user) VALUES('{$_GET[user]}', '$get_username_value')"; so my bb looks like this username by_user 2 1 3 1 4 1 but sould it not be better if i stored it liek this username friends 1 2,3,4,5,6 2 1,3,4,5,6 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 2, 2008 Share Posted June 2, 2008 but sould it not be better if i stored it liek this No. Most definitely not. Your first way is correct. Try reading up on database normalization. See this topic to get started. Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 2, 2008 Author Share Posted June 2, 2008 ah ok in that case..... why does this only echo 1 of my users and not then all :S <?php $query = "SELECT * FROM `friends` WHERE `username`= '$username' ORDER BY username ASC;"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)){ $friends= $row['friendname']; } $sql = "SELECT image FROM users WHERE username = '$friends'"; $result = mysql_query($sql) or die(mysql_error().'<br />Query was:'.$sql); while($row = mysql_fetch_assoc($result)){ $image = $row["image"]; echo $friends; }?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 2, 2008 Share Posted June 2, 2008 Because you close the while loop before you execute the second query. The result: only the last friend gets queried. In any case, you dont want to do it like that. Use one query. If you post up your database structure (firends and users table as well as a bit of sample data) ill show you how. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 2, 2008 Share Posted June 2, 2008 DELETED. Oops. Never mind. I missed something and Ginger hit it. Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 2, 2008 Author Share Posted June 2, 2008 oh yes that would be great thanks CREATE TABLE `friends` ( `id` int(10) NOT NULL auto_increment, `friendname` varchar(225) NOT NULL default '', `username` varchar(225) NOT NULL default '', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ; and CREATE TABLE `users` ( `ID` int(11) NOT NULL auto_increment, `Username` varchar(255) NOT NULL default '', `Password` varchar(255) NOT NULL default '', `date_registered` int(11) NOT NULL default '0', `Temp_pass` varchar(55) default NULL, `Temp_pass_active` tinyint(1) NOT NULL default '0', `Email` varchar(255) NOT NULL default '', `Active` int(11) NOT NULL default '0', `Level_access` int(11) NOT NULL default '2', `Random_key` varchar(32) default NULL, `about_me` text NOT NULL, `events` varchar(255) NOT NULL default '', `first_name` varchar(255) NOT NULL default '', `last_name` varchar(20) NOT NULL default '', `gender` varchar(6) NOT NULL default '', `dob` varchar(99) NOT NULL default '', `ip` varchar(29) NOT NULL default '', `lastlog` varchar(99) NOT NULL default '', `image` varchar(255) NOT NULL default '', `new_user` int(1) NOT NULL default '0', `club` varchar(40) NOT NULL default '', PRIMARY KEY (`ID`), UNIQUE KEY `Username` (`Username`), UNIQUE KEY `Email` (`Email`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 and obviusly smaple data is above Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 2, 2008 Share Posted June 2, 2008 Well if you were wanting to select all of your friends with images (which i think is what you wanted?), you can do: <?php $sql = "SELECT friendname,image FROM friends,users WHERE friends.username='runnerjp' and friends.friendname=users.Username ORDER BY friendname"; $result = mysql_query($sql) or die(mysql_error().'<br />Query was:'.$sql); while(list($friendname,$image) = mysql_fetch_row($sql)){ echo $friendname.'<br />'.$image.'<br />'; } ?> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 2, 2008 Author Share Posted June 2, 2008 ok so split up each query by , and the . does what... link username with the friendsname?? Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 2, 2008 Author Share Posted June 2, 2008 also i tried what you did and got Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in while(list($friendname,$image) = mysql_fetch_row($sql)){ Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted June 2, 2008 Share Posted June 2, 2008 should be: <?php $sql = "SELECT friendname,image FROM friends,users WHERE friends.username='runnerjp' and friends.friendname=users.Username ORDER BY friendname"; $result = mysql_query($sql) or die(mysql_error().'<br />Query was:'.$sql); while(list($friendname,$image) = mysql_fetch_row($result)){ echo $friendname.'<br />'.$image.'<br />'; } ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 2, 2008 Share Posted June 2, 2008 Yes, sorry, it was supposed to be $result. As for the . - that joins table and field names. For example, friends.friendname means the friendname field in the friends table. Whilst not strictly necessary in this query (since friendname doesn't appear in the other table), I find it best to always do it this way since it makes the query clearer. Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 2, 2008 Author Share Posted June 2, 2008 ahh ok that makes sence ty Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 2, 2008 Author Share Posted June 2, 2008 oh can i ask anouther question... so how would i display my users like this friend friend friend friend friend friend friend friend this one has puzzeld me for sum time lol as i can only get it to go up + down or left and right lol Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 2, 2008 Share Posted June 2, 2008 See this post Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 2, 2008 Author Share Posted June 2, 2008 ok i have gone for <?php $columns = 4; //change the query to get another field from the database $query = "SELECT friendname,image FROM friends,users WHERE friends.username='$username' and friends.friendname=users.Username ORDER BY friendname"; $result = mysql_query($sql) or die(mysql_error().'<br />Query was:'.$sql); $num_rows = mysql_num_rows($result); $rows = ceil($num_rows / $columns); while(list($friendname,$image) = mysql_fetch_row($result)){ $data[] = $row['friendname']; //store the other field into an array $data2[] = $row['image']; } echo "<TABLE BORDER=\"0\">\n"; for($i = 0; $i; < $rows; $i++) { echo "<TR>\n"; for($j = 0; $j < $columns; $j++) { if(isset($data[$i + ($j * $rows)])) { echo "<TD>" . $data[$i + ($j * $rows)] . "</TD>\n"; //echo out the field echo "<TD>" . $data2[$i + ($j * $rows)] . "</TD>\n"; } } echo "</TR>\n"; } echo "</TABLE>\n"; ?> but here for($j = 0; $j < $columns; $j++) { i get unexpected '&' expeting ) :S there is no & lolalso am i going about this right? Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 2, 2008 Author Share Posted June 2, 2008 bmp Quote Link to comment Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 Durrrr the error is right in your face man. for($i = 0; $i; < $rows; $i++) { Read http://php.net/manual/en/control-structures.for.php Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 2, 2008 Author Share Posted June 2, 2008 i cant see any error tho...iv looked at link and didnt rlly help too much :S Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 2, 2008 Share Posted June 2, 2008 Why are all your < and > showing up as > and < ?? for($i = 0; $i < $rows; $i++) { See the difference? Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 3, 2008 Author Share Posted June 3, 2008 humm wunder how that happend :S ok strange thing is now i get no data echod... am i doing it correct :S <?php $columns = 4; //change the query to get another field from the database $sql = "SELECT friendname,image FROM friends,users WHERE friends.username='$username' and friends.friendname=users.Username ORDER BY friendname"; $result = mysql_query($sql) or die(mysql_error().'<br />Query was:'.$sql); $num_rows = mysql_num_rows($result); $rows = ceil($num_rows / $columns); while(list($friendname,$image) = mysql_fetch_row($result)){ $data[] = $row['friendname']; //store the other field into an array $data2[] = $row['image']; } echo "<TABLE BORDER=\"0\">\n"; for($i = 0; $i < $rows; $i++) { echo "<TR>\n"; for($j = 0; $j > $columns; $j++) { if(isset($data[$i + ($j * $rows)])) { echo "<TD&>" . $data[$i + ($j * $rows)] . "</TD>\n"; //echo out the field echo "<TD>" . $data2[$i + ($j * $rows)] . "</TD>\n"; } } echo "</TR>\n"; } echo "</TABLE>\n"; ?> Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 3, 2008 Share Posted June 3, 2008 So you see absolutely nothing? No text at all? A completely blank page? Check the source. You do have an HTML error in your code. Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 3, 2008 Author Share Posted June 3, 2008 ok i corrected my & in td lol but still does not work... now i added border 1 so i could see it and i see a small dot but i suppose if no data is been inserted it wnt show table rpoperly <?php $columns = 4; //change the query to get another field from the database $sql = "SELECT friendname,image FROM friends,users WHERE friends.username='$username' and friends.friendname=users.Username ORDER BY friendname"; $result = mysql_query($sql) or die(mysql_error().'<br />Query was:'.$sql); $num_rows = mysql_num_rows($result); $rows = ceil($num_rows / $columns); while(list($friendname,$image) = mysql_fetch_row($result)){ $data[] = $row['friendname']; //store the other field into an array $data2[] = $row['image']; } echo "<TABLE BORDER=\"1\">\n"; for($i = 0; $i < $rows; $i++) { echo "<TR>\n"; for($j = 0; $j > $columns; $j++) { if(isset($data[$i + ($j * $rows)])) { echo "<TD>" . $data[$i + ($j * $rows)] . "</TD>\n"; //echo out the field echo "<TD>" . $data2[$i + ($j * $rows)] . "</TD>\n"; } } echo "</TR>\n"; } echo "</TABLE>\n"; ?> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted June 3, 2008 Author Share Posted June 3, 2008 bmp Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.