phpmysqlazureazure-appserviceazure-mysql-database

MySQL Query always returns only '1' in azure app service + azure database server


Currently, I am coding using azure app service and azure database server for mysql.

When the 'select' statement is executed, only '1' is returned.

I don't think it's a connection problem because 'insert' statement works fine.

This is my php code running on azure app service.

error_reporting(E_ALL); 
ini_set('display_errors',1); 
include('dbconnectr.php'); 

$id = $_GET['id'];
$stmt = $con->prepare('select * from contents where id='.$id);
$stmt->execute();
$result = $stmt->fetch();
echo $result; 

What is the problem?


Solution

  • This script will help you

    error_reporting(E_ALL); 
    ini_set('display_errors',1); 
    include('dbconnectr.php'); 
    
    $id = $_GET['id'];
    
    $stmt = $con->prepare("select * from contents where id=?");
    
    /* bind parameters */
    $stmt->bind_param("i", $id);
    
    /* execute query */
    $stmt->execute();
    
    /* bind result variables */
    $stmt->bind_result($result);
    
    /* fetch value */
    $stmt->fetch();
    
    print_r($result);
    

    I use the bind for protecting myself from sql injection.
    So I use bind_param to bind the param, then use execute for execute the command and then bind the result in the bind_result and after that use the fetch and fill the result variable and Done