- 论坛徽章:
- 0
|
- <?php
-
class Mysql{
-
private $HOST = "localhost"; // 数据库地址
-
private $USER = "root"; // 数据库帐号
-
private $PASS = "lizhihua"; // 数据库密码
-
private $DBNAME = "db_iqoomea"; // 数据库库名
-
private $CONN;
-
/**
-
* 构造函数:连接数据库
-
* @return TRUE:连接成功;FALSE:连接失败。
-
*/
-
function Mysql() {
-
$user = $this->USER;
-
$pass = $this->PASS;
-
$host = $this->HOST;
-
$db = $this->DBNAME;
-
// 连接数据库
-
$conn = mysql_connect($host, $user, $pass);
-
mysql_select_db($db, $conn);
-
mysql_query("SET NAMES UTF8");
-
$this->CONN = $conn;
-
return true;
-
}
-
/**
-
* 数据库表查询
-
*如 select * from table
-
*/
-
function getallinfo($table) {
-
if ( empty($table)) return false;
-
if ( empty($this->CONN) ) return false;
-
$conn = $this->CONN;
-
// 发送SQL语句,获得结果
-
$sql = "select * from $table";
-
$result = mysql_query($sql, $conn);
-
if ( (!$result) or (empty($result)) ) {
-
return false;
-
}
-
$num = 0;
-
$data = array();
-
// 将查询结果放二维数组中
-
while ( ($row = mysql_fetch_array($result)) ) {
-
$data[$num] = $row;
-
$num++;
-
}
-
mysql_free_result($result);
-
return $data;
-
}
-
-
//可用于插入、修改、更新、删除数据,创建数据表
-
function query($strSQL) {
-
if ( empty($strSQL) ) return false;
-
if ( empty($this->CONN) ) return false;
-
$conn = $this->CONN;
-
$result = mysql_query($strSQL,$conn);
-
if ( !result ) return false;
-
return $result;
-
}
-
-
function select($table,$field,$findname){
-
if ( empty($table) ) return false;
-
if ( empty($field) ) return false;
-
if ( empty($findname) ) return false;
-
if ( empty($this->CONN) ) return false;
-
$conn = $this->CONN;
-
$sql = "select * from $table where $field='$findname'";
-
$result = mysql_query($sql, $conn);
-
if ( (!$result) or (empty($result)) ) {
-
return false;
-
}
-
$data = mysql_fetch_array($result);
-
mysql_free_result($result);
-
return $data;
-
}
-
-
//根据查询条件,返回查询单条记录
-
function getinfo($strSQL) {
-
if ( empty($strSQL) ) return false;
-
if ( empty($this->CONN) ) return false;
-
$conn = $this->CONN;
-
// 发送SQL语句,获得结果
-
$result = mysql_query($strSQL, $conn);
-
if ( (!$result) or (empty($result)) ) {
-
return false;
-
}
-
$data = mysql_fetch_array($result);
-
mysql_free_result($result);
-
return $data;
-
}
-
-
//根据查询条件,返回多条记录
-
function getmoreinfo($strSQL){
-
if ( empty($strSQL) ) return false;
-
if ( empty($this->CONN) ) return false;
-
$conn = $this->CONN;
-
$result = mysql_query($strSQL, $conn);
-
if ( (!$result) or (empty($result)) ) {
-
return false;
-
}
-
$num = 0;
-
$data = array();
-
// 将查询结果放二维数组中
-
while ( ($row = mysql_fetch_array($result)) ) {
-
$data[$num] = $row;
-
$num++;
-
}
-
mysql_free_result($result);
-
return $data;
-
}
-
//根据查询条件,返回多条记录
-
//function getallinfo($table){
-
// if ( empty($table) ) return false;
-
// if ( empty($this->CONN) ) return false;
-
// $conn = $this->CONN;
-
// $sql = "select * from $table";
-
// $result = mysql_query($sql, $conn);
-
// if ( (!$result) or (empty($result)) ) {
-
// return false;
-
// }
-
// $num = 0;
-
// $data = array();
-
// // 将查询结果放二维数组中
-
// while ( ($row = mysql_fetch_array($result)) ) {
-
// $data[$num] = $row;
-
// $num++;
-
// }
-
// mysql_free_result($result);
-
// return $data;
-
//}
-
-
//返回影响(插入,查询,更新,删除)到的行数
-
function getrowsnum($strSQL){
-
if ( empty($strSQL) ) return false;
-
if ( empty($this->CONN) ) return false;
-
$conn = $this->CONN;
-
$result = mysql_query($strSQL,$conn);
-
$num = mysql_num_rows($result);
-
mysql_free_result($result);
-
return $num;
-
}
-
-
function getallrowsnum($table){
-
if ( empty($table) ) return false;
-
if ( empty($this->CONN) ) return false;
-
$conn = $this->CONN;
-
$sql = "select * from $table";
-
$result = mysql_query($sql,$conn);
-
$num = mysql_num_rows($result);
-
mysql_free_result($result);
-
return $num;
-
}
-
-
/* 可用于修改数据
-
* $tableName 表名
-
* $setPorpertyName 修改的字段名
-
* $setValue 修改的值
-
* $id 指定id
-
*
-
*/
-
function update($strSQL) {
-
if ( empty($strSQL) ) return false;
-
if ( empty($this->CONN) ) return false;
-
$conn = $this->CONN;
-
// 发送SQL语句,更新数据库
-
$result = mysql_query($strSQL, $conn);
-
if(!$result)return false;
-
return $result;
-
}
-
-
//删除指定表的某个id的记录
-
function delete($tableName,$id) {
-
if ( empty($tableName) ) return false;
-
if ( empty($this->CONN) ) return false;
-
$conn = $this->CONN;
-
$strSQL="delete from $tableName where id='$id'";
-
$result = mysql_query($strSQL, $conn);
-
return true;
-
}
-
-
//返回一个数组,数组包含该表所以字段
-
function getfields($table){
-
if ( empty($table) ) return false;
-
if ( empty($this->CONN) ) return false;
-
$conn = $this->CONN;
-
$sql = "show full fields from $table";
-
$showresult = mysql_query($sql,$conn);
-
$showdata = array();
-
while($showrow = mysql_fetch_array($showresult)){
-
$showdata[] = $showrow['Field'];
-
}
-
mysql_free_result($showresult);
-
return $showdata;
-
}
-
//返回一个表的字段的数目
-
function getfieldsnum($table){
-
if ( empty($table)) return false;
-
if ( empty($this->CONN) ) return false;
-
$conn = $this->CONN;
-
$sql = "select * from $table";
-
$showresult = mysql_query($sql,$conn);
-
$getcount = mysql_num_fields($showresult);
-
mysql_free_result($showresult);
-
return $getcount;
-
}
-
-
}
-
?>
使用举例: - <?php
-
header("content-Type: text/html; charset=utf-8");
-
session_start();
-
include "../include/conn.inc";
-
$str=microtime();
-
$arr=explode(" ",$str);
-
$start = $arr[0] * 1000;
-
$consql = new Mysql();
-
mysql_query('SET NAMES utf8;');
-
$valuetype = 2;
-
-
$musicname = $_GET['musicname'];
-
-
//$sqlsec = "select * from SecondDay_Info where CityName='$musicname'";
-
-
$Song_Info = array();
-
$Singer_Info = array();
-
$Album_Info = array();
-
$SecondDay_Info = array();
-
$strSQL = "select * from liricstable where singer_name='$musicname'";
-
if($valuetype == 2){
-
$Song_Info = $consql->getmoreinfo($strSQL);
-
print_r($str1);
-
foreach($Song_Info as $rows){
-
$str = $rows['song_name'] . "----" . $rows['singer_name'] . "----" . $rows['album_name'] . "----" . $rows['liric_path'] . "----" . $rows[album_cover_path] . "\n";
-
print_r($str);
-
}
-
}
-
?>
|
|