PHP / MySQL编辑数据库和调用值

时间:2010-09-17 16:20:44

标签: php mysql

我过去曾经使用PHP / MySQL做过一些工作但不是很多。这应该是我原本想到的一个相当简单的问题。

我有一个名为'user'的表,里面有名为'id'(主键),'name','room','subject'和'fb'(facebook profile URL)的列。我需要为这些中的每一个添加值,例如。

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

然后我需要根据特定的房间搜索PHP中的所有值,例如

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

很抱歉,如果我要求太多的指导,但如果有人能在某种程度上为我清理它,我真的很感激。

谢谢!

1 个答案:

答案 0 :(得分:1)

要添加值,请将mysql_queryINSERT INTO ...一起使用,如下所示:

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

然后搜索值是这样的:

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