用于从数据库获取数据然后在表中显示的PHP代码

时间:2014-04-17 09:58:12

标签: php mysql sql database

是sql,mysql和php的新手。但是,我正在学习。我有一个收集学生入学记录的数据库。我想要一个PHP脚本从学生档案中提取一些字段并打印在表格中。

下面是数据库结构和条目的示例。

Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `salt` text,
  `userid` int(11) NOT NULL AUTO_INCREMENT,
  `username` text,
  `password` text,
  `email` text,
  `register_date` int(11) DEFAULT NULL,
  `active` tinyint(4) DEFAULT NULL,
  `reg_salt` text,
  `level` int(11) DEFAULT NULL,
  `ip` text,
  `profile` text,
  PRIMARY KEY (`userid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9653 ;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`salt`, `userid`, `username`, `password`, `email`, `register_date`, `active`, `reg_salt`, `level`, `ip`, `profile`)

Within the 'Profile' column are data subsets that the user or admin enters via the website in a XML defined table. An example below

VALUES
('fc61f', 1, 'Admin', 'mutolo', 'smutolo@gmail.com', 1200247562, 1, 'e14973fc61f2bf249c540eb14b8525', 1, 'a:7:{s:14:"82.145.221.205";i:1;s:13:"82.145.221.54";i:1;s:14:"105.166.190.60";i:1;s:15:"197.181.203.204";i:1;s:14:"197.178.16.134";i:11;s:14:"105.166.241.43";i:1;s:15:"197.178.252.241";i:1;}', 'a:21:{s:10:"first_name";s:5:"Nzisa";s:9:"last_name";s:4:"Muli";s:6:"Gender";s:4:"Male";s:6:"County";s:0:"";s:8:"District";s:0:"";s:8:"Location";s:0:"";s:5:"Chief";s:0:"";s:6:"Orphan";s:25:"Yes Both Parents Deceased";s:6:"Parent";s:0:"";s:9:"Emergency";s:0:"";s:7:"Primary";s:0:"";s:4:"KCPE";s:0:"";s:20:"A_Little_Information";s:29:"I am the school administrator";s:9:"Notify_Me";s:1:"1";s:11:"alumni_year";s:4:"2010";s:7:"Picture";N;s:17:"alumni_include_me";s:1:"1";s:6:"Stream";s:0:"";s:9:"Admission";s:0:"";s:5:"House";s:0:"";s:8:"user_pic";s:5:"1.JPG";}'),
('4a1b6', 9641, 'bb', 'c80d5f83dbf874c4caebedfde385e1af', 'smutolo@gmail.com', 1389444756, 0, 'a2becf9eff6dbcde14ef8c1274a1b6', 10, NULL, 'a:21:
<====profile data starts here====>
    {s:10:"first_name";s:6:"Stella";s:9:"last_name";s:4:"King";s:6:"Gender";s:6:"Female";s:6:"County";s:5:"Kitui";s:8:"District";s:8:"Katulani";s:8:"Location";s:8:"Kathungi";s:5:"Chief";s:13:"Mr. Kasasaana";s:6:"Orphan";s:19:"Yes Father Deceased";s:6:"Parent";s:17:"Mr. Mutua Stephen";s:9:"Emergency";s:27:"Mr. Mutua Stephen-074321234";s:7:"Primary";s:7:"Kakuswi";s:4:"KCPE";s:3:"234";s:20:"A_Little_Information";s:39:"Am good in soccer\r\n\r\nAm allergic to oil";s:9:"Notify_Me";s:1:"1";s:11:"alumni_year";s:4:"2010";s:7:"Picture";N;s:17:"alumni_include_me";s:1:"1";s:6:"Stream";s:7:"1 South";s:9:"Admission";s:4:"3423";s:5:"House";s:5:"Nyayo";s:8:"user_pic";s:8:"9641.JPG";}')

我想要一个SQL和PHP代码来提取列Profile中的部分或全部数据。

希望很清楚。非常感谢

1 个答案:

答案 0 :(得分:0)

要从DB中选择所有结果,您可以

SELECT * FROM `users`

这将获取所有内容(所有用户和所有列)。

要获取特定用户,您可以

SELECT * FROM `users` WHERE `userid` = 1

上述查询将获取用户ID为

的用户

以上两个查询都将获取所有列。要获得特定列,您可以

 SELECT `username` FROM `users` 

它会为您提供所有用户名

或者对于特定用户,您可以再次使用Where子句......所以它将是

 SELECT `username` FROM `users` WHERE `userid` = 1