2014-02-24 32 views
0

我在頁面上有一張表,我正在填充來自MySQL數據庫的值;相當大的MySQL數據庫。我還爲表格的每一列都設置了過濾器,以便用戶可以過濾結果。我創建了一個存儲過程,只要我在MySQL中調用它就可以工作。但是,當我試圖從我的PHP代碼發送NULL值時,我在MySQL端發生錯誤。我得到的過濾器值在PHP端像這樣:我如何從我的PHP代碼獲取NULL值到MySQL存儲過程?

$var01 = (isset($_GET['var01']) && $_GET['var01']) ? $_GET['var01'] : null; 
. 
. 
. 
$var23 = (isset($_GET['var23']) && $_GET['var23']) ? $_GET['var23'] : null; 

然後我打電話給我的存儲過程像這樣:

$query = "CALL qyertWithFiltering (" . $var01 . ")"; // up to $var23 

唯一的問題是,就像我說的,越來越空值從PHP頁面存儲到存儲過程中。如果我是MySQL內並使用下面的查詢,我得到的結果我想到:

CALL queryWithFiltering (10,1234,,,,,,,,,,,,,,,,,,,,,,,) 

我碰到下面的MySQL錯誤:

call query WithFiltering(10,1234,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL); 

然而,當我送了從PHP頁面如下:

Database Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',,,,,,,,,,,,,,,,,,,,,)' at line 1 

正如你所看到的,不存在NULL值,而是我猜,我正在向存儲過程發送空值。作爲參考,這裏是我的存儲過程。同樣,我的存儲過程完美地工作,正如在MySQL中預期的那樣,但是從我的PHP頁面調用時不會。

use `my_database`; 

DROP PROCEDURE IF EXISTS `queryWithFiltering`; 

DELIMITER // 
CREATE PROCEDURE `queryWithFiltering` (IN inLimit INT(11),IN inOffset INT(11),IN param01 VARCHAR(255),IN param02 VARCHAR(255),IN param03 DATE,IN param04 DATE,IN param05 DATE,IN param06 TINYINT(1),IN param07 INT(11),IN param08 INT(11),IN param09 INT(11),IN param10 INT(11),IN param11 DATE,IN param12 TINYINT(1),IN param13 VARCHAR(50),IN param14 VARCHAR(50),IN param15 DATE,IN param16 INT(11),IN param17 VARCHAR(50),IN param18 DECIMAL(13,2),IN param19 VARCHAR(50),IN param20 VARCHAR(50),IN param21 VARCHAR(50),IN param22 INT(11),IN param23 INT(11)) 
LANGUAGE SQL 
DETERMINISTIC 
SQL SECURITY DEFINER 
COMMENT 'comment goes here ...' 
BEGIN 
DECLARE spLimit INT(11); 
DECLARE spOffset INT(11); 
DECLARE spVar01 VARCHAR(255); 
DECLARE spVar02 VARCHAR(255); 
DECLARE spVar03 DATE; 
DECLARE spVar04 DATE; 
DECLARE spVar05 DATE; 
DECLARE spVar06 TINYINT(1); 
DECLARE spVar07 INT(11); 
DECLARE spVar08 INT(11); 
DECLARE spVar09 INT(11); 
DECLARE spVar10 INT(11); 
DECLARE spVar11 DATE; 
DECLARE spVar12 TINYINT(1); 
DECLARE spVar13 VARCHAR(50); 
DECLARE spVar14 VARCHAR(50); 
DECLARE spVar15 DATE; 
DECLARE spVar16 INT(11); 
DECLARE spVar17 VARCHAR(50); 
DECLARE spVar18 DECIMAL(13,2); 
DECLARE spVar19 VARCHAR(50); 
DECLARE spVar20 VARCHAR(50); 
DECLARE spVar21 VARCHAR(50); 
DECLARE spVar22 INT(11); 
DECLARE spVar23 INT(11); 

