-1

I'm having trouble in this, i want to return the value of item_name.

$productnum = "1001";

$mysqli = new mysqli("localhost", "ewconline_db_user", "steve030405", "ewconline_db");
    $result = $mysqli->query("SELECT * FROM `shop_products` WHERE `item_id` = '$productnum' ");
    $row = $result->fetch_assoc();
    $item_name = $row['item_name'];

    $response = "{
                    'recipient':{
                        'id': $senderId
                        },
                    'message':{
                        'text': $item_name
                    }
                }";

it doesn't seem to get the value and sends a reply back to user.

and if i run the sql program on different url. it works perfectly fine.

Steve I
  • 21
  • 6
  • you can try with $item_name = $row[0]['item_name'] – Siva Feb 08 '18 at 08:21
  • sir? it still doesnt work. – Steve I Feb 08 '18 at 08:45
  • Don’t create JSON “manually” in string form. Create the proper data structure, and then use json_encode. _“and if i run the sql program on different url. it works perfectly fine.”_ - well then do some proper debugging …? – CBroe Feb 08 '18 at 09:11
  • WTH!. it works sir. THANK YOU THANK YOU THANK!. Don’t create JSON “manually” in string form. Create the proper data structure, and then use json_encode. – Steve I Feb 08 '18 at 10:43

3 Answers3

0

You can try like this

while ($row = $result->fetch_assoc()) {
   $item_name = $row['item_name'];
}
Siva
  • 1,481
  • 1
  • 18
  • 29
0

You should always check if your connection succeeded to begin with. So after you construct your mysqli instance, add something like this:

if (mysqli_connect_errno()) {
    printf("Database connection failed: %s\n", mysqli_connect_error());
    die(); // or throw an exception or whatever
}

This should tell you why are not getting your expected result.

Pevara
  • 14,242
  • 1
  • 34
  • 47
0
$mysqli = new mysqli("localhost", "ewconline_db_user", "steve030405", "ewconline_db");
$result = $mysqli->query("SELECT * FROM `shop_products` WHERE `item_id` = '".$productnum."'");
$row = $result->fetch_assoc();
$answer = $row['item_name'];

$response = [
    'recipient' => [ 'id' => $senderId ],
    'message' => [ 'text' => $answer ]
];

I have done it like this, and it works.

and used json_encode($response);

Steve I
  • 21
  • 6