dongwo5686 2016-10-13 14:52
浏览 36
已采纳

在PHP语言中从方法POST显示JSON对象

I'm trying to retrieve a JSON object from a sql query of php file.

For this I make a html code with a form, action=queryProduct.php and method post.

HTML code:

                  <form action="queryProduct.php" method="post">
                     <input name="codigo" type="text" placeholder="codigo">
                    <input type="submit" value="TEST">  
                  </form>

queryProduct.php:

<?php   
        session_start();

        if(isset($_SESSION['username']) && $_SESSION['username'] <> ''){ 

            include("functions.php"); 
            include("tools.php"); 

            $conn = Conectarse("localhost", "5432", "dbname", "dbuser", "dbpass");  

            $codigo = $_POST['codigo'];

            echo $codigo;

            $query = "SELECT * FROM produccion.ma_producto WHERE codigo={$codigo}"; 

            $result = pg_query($conn, $query);  

            if (!$result) {
                echo "Error query: " . pg_last_error($conn);
            } else {
                header('Content-type: application/json; charset=utf-8');
                echo json_encode($result);
            }

        echo json_encode($result);

            pg_close($conn);      

        } else{
            ?><p>La sesión no está activa, por favor ingrese <a href="login.php">aquí</a></p>
<?php   
        }?>

The idea is: it's make a JSON object from the query result and show the object with the browser: echo json_encode($result);

Now It doesn't nothing :(

I have tested the sql query and it's working fine...

  • 写回答

1条回答 默认 最新

  • duande1985 2016-10-13 14:59
    关注

    RTM: http://php.net/pg_query

    Return Values: A query result resource on success or FALSE on failure.

    That result resource is NOT something you can json_encode(). You have to fetch result rows via that handle, put that into an array, and then encode that array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!