php操作mysql示例源码

<?php
	header("Content-type:text/html; charset=utf-8");
	echo '<a href="#">刷新</a><br />';
//	echo "<br />mysql.connect_timeout=".get_cfg_var('mysql.connect_timeout');
//数据库连接-查询-取结果-关闭
	//连接,返回连接资源
	$mysql=mysql_connect('localhost:3306', 'fuxingdaima', 'phf936300')
		or die('Could not connect '.mysql_error);
//	echo "mysql_get_client_info()= ".mysql_get_client_info()."<br />";
//	echo "mysql_get_host_info()= ".mysql_get_host_info($mysql)."<br />";
//	echo "mysql_get_server_info()=".mysql_get_server_info($mysql)."<br />";
/*	$state = mysql_stat($mysql);
	echo "mysql_stat()=";
	var_dump($state);
	echo "<br />";
*/
	//选择数据库
	mysql_select_db("test") or die("Could not select database test");
	//查询
	$query = "SELECT * FROM sc";
	$result = mysql_query($query) or die("Invalid query".mysql_error());
	//var_dump($result);
//	echo "mysql_info()=".mysql_info($mysql)."<br />"; 
	//取结果
/*	//结果行列数
	$rowNum = mysql_num_rows($result);
	echo "num rows=".$rowNum."<br />";
	$fieldNum = mysql_num_fields($result);
	echo "num field=".$fieldNum."<br />";
*/
	//取出行数据
	$resArray = mysql_fetch_array($result, MYSQL_ASSOC);
	echo "mysql_fetch_array( , MYSQL_ASSOC)=";
	var_dump($resArray);
	echo "<br />";
	$resArray = mysql_fetch_array($result, MYSQL_NUM);
	echo "mysql_fetch_array(, MYSQL_NUM)=";
	var_dump($resArray);
	echo "<br />";
	$resArray = mysql_fetch_array($result, MYSQL_BOTH);
	echo "mysql_fetch_array(, MYSQL_BOTH)=";
	var_dump($resArray);
	echo "<br />";
	$lenArray = mysql_fetch_lengths($result);
	echo "mysql_fetch_lengths=";
	var_dump($lenArray);
	echo "<br />";
	$resArray = mysql_fetch_object($result);
	echo "mysql_fetch_object=";
	var_dump($resArray);
	echo "<br />";
	echo '$resArray->Sid='.$resArray->Sid."<br />";
/**/	

	
/*	//取出列字段信息
	$field = mysql_fetch_field($result);
	echo "mysql_fetch_field()=";
	var_dump($field);
	echo "<br />";
	$field_flags = mysql_field_flags($result, 0);
	echo "mysql_field_flags()=";
	var_dump($field_flags);
	echo "<br />";
*/
	//释放结果标识符$result 所关联的内存(可不需要)
	mysql_free_result($result);
	//关闭连接资源(可不需要)
	mysql_close($mysql);


你可能感兴趣的:(PHP,mysql)