download1323 2017-03-04 23:32
浏览 59
已采纳

Mysqli查询没有结果

I'm having an issue in mysqli prepare query that i cannot figure out what is wrong.

In phpmyadmin i'm writing this query

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

And i get a successful result.

enter image description here

When I try to do that with a mysqli prepare statement i get an error.

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 prints those data.

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
)

The weird thing is that it used to work, but all of a sudden stopped and i can't explain why !

P.S. All inserts are working with mysqli + prepare statements. Queries like these stopped working.

Update 1.


I've done this error logging above my class

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

And now i get this error.

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
', '\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')

And below the $db variable i set the charset to 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.
}
  • 写回答

1条回答 默认 最新

  • douyouqian8550 2017-03-05 10:33
    关注

    it looks like charset problem

    try to set mysqli_set_charset

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法