想请教下导致下面描述的问题的可以原因,多谢了! function insert($table, $data){
if(!$table){
echo 'Table can not be empty!';exit;
}
if(!is_array($data) || empty($data)){
echo 'The type of data stored in the array should be!';exit;
} $fields = '';
$values = '';
foreach($data as $key => $val){
$fields .= ','.$key;
$values .= ',"'.$val.'"';
}
$fields = ltrim($fields, ',');
$values = ltrim($values, ',');
$sql = 'insert into '.$table.'('.$fields.') values('.$values.')';
//为什么在phpmyadmin中直接执行可以,mysql_query($sql)却不行呢,链接数据库是不存在问题的
echo $sql;exit;//insert into cdb_member(uid,username,face,status) values("22222","PsdfdsM","default.gif","0")
mysql_query($sql);
return mysql_insert_id();
}
if(!$table){
echo 'Table can not be empty!';exit;
}
if(!is_array($data) || empty($data)){
echo 'The type of data stored in the array should be!';exit;
} $fields = '';
$values = '';
foreach($data as $key => $val){
$fields .= ','.$key;
$values .= ',"'.$val.'"';
}
$fields = ltrim($fields, ',');
$values = ltrim($values, ',');
$sql = 'insert into '.$table.'('.$fields.') values('.$values.')';
//为什么在phpmyadmin中直接执行可以,mysql_query($sql)却不行呢,链接数据库是不存在问题的
echo $sql;exit;//insert into cdb_member(uid,username,face,status) values("22222","PsdfdsM","default.gif","0")
mysql_query($sql);
return mysql_insert_id();
}
function Db($host="localhost",$user="root",$pass="123456",$db="test"){ $conn=mysql_connect($host,$user,$pass);
if(!$conn){
die("can't connect to mysql sever");
}else{
mysql_select_db($db,$conn);
mysql_query("SET NAMES 'utf8'");
}
}
虽然可以不用,但说不定就是这个问题呢
影响数据库无效(insert, delete, update),查询有效(select)
<?php
class DB{ function __construct(){ $config = parse_ini_file('config.ini'); if(!isset($config['dbhost']) || !$config['dbhost']){
echo 'Database address can not be empty!';exit;
} if(!isset($config['dbuser']) || !$config['dbuser']){
echo 'Database users can not be empty!';exit;
} if(!isset($config['dbpass']) || !$config['dbpass']){
echo 'Database password can not be empty!';exit;
} if(!isset($config['dbname']) || !$config['dbname']){
echo 'Database name can not be empty!';exit;
} $port = (isset($config['dbport']) && $config['dbport'])? $config['dbport']: 3306;
mysql_connect($config['dbhost'].':'.$port, $config['dbuser'], $config['dbpass']) or die('Connect DB mysql is fail!');
mysql_select_db($config['dbname']);
if(isset($config['dbchar']) && $config['dbchar']){
mysql_query('set names '.$config['dbchar']);
}
}
}
?><?php
class Model extends DB{ function insert($table, $data){
if(!$table){
echo 'Table can not be empty!';exit;
}
if(!is_array($data) || empty($data)){
echo 'The type of data stored in the array should be!';exit;
} $fields = '';
$values = '';
foreach($data as $key => $val){
$fields .= ', '.$key;
$values .= ', "'.$val.'"';
}
$fields = ltrim($fields, ', ');
$values = ltrim($values, ', ');
$sql = 'insert into '.$table.'('.$fields.') values('.$values.')';
mysql_query($sql);
return mysql_insert_id();
} function delete($table, $where = ''){
if(!$table){
echo 'Table can not be empty!';exit;
}
if($where){
$wheres = ' where 1';
if(is_array($where) && !empty($where)){
foreach($where as $key => $val){
$wheres .= ' and '.$table.'.'.$key.' = "'.$val.'"';
}
}else{
$wheres .= ' and '.$where;
}
}
$sql = 'delete from '.$table.($wheres? ' '.$wheres: '');
mysql_query($sql);
return mysql_affected_rows();
} function update($table, $data, $where = ''){
if(!$table){
echo 'Table can not be empty!';exit;
}
if(!is_array($data) || empty($data)){
echo 'The type of data stored in the array should be!';exit;
}
$set = '';
foreach($data as $key => $val){
$set .= ', '.$key.' = "'.$val.'"';
}
$set = ltrim($set, ', ');
if($where){
$wheres = ' where 1';
if(is_array($where) && !empty($where)){
foreach($where as $key => $val){
$wheres .= ' and '.$table.'.'.$key.' = "'.$val.'"';
}
}else{
$wheres .= ' and '.$where;
}
}
$sql = 'update '.$table.' set '.$set.($wheres? $wheres: '');
mysql_query($sql);
return mysql_affected_rows();
} function select($table, $where = '', $fields = '', $type = ''){
if(!$table){
echo 'Table can not be empty!';exit;
}
$sql = '';
if($where){
$wheres = ' where 1';
if(is_array($where) && !empty($where)){
foreach($where as $key => $val){
$wheres .= ' and '.$table.'.'.$key.' = "'.$val.'"';
}
}else{
$wheres = $where;
}
}
if($fields){
$fieldss = '';
if(is_array($fields) && !empty($fields)){
foreach($fields as $val){
$fieldss .= ', '.$table.'.'.$val;
}
$fieldss = ltrim($fieldss, ', ');
}else{
$fieldss = $fields;
}
}else{
$fieldss = '*';
}
$sql = 'select '.$fieldss.' from '.$table.($wheres ? ' '.$wheres: '');
$ret = mysql_query($sql);
$ret_arr = array();
while($row = mysql_fetch_assoc($ret)){
if($type == 'One'){
foreach($row as $val){
return $val;
}
}else if($type == 'Row'){
return $row;
}else{
$ret_arr[] = $row;
}
}
return $ret_arr;
}
}
?>
其他的好像要将连接传进去mysql_query($sql,$conn);