mysql資料庫連接類

來源:互聯網
上載者:User

mysql資料庫連接類
/*******************************
 * PHP MySQL Connect
 * Author  : Ray
 * Date    : 2005/07/30
 * Updata  : 2005/10/23
 * Version : 0.1
 */
 
/* 判斷串連資料庫所需的參數是否存在 */
if (empty($dbhost))
{
    $error = "<li>資料庫主機名稱為空白</li>";
}
if (empty($dbuser))
{
    $error .= "<li>資料庫使用者名稱為空白</li>";
}
/*if (empty($dbpass))
{
    $error .= "<li>資料庫密碼為空白</li>";
}*/
if (empty($dbname))
{
    $error .= "<li>資料庫名為空白</li>";
}

if (isset($error)) showErrorMessage("1","資料庫操作失敗",$error);

/* 串連資料庫 */
    if (!$connect = @mysql_connect($dbhost,$dbuser,$dbpass))
    {
        $error = "<li>無法串連上資料庫主機</li><li>MySQL錯誤資訊:".mysql_error()."</li>";
        showErrorMessage("1","資料庫連接失敗",$error);exit;
    }
   
    if (!$select_db = @mysql_select_db($dbname))
    {
        $error = "<li>無法開啟資料庫</li><li>MySQL錯誤資訊:".mysql_error()."</li>";
        showErrorMessage("1","資料庫開啟失敗",$error);exit;
    }
    mysql_query("set names 'gbk'");
/* 資料庫查詢 */   
function dbQuery ($mysql_query,$debug = 0)
{
    if ($debug != 0)
    {
        echo $mysql_query."n";
        if ($debug == 1) return;
    }
    $GLOBALS['mysql']['query_count']++;
    if (!$query = @mysql_query($mysql_query))
    {
        $error = "<li>MySQL語句在執行時遇到了一些錯誤</li><li>MySQL語句:".$mysql_query."</li><li>MySQL錯誤資訊:".mysql_error()."</li>";
        showErrorMessage("1","錯誤的MySql查詢",$error);exit;
    }
    if ($debug != 0) echo "ROWS:".mysql_num_rows($query);
    return $query;
}

/* 查詢結果分解到數組 */ 
function dbArray ($mysql_result)
{
    return @mysql_fetch_array($mysql_result);
}

function queryBot($query_data)
{
    if (!empty($query_data['field']))
    {
        foreach ($query_data['field'] as $field => $value)
        {
            switch ($query_data['action'])
            {
                case 'insert' :
                    $query_insert_field[(int)$i] .= "`".$field."`";
                    $query_insert_value[(int)$i] .= "'".$value."'";
                    break;
                case 'update' :
                    $query_update_field[(int)$i] .= "`".$field."` = '".$value."'";
                    unset($query_update_where);
                    foreach ($query_data['where'] as $field => $value)
                    {
                        $query_update_where[(int)$j] .= "`".$field."` = '".$value."'";
                    }
                    $j++;
                    break;
            }
            $i++;
        }
            switch ($query_data['action'])
            {
                case 'insert' :
                    dbQuery("INSERT INTO `".$GLOBALS['prefix'].$query_data['table']."` (".implode(" , ",$query_insert_field).") VALUES (".implode(" , ",$query_insert_value).");");
                    break;
                case 'update' :
                    dbQuery("UPDATE `".$GLOBALS['prefix'].$query_data['table']."` SET ".implode(" , ",$query_update_field)." WHERE ".implode(" AND ",$query_update_where).";");
                    break;
            }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.