简体   繁体   English

PHP PDO SQL查询

[英]PHP PDO SQL query

I have two tables, and when I use a SQL Statement like this, it works on phpmyadmin: 我有两个表,当我使用这样的SQL语句时,它可以在phpmyadmin上运行:

SELECT DISTINCT B.ID,
    CASE B.Menu 
        WHEN 'Menu1' THEN S.Menu1 
        WHEN 'Menu2' THEN S.Menu2 
        WHEN 'Menu3' THEN S.Menu3 
        WHEN 'Menu4' THEN S.Menu4 
        WHEN 'Menu5' THEN S.Menu5 
        ELSE 'Unknown' END AS Menu, S.Day 
    FROM order B  JOIN menu_plan S ON B.ID_Date = S.ID
    WHERE B.ID_order = '4000859'

But when I use this Statement in my PHP PDO query, it give nothing back :/ 但是,当我在PHP PDO查询中使用此Statement时,它什么也没有返回:/

<?php

require_once "config.php";

error_reporting(E_ALL);

try
{
    $con = new PDO("mysql:host=".$db_host.";dbname=".$db_name,$db_user,$db_password);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
    echo "Error: ".$e->getMessage();
    exit();
}   

$_sql = sprintf("SELECT DISTINCT B.ID,
    CASE B.Menu 
        WHEN 'Menu1' THEN S.Menu1 
        WHEN 'Menu2' THEN S.Menu2 
        WHEN 'Menu3' THEN S.Menu3 
        WHEN 'Menu4' THEN S.Menu4 
        WHEN 'Menu5' THEN S.Menu5 
        ELSE 'Unknown' END AS Menu, S.Day 
    FROM order B  JOIN menu_plan S ON B.ID_Date = S.ID
    WHERE B.ID_order = '4000859'"); // Here the sql statement
$stmt = $con->query($_sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($rows);
echo json_encode(array('Order'=>$rows));

?>

EDIT: With this code now it shows two arrays with the variables like in phpmyadmin. 编辑:现在,使用此代码可以显示两个数组,这些数组的变量类似于phpmyadmin。 But the json_encode gives nothing back. 但是json_encode没有任何回报。 The php file only show this: php文件仅显示以下内容:

array(2) { [0]=> array(3) { ["ID"]=> string(1) "1" ["Menu"]=> string(5) "Meal1" ["Day"]=> string(8) "Thursday" } [1]=> array(3) { ["ID"]=> string(1) "4" ["Menu"]=> string(1) "-" ["Day"]=> string(7) "Sunday" } } 

The config.php and the variables for the data base and the table are correct. config.php以及数据库和表的变量正确。 When I use the PHP file without the case-statement in the sql query then it shows something. 当我在sql查询中使用不带大小写声明的PHP文件时,它显示了一些信息。 In phpmyadmin it shows all with the case-statement. 在phpmyadmin中,它显示所有带有大小写的语句。

EDIT ANSWER: The Problem was the utf8-encoding. 编辑回答:问题是utf8编码。 I have to use this: 我必须使用这个:

$con = new PDO("mysql:host=".$db_host.";dbname=".$db_name,$db_user,$db_password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

Try changing this: 尝试更改此:

$sql = sprintf("..."); // Here the sql statement
$sql = $con->prepare($_sql);

To this: 对此:

$_sql = sprintf("..."); // Here the sql statement
$sql = $con->prepare($_sql);

Looks like you just forgot the underscore in the first sql definition. 看起来您只是忘记了第一个sql定义中的下划线。

Use this code: 使用此代码:

$stmt = $con->prepare($_sql);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($rows);
echo json_encode(array('Order'=>$rows));

and if the value is hardcoded in the query you don't have to prepare 如果该值在查询中是硬编码的,则无需准备

$stmt = $con->query($_sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($rows);
echo json_encode(array('Order'=>$rows));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM