我过去曾使用 PHP/MySQL 做过一些工作,但数量不多。我认为这应该是一个相当简单的问题。
我有一个名为“user”的表,其中有名为“id”(主键)、“name”、“room”、“subject”和“fb”(Facebook 个人资料 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;
}
抱歉,如果我要求太多指导,但如果有人能为我解决一些问题,我将非常感激。
谢谢!
最佳答案
要添加值,请使用 mysql_query
和 INSERT 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;
}
关于PHP/MySQL 编辑数据库和调用值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3737108/