带有别名字段名称的mySQL在curl_exec PHP调用中失败

时间:2017-06-29 03:44:21

标签: php mysql php-curl

我有一个简单的方法,使用function load_entry_list($url) { // Initialize an empty array to store entries $entries = array(); // Load the data from the API $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $ch_data = curl_exec($ch); curl_close($ch); ***point of failure*** if(!empty($ch_data)) // { // Convert the JSON into an array of entry objects $json_data = json_decode($ch_data, TRUE); foreach($json_data as $entry) { $entries[] = (object) $entry; } return $entries; } else { die('Something went wrong with the API request!'); } } 为数据调用PHP脚本。

// Create connection
$conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);

// Check connection
if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT rec.id, rec.name, rec.submitted_by, rec.addDate, rec.hits, rec.metaDesc, rec.metaKeys, 
              IFNULL(pic.picPath,'default.png') as picPath, 
              var.catname as variety, cat.catname as category
        FROM wms_recipes rec LEFT OUTER JOIN 
             wms_categories var on rec.cat = var.id LEFT OUTER JOIN
             wms_categories cat on var.childOf = cat.id LEFT OUTER JOIN
             wms_pictures pic on rec.id = pic.recipe
       WHERE enRecipe  = 'yes'
             AND isApproved  = 'no'
       ORDER BY 1";

$result = $conn->query($sql);   

$rows = array();
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {              
        $rows[] = $row;
    }
} 
$conn->close();

$articles = array();
foreach($rows as $item) {           
    $myArticle = new article();
    $myArticle->id = $item['id'];       
    $myArticle->name = $item['name'];
    $myArticle->category = $item['category'];  *** it will also work if I comment out this line
    $myArticle->variety = $item['variety'];
    $myArticle->submitted_by = $item['submitted_by'];
    $myArticle->addDate = $item['addDate'];
    $myArticle->hits = $item['hits'];
    $myArticle->description = $item['metaDesc'];
    $myArticle->keys = $item['metaKeys'];
    $myArticle->picPath = IMAGES_URL.'recipe/'.$item['picPath'];
    $articles[] = $myArticle;
}

header('Content-Type: application/json');   
echo json_encode( $articles );
exit;

PHP脚本看起来像这样

cat.catname as category

更新:以下是问题,

如果我在SQL查询中保留$ch_data

然后来自$ch_data = curl_exec($ch);的来电方法empty将为[11] => article Object ( [id] => 12 [name] => White Zinfandel from Fresh Juice [category] => Rosé Wines [variety] => Zinfandel [submitted_by] => Jim Stevens [addDate] => 2016-04-27 [hits] => 558 [description] => A good gift wine that all will like [metaKeys] => [picPath] => http://localhost/resources/images/recipe/default.png [keys] => White Zinfandel )

事实证明,一个类别导致了这个问题。以下是罪魁祸首的一个例子:

curl_exe

请注意类别名称 RoséWines 。急性é导致了这个问题。如果我拼写它上升,问题就会消失。 SQL返回就好了。但normalize不喜欢它。所以现在我的问题是为什么以及如何解决它。

1 个答案:

答案 0 :(得分:0)

修复此问题的最简单方法是强制将mysql连接强制为utf-8:

$conn = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME_WMS); $conn->set_charset("utf8");