Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中从mysqi查询中获取前一行_Php_Mysql_Mysqli - Fatal编程技术网

在php中从mysqi查询中获取前一行

在php中从mysqi查询中获取前一行,php,mysql,mysqli,Php,Mysql,Mysqli,我在MySQL中有一个名为published_people的视图,如下所示: PersonID Name LastName MarkerID date -------- ---- -------- -------- ---- 1198 Jane Doe Doe 1174 2015-05-20 864 John Doe Doe 863 2015-04-23 1187 Richar

我在MySQL中有一个名为published_people的视图,如下所示:

PersonID Name          LastName MarkerID date
-------- ----          -------- -------- ----
1198     Jane Doe      Doe      1174     2015-05-20
 864     John Doe      Doe       863     2015-04-23
1187     Richard Roe   Roe      1165     2015-05-21
1190     Sam Spade     Spade    1167     2015-01-01
我有一个post变量,表示我正在查看其数据页的人的标记ID

我还有另一个post变量,它表示我正在查看其数据页的人的姓氏

我希望能够反复浏览已发表的文章。如果LastName字段与变量匹配,我希望在published_people中获取之前的记录

以下是我目前使用php编写的代码:

include_once ('constants_test.php'); 
$mysqli_prior = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
if (mysqli_connect_errno()) {
            printf("Connect failed: %s", mysqli_connect_error());
            exit();
}

//get the year I'm looking for
$this_date = mysqli_real_escape_string($mysqli_prior, $_POST['this_date']);
$pieces = explode("-", $this_date);
$this_year = $pieces[0];

//find the last name of the person I'm looking for  
$marker_id = $_POST['marker_id'];
$q_getLastName = "select LastName from published_people where MarkerID =" . $marker_id;
    $result = mysqli_query($mysqli_prior,$q_getLastName);
    $r = $result->fetch_row();
    $thisLastName = $r[0];

    //get all records from this year, alphabetized by last name
    $q = "select * from published_homicides where year(date) = '" . $this_year . "' order by LastName";
    $result = $mysqli_prior->query($q);
     $allresults = array();
      $num_rows = mysqli_num_rows($result);

      if ($num_rows != 0) {
         while($row = $result->fetch_array(MYSQLI_ASSOC)) {

            // How do I say this?
            // if $row["LastName"] == $thisLastName then find the record 
            // PRIOR TO this one and do the following:

            $results = array($row['Name'],  $row['date']);
            array_push($allresults, $results);
         }
         echo json_encode($allresults);
      } else {
         echo "nothing";
      }
      mysqli_close($mysqli_prior);

我最终创建了一个变量来保存上一条记录中的数据。我遍历了查询。当MarkerID等于我想要的上一条记录的marker_id时,我停止:

$priorname = ""; //initialize at nothing
//we'll go through the list of names in alphabetical order by year
  while($row = $result->fetch_array(MYSQLI_ASSOC)) {
        //compare the row id to the posted id
        if ($row['MarkerID'] == $marker_id) {
            $results = array($priorname);
            array_push($allresults, $results);
        } else {
            $priorname = $row['Name']; //save this data in $priorname
        }


     }
     echo json_encode($allresults);

  mysqli_close($mysqli_prior);
这就是之前的记录

我还想获得下一张唱片

我分两部分处理这个问题。首先,我通过我的询问,数一数我所坐的那一排。当我的post变量marker_id与数据库视图中的MarkerID匹配时,我停止了查询:

$marker_id = $_POST['marker_id'];

$q = "select MarkerID, Name, LastName, date from published_people order by year(date) desc, LastName asc";

 $result = $mysqli_next->query($q);
 $allresults = array();

$count = 0;
//we'll go through the list of names in alphabetical order by year

while($row = $result->fetch_array(MYSQLI_BOTH)) {
    $count++; //keep track of what row you're on
    //compare the row id to the posted id
    if ($row['MarkerID'] == $marker_id) {
        //if they're the same, stop this query - we have counted to the spot that they matched
        break;
    } 
}
现在我知道在哪里设置另一个查询的限制:

//make a new query with a limit of one record starting at the row # indicated by $count
$newq = "select MarkerID, Name, LastName, date from published_homicides order by year(date) desc, LastName asc LIMIT " . $count . ",1"; 
$result2 = $mysqli_next->query($newq);
while($row2 = $result2->fetch_array(MYSQLI_ASSOC)) {
    $results = array($row2["Name"]);
    array_push($allresults, $results);
}       
echo json_encode($allresults);

mysqli_close($mysqli_next);

基于什么排序顺序的先前记录?视图的默认顺序,如上所述,它是按year desc和LastName asc排序的。