15、sql语句集,Linux 下PHP查询mysql
mysql>
Database changedmysql> exit
[[email protected] html]# vi root.sql 效果如下:
登录 : [[email protected] root]# mysql -uroot -proot123
);一、mysql 创建数据库
grant all on book.* to [email protected] identified by "lili123" ;mysql> create table book( 如果想更改的话, mysqladmin -uroot password root123 4、创建book表,添加数据 7、查询表
<h1>
<?php
mysql_connect(NULL,"lili","lili123","localhost");
mysql_selectdb("book");
$sql="select * from book";
$res=mysql_query($sql);
while($row=mysql_fetch_row($res)){
print "$row[0] $row[1]<br/>";
}
$a=array("阳光","沙滩","海浪","仙人掌");
for($i=0;$i<4;$i++){
print "$a[$i]<br/>";
}
?>
</h1>
[[email protected] html]# cat > lili.sql 如果想要网格线 -> (1,"语文"), mysql> use book; (先确定要操作的数据库) mysql> grant all on book.* to [email protected] identified by "lili123"; [[email protected] html]# mysql -u lili -p mysql密码,默认没有 5、创建book表
-> );
| 2 | 数学 |
+----+--------+
Records: 3 Duplicates: 0 Warnings: 0
[[email protected] html]# mysql -u root -p < root.sql 3、lili登录 创建lili用户 1、创建book数据库 二、在 /var/www/html 中 创建index.php mysql> create database book; )
+----+--------+
-> name char(20) not null,
-> (3,"英语");
| 3 | 英语 |
在浏览器输入 , 结果如下:
| 1 | 语文 |mysql> select * from book; mysql> insert into book values 退出mysql Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the buffer.
(1,"语文",),
[[email protected] html]# mysql -u lili -p < lili.sql [[email protected] html]# vi index.php
primary key(id)
Query OK, 3 rows affected (0.00 sec)
2、授权给 lili 用户
<html> <title> </title> <body> <h1> <?php mysql_connect(NULL,"lili","lili123","localhost"); mysql_selectdb("book"); $sql="select * from book"; $res=mysql_query($sql); print (‘<table border=1>‘); while($row=mysql_fetch_row($res)){ print (‘<tr>‘); for($i=0;$i<3;$i++){ print "<td>$row[$i]</td>"; } print (‘</tr>‘); } print (‘</table>‘); $a=array("阳光","沙滩","海浪","仙人掌"); for($i=0;$i<4;$i++){ print "$a[$i]<br/>"; } ?> </h1> </body> </html>
6、添加数据
create table book((2,"数学"),
create database book;
+----+--------+ -> primary key(id)
-> id int not null,
Bye insert into book values
(注意:上面操作也可以用sql语句集来执行: (好处是,去别的机子也可以执行sql语句集 就行,不用从头打过)