Jump to content

inserting data with ,


runnerjp

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
}?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

 

Link to comment
Share on other sites

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 />';
}
?>

Link to comment
Share on other sites

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 />';
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.