php学习过程问题记录
php操作mysql
1、连接
<?php $host = "localhost:3306"; $user = "root"; $pwd = "1234567"; $conn = mysql_connect($host, $user, $pwd); if (!$conn) die("connect error: " . mysql_error()); echo "connect ok!"; mysql_close($conn); ?>
2 、创建数据库
<?php $host = "localhost:3306"; $user = "root"; $pwd = "123456"; $conn = mysql_connect($host, $user, $pwd); if (!$conn) die("connect error: " . mysql_error()); echo "connect ok!<br>"; $sql = "create database test1"; $ret = mysql_query($sql, $conn); if (!$ret) die("create database error: " . mysql_error()); echo "create database ok!"; mysql_close($conn); ?>
3、删除数据库
<?php $host = "localhost:3306"; $user = "root"; $pwd = "123456"; $conn = mysql_connect($host, $user, $pwd); if (!$conn) die("connect error: " . mysql_error()); echo "connect ok!<br>"; $sql = "drop database test1"; $ret = mysql_query($sql, $conn); if (!$ret) die("drop database error: " . mysql_error()); echo "drop database ok!"; mysql_close($conn); ?>
4、选择数据库
<?php $conn = mysql_connect("localhost:3306", "root", "123456"); if (!$conn) die("connect error: " . mysql_error()); echo "connect ok!\n"; $ret = mysql_select_db("test"); if (!$ret) die("select db error: " . mysql_error()); echo "select db ok!\n"; mysql_close($conn); ?>
5、创建表
<?php $conn = mysql_connect("localhost", "root", "123456"); if (!$conn) die("connect error: " . mysql_error()); echo "connect ok!"; $sql = "create table user1(" . "id int(6) not null auto_increment," . "name varchar(10) not null," . "pwd varchar(10) not null," . "primary key(id));"; $ret = mysql_select_db("test"); if (!$ret) die("select db error: " . mysql_error()); $ret = mysql_query($sql, $conn); if (!$ret) die("create table error: " . mysql_error()); echo "create table ok!"; mysql_close($conn); ?>
6、删除表
$sql = "drop table user1;"
7、插入记录
<?php $conn = mysql_connect("localhost", "root", "123456"); if (!$conn) die("connect error: " . mysql_error()); echo "connect ok!"; $name = "zzz"; $pwd = "123"; $sql = "insert into user1". "(name,pwd)". " values". "('$name','$pwd');"; $ret = mysql_select_db("test"); if (!$ret) die("select db error: " . mysql_error()); $ret = mysql_query($sql, $conn); if (!$ret) die("insert table error: " . mysql_error()); echo "insert table ok!"; mysql_close($conn); ?>
8、查询数据
<?php $conn = mysql_connect("localhost", "root", "123456"); if (!$conn) die("connect error: " . mysql_error()); $sql = "select * from user1"; mysql_select_db("test"); $ret = mysql_query($sql, $conn); if (!$ret) die("query data error: " . mysql_error()); while ($row = mysql_fetch_array($ret, MYSQL_ASSOC)) { echo "id={$row['id']}<br>"; echo "name={$row['name']}<br>"; echo "pwd={$row['pwd']}<br>"; echo "*******************<br>"; } echo "fetch data ok!"; mysql_close($conn); ?>
9、更新数据
$sql = "update user1 set pwd='456' where name='zzz';"
10、删除数据
delete from user1 where name='zzz';
delete from user1;
php+jquery+ajax
http://www.css88.com/jqapi-1.9/jQuery.ajax/