2017-03-04 148 views
0

我在mysqli準備查詢時遇到問題,我無法弄清楚什麼是錯的。Mysqli查詢無結果

在phpMyAdmin我寫這篇文章的查詢

SELECT `country_id` FROM `oa_country` WHERE `name` = "Ελλάδα" 

,我得到一個成功的結果。

enter image description here

當我嘗試做與mysqli的準備聲明我得到一個錯誤。

function getCountryId($country) { 
    print($country); \\prints "Ελλάδα" or anything else, even countries in latin letters. 
    $db = mysqli_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD,DB_DATABASE); 
    $result = mysqli_prepare($db, "SELECT `country_id` FROM `oa_country` WHERE `name` = ? ;"); 
    mysqli_stmt_bind_param($result, 's', $country); 
    mysqli_stmt_execute($result); 
    mysqli_stmt_bind_result($result, $countcountry); 
    print_r($result);\\ see below. 
    while(mysqli_stmt_fetch($result)) 
     { 
      $countryid = $countcountry; 

     } 
    mysqli_stmt_close($result); 
    mysqli_close($db); 

    echo $countryid; \\undefined variable countryid 
    return $countryid; 
} 

打印$結果打印這些數據。

mysqli_stmt Object 
(
    [affected_rows] => -1 
    [insert_id] => 0 
    [num_rows] => 0 
    [param_count] => 1 
    [field_count] => 1 
    [errno] => 0 
    [error] => 
    [error_list] => Array 
     (
     ) 

    [sqlstate] => 00000 
    [id] => 1 
) 

奇怪的是,它曾經工作,但突然停下來,我無法解釋爲什麼!

P.S.所有的插入都使用mysqli + prepare語句。 像這樣的查詢停止工作。

更新1


我已經做了我的課

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 

,現在我得到這個錯誤上面這個錯誤日誌。

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Column 'country_id' cannot be null' in /some/url/lib/registerClass.php:49 
Stack trace: 
#0 /some/url/lib/registerClass.php(49): mysqli_stmt_execute(Object(mysqli_stmt)) 
#1 /some/url/register.php(34): RegisterLibrary->Address(21267, 'kapa\n', '\xCE\x9D\xCE\xAC\xCF\x84\xCF\x83\xCE\xB9\xCE\xBF\xCF\x82', 'oriste', 'pws', '123123', 'asdasd', '4121sadsa', 'sadas', '13322', '\xCE\x95\xCE\xBB\xCE\xBB\xCE\xAC\xCE\xB4\xCE\xB1', '\xCE\x91\xCF\x84\xCF\x84\xCE\xB9\xCE\xBA\xCE\xAE') 

而在$db變量下面,我將charset設置爲utf8。

function getCountryId($country) { 
    $db = mysqli_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD,DB_DATABASE); 
    mysqli_set_charset($db, 'utf8'); 
    if ($stmt = mysqli_prepare($db, "SELECT country_id FROM oa_country WHERE name=?")) { 

     /* bind parameters for markers */ 
     mysqli_stmt_bind_param($stmt, "s", $country); 

     /* execute query */ 
     mysqli_stmt_execute($stmt); 

     /* bind result variables */ 
     mysqli_stmt_bind_result($stmt, $countryidr); 
     $thecountryid = $countryidr; 
     echo $thecountryid; //no echo. 
     /* fetch value */ 
     mysqli_stmt_fetch($stmt); 
     /* close statement */ 
     mysqli_stmt_close($stmt); 

    } 
    return $thecountryid; //no return. 
} 
+0

也許嘗試設置mysqli_set_charset – bxN5

+0

@ bxN5我想它做的charset即可。看到更新的問題! –

+0

@ bxN5好的,我設置了正確的字符集,它工作。如果你想回答它,我會接受它:) –

回答

1

它看起來像字符集問題

嘗試設置mysqli_set_charset