【问题标题】:Mysqli query no resultmysqli查询无结果
【发布时间】:2017-07-24 23:09:04
【问题描述】:

我在 mysqli 准备查询中遇到问题,我无法找出问题所在。

在 phpmyadmin 中我正在编写这个查询

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

我得到了一个成功的结果。

当我尝试使用 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;
}

Print $result 打印这些数据。

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
)

奇怪的是它曾经可以工作,但突然停止了,我无法解释为什么!

附:所有插入都使用 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 变量下方,我将字符集设置为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.
}

【问题讨论】:

  • 也许尝试设置mysqli_set_charset
  • @bxN5 我想这与字符集有关。查看更新后的问题!
  • @bxN5 好的,我正确设置了字符集并且它工作正常。如果你想回答它,我会接受它:)
  • 会很酷:)

标签: php mysql mysqli prepared-statement


【解决方案1】:

看起来像字符集问题

尝试设置mysqli_set_charset

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 2015-06-18
    • 1970-01-01
    • 2012-09-20
    • 2016-11-03
    相关资源
    最近更新 更多