第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試插入 SQL Server 數(shù)據(jù)庫時出現(xiàn)“PK”錯誤附近的語法錯誤

嘗試插入 SQL Server 數(shù)據(jù)庫時出現(xiàn)“PK”錯誤附近的語法錯誤

PHP
長風(fēng)秋雁 2023-09-08 21:36:46
數(shù)據(jù)庫異常 – yii\db\ExceptionError Info: Array(    [0] => HY000    [1] => 102    [2] => Incorrect syntax near 'PK'. [102] (severity 15) [INSERT INTO [ORDERS] ([ID], [STATUS], [ZIPFILE], [COLOR], [PRICE]) VALUES (87, 1,'PK]    [3] => -1    [4] => 15)? 導(dǎo)致:PDOException SQLSTATE[HY000]:一般錯誤:102 'PK' 附近的語法不正確。[102](嚴(yán)重性 15)[插入 [訂單]([ID]、[狀態(tài)]、[ZIPFILE]、[顏色]、[價格])值 (87, 1,'PK]在 yii2 應(yīng)用程序中,我嘗試使用 file_get_uploads php 函數(shù)上傳 zip 文件,首先將 zip 轉(zhuǎn)換為二進制并將其保存到 SQL Server 數(shù)據(jù)庫中的“ZIPFILE”列中,但收到上述錯誤??雌饋?PK 后面的特殊字符會截斷 ZIPFILE 的值以及 SQL 插入字符串中緊隨其后的任何內(nèi)容。如果我使用 PHP 函數(shù)轉(zhuǎn)義特殊字符addslashes(),我能夠完成插入,但 zip 文件會因為額外的斜杠而被損壞。更改編碼不是一個選項,因為我使用另一個應(yīng)用程序從數(shù)據(jù)庫下載數(shù)據(jù),并且該應(yīng)用程序要求值采用此編碼。有沒有辦法在不損壞 zip 文件的情況下轉(zhuǎn)義特殊字符?我添加了一些代碼以使事情更加清晰。應(yīng)用程序使用Yii2框架控制器代碼public function actionCreate(){    $model = new Orders();    $model->load(Yii::$app->request->post())       //populates ZIPFILE property with binary value of the ASCII encoded ZIPFILE string(pre-populated) compressed to a .zip file and read using file_get_contents in the Zip model    $model->ZIPFILE = (new Zip)->asBinaryString($model->ZIPFILE);        if ($model->save()) {        return $this->redirect(['view', 'id' => $model->ID]);    } else {        return $this->render(            'create', [                'model' => $model,            ]);    }}在 Zip 模型中private string $_tempFolder = '/somecache/';public function asBinaryString($content): ?string    {        $fileName = $this->create($content);            $fileAsBinary = file_get_contents(Yii::$app->basePath . $this->_tempFolder . $fileName);                return $fileAsBinary;       }加載回 $model->ZIPFILE 的字符串看起來像這樣“PKa??PM???1590409143.RTFu??n?0?_e`???Os?R??j??*???A” ?5Dq??@0……”無法使用數(shù)據(jù)庫中的鏈接將文件存儲到磁盤上,因為這是對舊應(yīng)用程序的增強,需要將數(shù)據(jù)作為 zip 存儲在數(shù)據(jù)庫中。嘗試通過使用 PHP bin2hex() 將二進制 zip 轉(zhuǎn)換為十六進制來更接近解決方案,但沒有成功。十六進制沒有特殊字符;因此,$model 保存得很好,只是保存到 SQL Server 的 zip 文件無法被其他應(yīng)用程序讀取。這不是一個簡單的拼寫錯誤問題,并且已嘗試在此處使用準(zhǔn)備好的語句,但不起作用。
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經(jīng)驗 獲得超10個贊

我花了至少10天的時間在網(wǎng)上廣泛搜索并測試了各種解決方案才解決這個問題。


上述問題的答案是不可能將二進制字符串直接保存到 SQL Server 數(shù)據(jù)庫中。該文件需要作為字符串發(fā)送hex。


解決方案:


public function asBinary($content): ?string

{

    $fileName = $this->create($content);

    $fileAsBinary = file_get_contents(Yii::$app->cache->cachePath . '/' . $fileName);

    return $fileAsBinary;   

}


/**

* zips file in cache folder into archive

*/

public function create($content): ?string

{

    $zipName = \microtime();

    try {

            $zip = new \ZipArchive;

            if ($zip->open(Yii::$app->cache->cachePath . '/'  . $zipName, \ZipArchive::CREATE) === true) {

                $zip->addFromString(time().'.RTF', $content);

                $zip->close();

            } else {

                throw new Exception(Yii::t('app', 'Archive could not be created in cache folder.'));

            }

    } catch (\Throwable $th) {

        throw $th;

    } 

    return $zipName ?? null;      

}


//return file as Hex string

public function asHex($content): string

{

    $fileAsBinary = $this->asBinary($content);

    $unpackedBinaryFile = unpack('H*hex', $fileAsBinary);

    $fileAsHex = '0x' . $unpackedBinaryFile['hex'];        

    return $fileAsHex;   

}

填充 $model 中的 ZIPFILE


$model->ZIPFILE = (new Zip)->asHex($model->ZIPFILE);

zip 文件需要轉(zhuǎn)換為hex可以直接保存到 SQL Server 中的字符串。VARBINARYSQL Server在插入數(shù)據(jù)庫時會將其轉(zhuǎn)換為。


由于不清楚的原因,我無法使用 Yii2 的準(zhǔn)備好的語句傳遞 ZIPFILE。每次都會出錯。另外,無法使用 ActiveRecord 的$model->save()方法進行保存。必須使用 SQL 查詢字符串插入。如果有人能說出原因,那就太好了。


查看完整回答
反對 回復(fù) 2023-09-08
  • 1 回答
  • 0 關(guān)注
  • 102 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號