SET spLimit = inLimit; 
SET spOffset = inOffset; 
SET spVar01 = param01; 
SET spVar02 = param02; 
SET spVar03 = param03; 
SET spVar04 = param04; 
SET spVar05 = param05; 
SET spVar06 = param06; 
SET spVar07 = param07; 
SET spVar08 = param08; 
SET spVar09 = param09; 
SET spVar10 = param10; 
SET spVar11 = param11; 
SET spVar12 = param12; 
SET spVar13 = param13; 
SET spVar14 = param14; 
SET spVar15 = param15; 
SET spVar16 = param16; 
SET spVar17 = param17; 
SET spVar18 = param18; 
SET spVar19 = param19; 
SET spVar20 = param20; 
SET spVar21 = param21; 
SET spVar22 = param22; 
SET spVar23 = param23; 

SET @primaryQuery = "select ... where ..."; 

IF (spVar01 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and alpha like '%", spVar01, "%'"); 
END IF; 

IF (spVar02 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and beta like '%", spVar02, "%'"); 
END IF; 

IF (spVar03 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and gamma like '%", spVar03, "%'"); 
END IF; 

IF (spVar04 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and delta like '%", spVar04, "%'"); 
END IF; 

IF (spVar05 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and epsilon like '%", spVar05, "%'"); 
END IF; 

IF (spVar06 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and zeta = '%", spVar06, "%'"); 
END IF; 

IF (spVar07 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and eta like '%", spVar07, "%'"); 
END IF; 

IF (spVar08 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and theta like '%", spVar08, "%'"); 
END IF; 

IF (spVar09 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and iota like '%", spVar09, "%'"); 
END IF; 

IF (spVar10 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and kappa like '%", spVar10, "%'"); 
END IF; 

IF (spVar11 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and lambda like '%", spVar11, "%'"); 
END IF; 

IF (spVar12 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and mu = '%", spVar12, "%'"); 
END IF; 

IF (spVar13 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and nu like '%", spVar13, "%'"); 
END IF; 

IF (spVar14 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and xi like '%", spVar14, "%'"); 
END IF; 

IF (spVar15 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and omicron like '%", spVar15, "%'"); 
END IF; 

IF (spVar16 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and pi like '%", spVar16, "%'"); 
END IF; 

IF (spVar17 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and rho like '%", spVar17, "%'"); 
END IF; 

IF (spVar18 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and sigma like '%", spVar18, "%'"); 
END IF; 

IF (spVar19 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and tau like '%", spVar19, "%'"); 
END IF; 

IF (spVar20 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and upsilon like '%", spVar20, "%'"); 
END IF; 

IF (spVar21 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and phi like '%", spVar21, "%'"); 
END IF; 

IF (spVar22 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and chi like '%", spVar22, "%'"); 
END IF; 

IF (spVar23 IS NOT NULL) THEN 
    SET @primaryQuery = CONCAT(@primaryQuery, " and psi like '%", spVar23, "%'"); 
END IF; 

SET @primaryQuery = CONCAT(@primaryQuery, " order by ID limit ", spLimit, " offset ", spOffset); 

PREPARE stmt FROM @primaryQuery; 
EXECUTE stmt; 

END// 

回答

1

PHP「null」值與SQL空值完全不兼容。插入到字符串中的PHP null只是一個空字符串。例如

$null = NULL; 
$strin = "foo{$null}bar"; // comes "foobar", with nothing in between foo and bar. 

如果你想有一個PHP空轉換成SQL空,你需要將它做成字符串'NULL'代替,例如

$phpnull = null; 
$fakenull = "NULL"; //this is just a string that happens to say "null" 

$sql = "UPDATE ... WHERE phpnull=$null, fakenull=$fakenull"; 

和您的SQL變爲:

UPDATE ... WHERE phpnull=, fakenull=NULL; 
         ^---empty string, because of PHP type conversions 
            ^^^^^---php string, now being used as an SQL null 
相關問題