从MySQL查询中获取值

时间:2018-04-03 21:50:54

标签: php mysql sql xamarin

我正在开发一个Xamarin.Forms社交媒体应用。我试图抓住与用户联系的帖子,这样我就可以显示海报的用户名。

我使用以下方式抓住帖子:

$sql = "SELECT * FROM posts AS P INNER JOIN users AS U ON U.id = P.user_id WHERE P.user_id=:userid";
$results = DB::query($sql, array(':userid' => $target_id));

使用print_r将其显示为:

  

阵   (       [0] =>排列           (               [id] => 剪断               [0] => 剪断               [user_id] => 剪断               [1] => 剪断               [body] => 56565               [2] => 56565               [data] => 0               [3] => 0               [timestamp] => 2018-03-22 00:00:00               [4] => 2018-03-22 00:00:00               [type] => 1               [5] => 0               [评论] => 0               [6] => 0               [users] =>               [7] =>               [8] => 剪断               [username] => username_test               [9] => username_test               [密码] => 剪断               [10] => 剪断               [user_password] =>               [11] =>               [email] => 剪断               [12] => 剪断               [name] =>乔               [13] =>乔               [bio] => Hello World Test Print               [14] => Hello World Test Print               [birthday] => 2001-05-30 00:00:00               [15] => 2001-05-30 00:00:00               [性别] => 0               [16] => 0               [created] => 2018-03-25 20:29:44               [17] => 2018-03-25 20:29:44))

在添加连接之前,我使用类似下面的内容循环结果:

foreach($results as $p) { 
    $username = $p[username']
}   

但是现在我不明白如何从用户或帖子中获取内容,例如如果我需要获取用户ID,我如何指定用户的“id”而不是来自帖子的“id”并且不小心获得帖子ID

我试过像$ p [users =>用户名],但不起作用

2 个答案:

答案 0 :(得分:1)

不确定这是否适合您,但由于您只对userId感兴趣,为什么不选择您想要的? 然后$ result数组每个记录集只有一个条目。

<!DOCTYPE html>
<html>
    <head>
        <title> My Life</title>
        <link rel="stylesheet" type="text/css" href="MyLife.css">
        <link href="https://fonts.googleapis.com/css?family=Contrail+One" rel="stylesheet">

        <style type="text/css">
         
            ul {
                list-style-type: none;
                margin: 0;
                padding: 0;
                overflow: hidden;
                background-color: #333;
            }

            li {
                float: left;
            }

            li a {
                display: block;
                color: white;
                text-align: center;
                padding: 14px 16px;
                text-decoration: none;
            }

            /* Change the link color to #111 (black) on hover */
            li a:hover {
                background-color: #111;
            }

            h1{
                width:20%
                text-align:center;
            }

            h2{
                text-align: center;
            }

            body{
                background-color: white;
            }
            img{
                width: 30%;
                float: left;


                margin: 1.66%;
            }

            p{
                margin-left: 1.66%;
                font-family: 'Contrail One', cursive;
                font-size: 35px;
                text-transform: uppercase;
                border-bottom: 2px solid black;
                width: 30%;
                padding-bottom: 20px;

            } 

            div.a{
                text-align: center;
            }
            
        </style>
    </head>
    <body>

        <!-- 
        This is the code for the Home Page -->

        <div class ="a">
            <h1> My Name is John </h1>
        </div>
        <h2> Welcome to my Website!</h2>


        <ul>
            <li><a href="">Home</a></li>
            <li><a href="MyHistory.html">My History</a></li>
            <li><a href="MyEducation.html">My Education</a></li>
            <li><a href="MyTravels.html">My Travels</a></li>
            <li><a href="">My Form Page</a></li>
            <li><a href="">What I Like to Do</a></li>
        </ul>
<ul>                <img src = "http://c1.staticflickr.com/9/8450/8026519634_f33f3724ea_b.jpg">
            <img src = "http://c2.staticflickr.com/8/7218/7209301894_c99d3a33c2_h.jpg ">
            <img src = "http://c2.staticflickr.com/8/7231/6947093326_df216540ff_b.jpg ">
            <img src = "http://c1.staticflickr.com/9/8788/17367410309_78abb9e5b6_b.jpg ">
            <img src = "http://c2.staticflickr.com/6/5814/20700286354_762c19bd3b_b.jpg ">
            <img src = "http://c2.staticflickr.com/6/5647/21137202535_404bf25729_b.jpg ">
            <img src = "http://c2.staticflickr.com/6/5588/14991687545_5c8e1a2e86_b.jpg">
            <img src = "http://c2.staticflickr.com/4/3888/14878097108_5997041006_b.jpg ">
            <img src = "http://c2.staticflickr.com/8/7579/15482110477_0b0e9e5421_b.jpg">
</ul>
    </body>
</html>

答案 1 :(得分:1)

试试这个:

SELECT * FROM posts
INNER JOIN users
ON users.id = posts.user_id
WHERE posts.user_id=:userid

您的foreach语句缺少引号并以分号结束:

foreach($results as $p) { 
    $username = $p['username'];
}  

获取明确的ID:

SELECT *, users.id AS uid, posts.id AS postId
FROM posts
INNER JOIN users ON users.id = posts.user_id
WHERE posts.user_id=:userid

要在同一查询中获取相似的数量:

SELECT *, users.id AS uid, posts.id AS postId,
COUNT(L.likes) AS likeCount 
FROM posts
INNER JOIN users ON users.id = posts.user_id
LEFT JOIN like AS L ON L.post_id = P.id
WHERE posts.user_id=:userid