mysql—用php编写抽象数据库

pgvzfuti  于 2021-06-15  发布在  Mysql
关注(0)|答案(0)|浏览(329)

我有一个用php编写的程序,但是我需要替换query函数,使queryfub用于查询插入、更新和删除。此外,query函数应该返回resource类对象,并执行影响请求的行数(受影响的行)。我不知道该怎么办。

?php
function db_connect( $error=null, $configfile='database_config.php' )
{
require_once( $configfile );
//  $conn = pg_connect( "host=$server dbname=$database user=$user password=$password" );
//  $error= pg_last_error( $conn ); 

$conn = mysqli_connect( $server, $user, $password, $database ); 
$error= mysqli_error( $conn ); 
/* change character set to utf8 */
mysqli_set_charset($conn, "utf8");
return $conn;
}

function db_query( $conn, $query, $error=null )
{
//  $res=pg_query( $conn, $query );
//  $error=pg_last_error( $conn );

$res=mysqli_query( $conn, $query );
$error=mysqli_error( $conn );
return $res;
}

function db_fetch( $result )
{
//  return pg_fetch_assoc( $result );

return mysqli_fetch_assoc( $result );
}

 function db_genid( $link, $table, $idcol )
 {
    $res=db_query( $link, "select max($idcol) from $table" );
    $row=db_fetch( $res );
    if ( $res ) return $row[0]+1;
    return 1;
 }

function db_close( $conn )
{
 //  pg_close($conn);

  mysqli_close($conn);
 }
?>
<!DOCTYPE html>
<html>
<head>
<title>Tworzenie tabeli</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>

Dane tej tabeli:
<table border="1">
<?php
  $link=db_connect();
  $result=db_query( $link, "select * from osoba" );
 while ( $row=db_fetch( $result ) )
 {
    echo "<tr>";
    foreach ( $row as $col ) echo "<td>$col</td>";
    echo "</tr>";   
  }
 db_close( $link );
 ?>
</table> 
<hr><h2> Zrodlo pliku </h2>
<?php 
 show_source(__FILE__);
?>
</body>
</html>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题