PHP面試題及答案(七)

來源:互聯網
上載者:User

一、PHP/MySQL編程
1) 某內容管理系統中,表message有如下欄位
id 文章id
title 文章標題
content 文章內容
category_id 文章分類id
hits 點擊量
建立上表,寫出MySQL語句

2)同樣上述內容管理系統:表comment記錄使用者回複內容,欄位如下
comment_id 回複id
id 文章id,關聯message表中的id
comment_content 回複內容
現通過查詢資料庫需要得到以下格式的文章標題列表,並按照回複數量排序,回複最高的排在最前面

文章id 文章標題 點擊量 回複數量

 

用一個SQL陳述式完成上述查詢,如果文章沒有回複則回複數量顯示為0

3) 上述內容管理系統,表category儲存分類資訊,欄位如下
category_id int(4) not null auto_increment;
categroy_name varchar(40) not null;
使用者輸入文章時,通過選擇下拉式功能表選定文章分類
寫出如何?這個下拉式功能表

drop table if exists Comment;

drop table if exists category;

drop table if exists message;

/**//*==============================================================*/
/**//* Table: Comment                                               */
/**//*==============================================================*/
create table Comment
(
    comment_id                     int unsigned                   not null,
    id                             int unsigned                   not null,
    comment_content                text,
   primary key (comment_id)
)
type = InnoDB;

/**//*==============================================================*/
/**//* Table: category                                              */
/**//*==============================================================*/
create table category
(
    category_id                    int                            not null AUTO_INCREMENT,
    category_name                  varchar(40)                    not null,
   primary key (category_id),
   key AK_pk_category_id (category_id)
)
type = InnoDB;

/**//*==============================================================*/
/**//* Table: message                                               */
/**//*==============================================================*/
create table message
(
    id                             int                            not null,
    title                          varchar(120)                   not null,
    content                        text                           not null,
    category_id                    int unsigned,
    hit                            int unsigned,
   primary key (id)
)
type = InnoDB;

select A.id,A.title,A.hits,IFNULL(B.num,0)
from message A left join (select id,count(*) as num from comment B group by id) B
on A.id=B.id
order by B.num desc;

 

<html>
<head><title>JS列印</title></head>
<body>
<form>
<select id="category" name="category">
<?php
mysql_connect("localhost","root","") or die("db conn error:".mysql_error());
mysql_select_db("phpinterview") or die("db error".mysql_error());
$result=mysql_query("select category_id,category_name from category");
while($row=mysql_fetch_array($result))
{
    echo "<option value='".$row["cateogry_id"]."'>".$row["category_name"]."</option>";
}
?>
</select>
</form>
</body>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.