1

I have two tables, and when I use a SQL Statement like this, it works on 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

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. But the json_encode gives nothing back. The php file only show this:

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. When I use the PHP file without the case-statement in the sql query then it shows something. In phpmyadmin it shows all with the case-statement.

EDIT ANSWER: The Problem was the utf8-encoding. 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"));
7
  • Where is $_sql defined? Commented Sep 10, 2014 at 18:45
  • looks like you overwrote the variable $sql with the connection variable. You also have a different variable name in the prepare statment $_sql Commented Sep 10, 2014 at 18:46
  • Sorry, i correct it. But that was not the mistake. Commented Sep 10, 2014 at 18:49
  • I still don't see a sql query. ... is not a query. Commented Sep 10, 2014 at 18:54
  • show us the real code bro Commented Sep 10, 2014 at 18:59

2 Answers 2

1

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));
Sign up to request clarification or add additional context in comments.

7 Comments

it shows two arrays which are right!! :) But the json_encode give a null back :(
@user3493797 sorry typo check again, use $rows when you encode
Thanks, but there are these 2 arrays from var_dump. But Json_Encode shows nothing.
can you update your question with the dump, I dont see why it fails unless you have bad encoding characters
@user3493797 json_encode will only fail when the strings are not encoded properly
|
1

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.

2 Comments

Thanks for answering, but thats not the mistake. I correct my question.
are you passing any parameter to this query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.