0

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
  • Do you have any code set up yet, like a database connection? Commented Sep 17, 2010 at 16:23

1 Answer 1

1

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;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your response. the SQL query works fine but the php isn't working for some reason..
Does it give you any error? Did you remember to put correct username, password, tablename, etc in the code ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.