tags:

views:

19

answers:

1

I've done some work with PHP/MySQL in the past but not huge amounts. This should be a fairly simple problem I would have thought.

I have a table called 'user' inside which there are columns called 'id' (primary key), 'name', 'room', 'subject, and 'fb' (facebook profile URL). I need to add values to each of these eg.

id: 1
name: bob
room: B4
subject: maths
fb: www.facebook.com/bob

I then need to search all values in PHP based on a particular room eg.

if (room called B4 exists) {
$name = name;
$room = room;
$subject = subject;
$fb = fb;
echo $name;
}

Sorry if I'm asking for too much guidance, but I'd really appreciate it if someone could clear it up for me somewhat.

Thanks!

+1  A: 

To add values, use mysql_query with INSERT INTO ... like this:

 //connect to mysql and select database
 mysql_connect('localhost', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
 mysql_select_db('put_your_database_name_here') or die("Can not select database");

 //insert data into MySQL
 mysql_query("insert into user (id, name, room, subject, fb) values ('1', 'bob', 'B4', 'maths', 'www.facebook.com/bob')");

Then to search values do like this:

 //connect to mysql and select database
 mysql_connect('localhost', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
 mysql_select_db('put_your_database_name_here') or die("Can not select database");


 //fetch data from MySQL
 $result = mysql_query("select * from user where room = 'B4'");

 //iterate over each row and do what you want.
 while($row = mysql_fetch_assoc($result))
 {
      $name = $row['name'];
      $room = $row['room'];
      $subject = $row['subject'];
      $fb = $row['fb'];
      echo $name;
 }
shamittomar
thanks for your response. the SQL query works fine but the php isn't working for some reason..
Sebastian
Does it give you any error? Did you remember to put correct username, password, tablename, etc in the code ?
shamittomar