全部博文(2065)
分类:
2009-04-27 16:29:41
/*1.测试是否建立起连接*/
$hostname = "localhost";
$usr = "root";
$pw = "321";
$link = mysql_connect($hostname,$usr,$pw) or die(mysql_error());
if($link)
{
echo "ok";
}else {
echo "down";
}
mysql_close($link);
/*2.测试连接是否存活 */
$link = mysql_connect("localhost","root","321");
$isok = mysql_ping($link);
if($isok){
echo "the server is ok";
}else{
echo "the server is down";
}
/*3. 建立连接之后执行一个查询SQL语句得到结果集之后显示其中的记录信息*/
$link = mysql_connect("localhost","root","321");
mysql_query("set names gb2312");
mysql_select_db("php");
$sql="select * from test";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo
"id is :".$row[id]." and the name
is:".$row[name]."
";
}
//释放结果
mysql_free_result($result);
/*4. 实现类似JAVA中的方式哦。*/
$link = mysql_connect("localhost","root","321");
mysql_query("set names gb2312");
mysql_select_db("php");
$sql=sprintf("select * from test where name='%s'",mysql_real_escape_string("d"));
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo
"id is :".$row[id]." and the name
is:".$row[name]."
";
}
//释放结果
mysql_free_result($result);
/*5.如果SQL执行之后只有一条记录的话那就没有必要进行WHILE操作了。*/
$link = mysql_connect("localhost","root","321");
if(is_resource($link))
{
mysql_query('SET NAMES gb2312;');
mysql_select_db("php");
$sql = "select * from test where name='d'";
$result = mysql_query($sql);
echo
"id
is:".mysql_result($result,0,"id")."
";
echo
"name
is:".mysql_result($result,0,"name")."
";
}else{
$ping = mysql_ping($link);
if($ping)
{
echo "ping ok";
}else{
echo mysql_error();
}
}
//突然发现PHP中的函数实在是有些多的。所以要总结!
/*6. 执行更新SQL语句。比如DELETE操作计算影响的行数*/
$link = mysql_connect("localhost","root","321");
mysql_query("set names gb2312");
mysql_select_db("php");
$sql = "delete from test where id=1";
mysql_query($sql);
echo "影响的行数:".mysql_affected_rows();
mysql_close($link);
/*7.得到当前数据库中全部可用数据库*/
$link = mysql_connect("localhost","root","321");
$db_list = mysql_list_dbs($link); //获取到一个DB的列表出来的。
$dbNum = mysql_num_rows($db_list); //提取出来数目
while($i<$dbNum)
{
echo mysql_db_name($db_list,$i)."\n";
echo
" \n";
$i++;
}
/*9. 获取字段的长度*/
$link = mysql_connect("localhost","root","321");
mysql_select_db("php");
$sql="select * from test where id=3";
$result=mysql_query($sql);
$length = mysql_field_len($result,1);
echo "字段长度为:".$length;
/*10.获取当前操作表名*/
$link = mysql_connect("localhost","root","321");
mysql_select_db("php");
$sql="select * from test where id=3";
$result=mysql_query($sql);
$table=mysql_field_table($result,1);
echo "The table name is:".$table;
/*11.对数据表中的字段的一些操作*/
$link=mysql_connect("localhost","root","321"); //打开Mysql连接
mysql_select_db("php"); //选择数据库
$result=mysql_query("SELECT * FROM test"); //执行查询
$fields=mysql_num_fields($result); //获取字段数
$table=mysql_field_table($result,0); //获取表名
for($i=0;$i<$fields;$i++) //循环读取记录
{
$type=mysql_field_type($result,$i); //获取字段类型
$name=mysql_field_name($result,$i); //获取字段名
$len=mysql_field_len($result,$i); //获取字段长度
$flags=mysql_field_flags($result,$i); //获取字段标识
echo $type." ".$name." ".$len." ".$flags."\n"; //输出信息
echo " ";
}
mysql_free_result($result); //释放结果集
mysql_close(); //关闭连接