>  기사  >  백엔드 개발  >  PHP와 mysql은 데이터 테이블을 생성하고 프런트 엔드 페이지에 렌더링할 콘텐츠를 얻습니다.

PHP와 mysql은 데이터 테이블을 생성하고 프런트 엔드 페이지에 렌더링할 콘텐츠를 얻습니다.

小云云
小云云원래의
2018-03-13 10:45:513791검색

이 기사에서는 주로 php와 mysql을 사용하여 데이터 테이블을 만들고 콘텐츠를 프런트엔드 페이지에 렌더링하는 방법을 공유합니다. 도움이 되기를 바랍니다.

1.Mysql데이터 테이블(테이블 구조)을 생성하는 명령줄:


소스 코드:

create table myValues(

 id int(4) not null primary key auto_increment,

 name char(20) not null,

 year int(20) not null,

 sex char(20) not null,

 class char(20) not null,

 degree double(16,2)

);

2. 테이블 구조 보기:


3.php

Connectmysqlidatabase

$connect = mysqli_connect('localhost','root','root'); //连接数据库

if($connect){
     echo '连接数据库成功!';
}else{
     echo '连接数据库失败';
}

//连接数据表
mysqli_select_db($connect,'lgc');


4.php

데이터베이스에 데이터 삽입

 //向数据库发送操作信息

 mysqli_query($connect,'set names utf8');

 mysqli_query($connect,'insert into myvalues values(1,"王二",20,"男","13.1班","98.68")');  

 mysqli_query($connect,'insert into myvalues values(2,"李三",22,"男","12.5班","88.66")');  

 mysqli_query($connect,'insert into myvalues values(3,"小花",16,"女","13.1班","78.32")');  

 mysqli_query($connect,'insert into myvalues values(4,"小明",15,"男","12.6班","69.95")');  

 mysqli_query($connect,'insert into myvalues values(5,"小刚",18,"男","13.2班","83.75")');  

 mysqli_query($connect,'insert into myvalues values(6,"小贝",23,"女","12.3班","93.63")');


5. php

데이터베이스에 쿼리 정보 보내기

//像数据库提交查询信息(所有信息) 返回查询到资源句柄

$info = mysqli_query($connect,'select * from myvalues');


6.php

데이터베이스에서 쿼리된 데이터 가져오기: (중요)

$newArr = array();
while($arry = mysqli_fetch_array($info,MYSQLI_ASSOC))
{
     array_push($newArr,$arry);
}

exit(json_encode($newArr));

//获取数据:
//Array ( [0] => Array ( [id] => 1 [name] => 王二 [year] => 20 [sex] => 男 [class] => 13.1班 [degree] => 98.68 ) [1] => Array ( [id] => 2 [name] => 李三 [year] => 22 [sex] => 男 [class] => 12.5班 [degree] => 88.66 ) [2] => Array ( [id] => 3 [name] => 小花 [year] => 16 [sex] => 女 [class] => 13.1班 [degree] => 78.32 ) [3] => Array ( [id] => 4 [name] => 小明 [year] => 15 [sex] => 男 [class] => 12.6班 [degree] => 69.95 ) [4] => Array ( [id] => 5 [name] => 小刚 [year] => 18 [sex] => 男 [class] => 13.2班 [degree] => 83.75 ) [5] => Array ( [id] => 6 [name] => 小贝 [year] => 23 [sex] => 女 [class] => 12.3班 [degree] => 93.63 ) )

//json_encode()之后:
[{"id":"1","name":"\u738b\u4e8c","year":"20","sex":"\u7537","class":"13.1\u73ed","degree":"98.68"},{"id":"2","name":"\u674e\u4e09","year":"22","sex":"\u7537","class":"12.5\u73ed","degree":"88.66"},{"id":"3","name":"\u5c0f\u82b1","year":"16","sex":"\u5973","class":"13.1\u73ed","degree":"78.32"},{"id":"4","name":"\u5c0f\u660e","year":"15","sex":"\u7537","class":"12.6\u73ed","degree":"69.95"},{"id":"5","name":"\u5c0f\u521a","year":"18","sex":"\u7537","class":"13.2\u73ed","degree":"83.75"},{"id":"6","name":"\u5c0f\u8d1d","year":"23","sex":"\u5973","class":"12.3\u73ed","degree":"93.63"}]

7.

로드 data :


8.
페이지에 mysql데이터 테이블 정보가 표시됩니다.

관련 추천 :

React는 어떻게 백엔드에서 데이터를 가져와 프론트엔드로 렌더링하나요?

위 내용은 PHP와 mysql은 데이터 테이블을 생성하고 프런트 엔드 페이지에 렌더링할 콘텐츠를 얻습니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.