将WHERE语句放在SQL STATEMENT中

时间:2016-06-12 11:49:17

标签: php mysql mysqli

 <div class = "col-md-9 text-left">
    <?php
    $host = 'localhost';
    $dbname   = 'project';
    $username = 'root';
    $password = '1234';
    $charset = 'utf8';


    try 
    {
        $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);     
        $sql = "SELECT subject,description,time,date FROM status";       
        $q = $pdo->query($sql);      
        $q->setFetchMode(PDO::FETCH_ASSOC);
        $usid = ($row['userID']);
        $sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname 
                 FROM status , tbl_users 
                 WHERE status.userID=tbl_users.userID ORDER BY status.time DESC';



        $q1 = $pdo->prepare($sql1);
        $q1->execute([$usid]);
        $q1->setFetchMode(PDO::FETCH_ASSOC);        

    } 
    catch (PDOException $e) 
    {
        die("Could not connect to the database $dbname :" . $e->getMessage());
    }

    ?>




    <?php while ($row = $q->fetch()): ?>
    <?php while ($row1 = $q1->fetch()): ?>
    <div class="col-md-9">
        <div class="box box-widget">
            <div class="box-header with-border">
                <div class="user-block">
                    <img class="img-circle" src="<?php echo $row10['des']; ?><?php echo $row9['userPic']; ?>" alt="User Image">                  
                    <span class="username"><a href="#"><?php echo htmlspecialchars($row1['Fname']); ?>&nbsp;<?php echo htmlspecialchars($row1['Lname']); ?></a></span>
                    <span class="description">Shared publicly - <?php echo htmlspecialchars($row['time']) ?>&nbsp; <?php echo htmlspecialchars($row['date']) ?></span>
                </div>
                  <!-- /.user-block -->
                <div class="box-tools">
                    <button type="button" class="btn btn-box-tool" data-toggle="tooltip" title="Mark as read">
                      <i class="fa fa-circle-o"></i>
                    </button>
                    <button type="button" class="btn btn-box-tool" data-widget="collapse">
                      <i class="fa fa-minus"></i>
                    </button>
                    <button type="button" class="btn btn-box-tool" data-widget="remove">
                      <i class="fa fa-times"></i>
                    </button>
                </div>
                  <!-- /.box-tools -->
            </div>
                <!-- /.box-header -->
            <div class="box-body">     
                <p><b><?php echo htmlspecialchars($row1['subject']) ?></b></p>
                <p><i><?php echo htmlspecialchars($row1['description']) ?></i></p>



    <?php

    // Check connection



    $servername = "localhost";
    $username = "root";
    $password = "1234";
    $dbname = "project";

    htmlspecialchars($a = $row1['stno']);

    $d1 = $row7['userID'];

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "SELECT * FROM likes WHERE rec = $a";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) 
    {
        // output data of each row
        while($row = $result->fetch_assoc()) 
        {
            $GLOBALS['a'] = $row['do'];      
        } 
    }  
    $z4 = $GLOBALS['a'];
    if ($d1==$z4)  
    {
        include ("unlikee.php");
    }
    else 
    {
        include ("likee.php");
    }
    $conn->close();

    ?>
    <span class="pull-right text-muted"><?php
    $con=mysqli_connect("localhost","root","1234","project");
    // Check connection
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }


    htmlspecialchars($a = $row1['stno']);

    $sql="SELECT * FROM likes WHERE rec = $a";

    if ($result=mysqli_query($con,$sql))
    {
      // Return the number of rows in result set
      $rowcount=mysqli_num_rows($result);
      printf("%d\n",$rowcount);
      // Free result set
      mysqli_free_result($result);
    }

    mysqli_close($con);
    ?>
    </h5>

    <span class="description-text"><?php
    $con=mysqli_connect("localhost","root","1234","project");
    // Check connection
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }


    htmlspecialchars($a = $row1['stno']);

    $sql="SELECT * FROM likes WHERE rec = $a";

    if ($result=mysqli_query($con,$sql))
    {
      // Return the number of rows in result set
      $rowcount=mysqli_num_rows($result);

      if ($rowcount==1)
          echo 'Like';
      else
          echo 'Likes';

      // Free result set
      mysqli_free_result($result);
    }

    mysqli_close($con);
    ?>   - 3 comments</span>


    </div>
                <!-- /.box-body -->

                <!-- /.box-footer -->
                <div class="box-footer">
                  <form action="#" method="post">
                    <img class="img-responsive img-circle img-sm" src="../dist/img/user4-128x128.jpg" alt="Alt Text">
                    <!-- .img-push is used to add margin to elements next to floating images -->

                  </form>
                </div>
                <!-- /.box-footer -->
              </div>
              <!-- /.box -->
                </div>




        <?php endwhile; ?> <?php endwhile; ?>
    </div>

我想只检索一个USER的数据,但我不知道如何在SQL语句中为它提供条件。我可以在何处以及如何放置WHERE userID = $user_Session

$sql = "SELECT subject,description,time,date FROM status";

在上面的两个代码语句中,我应该放在第一个?

$sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname 
FROM status , tbl_users 
WHERE status.userID=tbl_users.userID ORDER BY status.time DESC';

2 个答案:

答案 0 :(得分:0)

$sql1 = 
 'SELECT 
    status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname 
 FROM 
      status , tbl_users 
 WHERE 
      status.userID=tbl_users.userID 
      AND [correct_table_name].userID = $user_Session #  here with AND instead WHERE
 ORDER BY 
      status.time DESC';

答案 1 :(得分:0)

这是我添加参数UID的代码

 try 
    {
        $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);     
        $sql = "SELECT subject,description,time,date FROM status";       
        $q = $pdo->query($sql);      
        $q->setFetchMode(PDO::FETCH_ASSOC);
        $usid = ($row['userID']);
        $sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname 
                 FROM status , tbl_users 
                 WHERE status.userID=tbl_users.userID and tbl_users.userID = :UID ORDER BY status.time DESC';



        $q1 = $pdo->prepare($sql1);
        $q1->bindParam(':UID', $usid, PDO::PARAM_INT);  //call with param
        $q1->execute();
        $q1->setFetchMode(PDO::FETCH_ASSOC);        

    }