本文目录一览:
- 1、PHP如何实现MYSQL查询功能
- 2、请问php+mysql查询语句该如何写,查询表字段的总和,按每天且按类型字段
- 3、PHP mysql 查询语句求教
- 4、php mysql数据库查询语句并输出
PHP如何实现MYSQL查询功能
首先搭建一个PHP环境,我用的wamp
然后比如你的数据库位置是本地localhost
数据库用户名是root
数据库密码是123456
数据库名是mydb
数据库里有个表mytab
有3个字段
id(主键) name sno
1 张三 123
2 李四 456
然后在项目根目录,新建一个文件:index.php
?php
//连接数据库
$con=mysqli_connect("localhost","root","123456","mydb");
//SQL语句
$sql="select * from mytab;";
//执行SQL语句,结果保存到$arr
$obj=mysqli_query($con,$sql);
$arr=mysqli_num_rows($result);
?
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
title实现最简单的php网页+mysql查询功能/title
/head
body
?php
echo "pre";
print_r($obj);
?
/body
/html
之后就能够看到结果了
请问php+mysql查询语句该如何写,查询表字段的总和,按每天且按类型字段
(我把你表格中的数据 "中文" 换成了 "英文" 数据和你的是一致的)
我创建你的表格的SQL语句:
create table cellphone
( id int(3) zerofill not null auto_increment primary key,
title char(20) not null,
number int not null,
type char(20) not null,
time date not null
);
插入数据的SQL语句:
insert into cellphone (title,number,type,time) values
("Phone Model 1",90,"cellphone","2012-08-14"),
("Phone Model 1",90,"cellphone","2012-08-14"),
("Phone Model 2",100,"cellphone","2012-08-14"),
("Mobile Accessory 1",100,"Accessory","2012-08-14");
查询语句:
select title, sum(number), time
from cellphone
where type = 'cellphone'
and time= date_format(now(),'%Y-%m-%d') //获取今天日期的函数
group by title;
创建后的表:
查询结果:
php 实现代码:
?php
/************************ MySQL连接 ************************/
$dbc = @ mysqli_connect('localhost', 'root', 'mysql', 'test');
if (mysqli_connect_errno()) {
echo "Error: Could not connect to database.Please try again later.";
exit;
}
/************************************************************/
/********************************** 执行查询 **********************************/
$query = "select title, sum(number), time
from cellphone
where type = 'cellphone'
and time= date_format(now(),'%Y-%m-%d')
group by title";
$results = mysqli_query($dbc, $query);
if ($results) {
echo mysqli_affected_rows($dbc)." rows get.br/";
} else {
echo "An error has occurred.";
}
/***********************************************************************************/
/********************************** 输出结果 **********************************/
while ( list($title, $num, $time) = mysqli_fetch_row($results) ) {
echo "$title $num $timebr/";
}
/******************************************************************************/
/********************************* 关闭和释放 *********************************/
mysqli_free_result($results);
mysqli_close($dbc);
/******************************************************************************/
?
运行结果:
PHP mysql 查询语句求教
?php
/**
*
*@author tanqing
*@date 2015年7月16日
*email itanqing@gmail.com
*/
/*
* $str = file_get_contents('./a.txt');
* $str = preg_replace('/\s/', '#', $str);
* $arr = explode('#', $str);
* print_r($arr);
* $data['riqi'] = $arr[0];
* $data['qihao'] = $arr[1];
*/
header("Content-type: text/html; charset=utf-8");
$link = mysql_connect('127.0.0.1','root','123456');
$database = 'mytest';
$tab_1_sql= 'select * from tab_1';
$tab_1_result = mysql_db_query($database, $tab_1_sql);
while ($tab_1_row = mysql_fetch_assoc($tab_1_result)){
$tab1Result[$tab_1_row['pid']] = $tab_1_row['name'];
}
$query = 'select * from tab_2 ';
$result = mysql_db_query($database, $query);
$row=mysql_fetch_row($result);
while ($row=mysql_fetch_assoc($result))
{
$rowResult[$row['date']][$row['pid']] = $row['val'];
}
?
table
tr
td日期/td
?php foreach ($tab1Result as $pid=$pname){?
td?php echo $pname;?/td
?php }?
/tr
?php foreach ($rowResult as $date=$val){?
tr
td?php echo $date;?/td
?php foreach ($tab1Result as $pid=$pname){?
td?php if ($val[$pid]) {
echo $val[$pid];
}else{
echo 0;
} ?/td
?php }?
/tr
?php }?
/table
打印结果
php mysql数据库查询语句并输出
php中,.是连接符,改成这样
$proname = "SELECT areaname FROM oepre_user_params JOIN oepre_hometown ON oepre_user_params".".`provinceid` = oepre_hometown.`areaid` WHERE userid=123;