简体   繁体   English

在PHP中选择特定的SQL行

[英]Selecting specific sql rows in php

I'm trying to access a database table, this table is going to have 100+ records for which I would like to pick specific rows (through their unique id). 我正在尝试访问数据库表,该表将具有100多个记录,我希望为其选择特定的行(通过其唯一ID)。 Here is some of the php code I have... 这是我拥有的一些php代码...

<?php

function connect() {
  $conn = new mysqli('localhost', 'root', 'pass', 'db') or die('There was a problem connecting to the db');
  return $conn;
}

function get($conn) {
  $stmt = $conn->prepare("SELECT * FROM camera") or die('There is a problem with the connection');
  $stmt->execute();
  $stmt->bind_result($id, $camera_id, $name, $location, $camera_status, $contact_name, $contact_phone);

  $rows = array();

  while($row = $stmt->fetch()) {
    $item = array(
             'id' => $id,
      'camera_id' => $camera_id,
           'name' => $name,
       'location' => $location,
  'camera_status' => $camera_status,
   'contact_name' => $contact_name,
  'contact_phone' => $contact_phone
);
$rows[] = $item;
  }
  return $rows;
}

$conn = connect();
$results = get($conn);

?>

There will be 9 results per page, these results have to be coded in manually. 每页有9个结果,这些结果必须手动编码。 I am able to display all results in the database but I would like to be able to pick 9 unique results, display the contents of the row as well as offering a way to edit the entry. 我能够显示数据库中的所有结果,但是我希望能够选择9个唯一的结果,显示行的内容以及提供一种编辑条目的方法。 The unique identifier will be their $id. 唯一标识符将是其$ id。

What would be the easiest way to select these rows in php? 在php中选择这些行的最简单方法是什么?

Thanks in advance! 提前致谢!

J. J.

use limit in query for specific rows like this example 在查询中对特定行使用limit ,例如本例

SELECT * FROM camera limit 0,9

http://dev.mysql.com/doc/refman/5.0/en/select.html http://dev.mysql.com/doc/refman/5.0/en/select.html

Use limits 使用限制

$start= 0; $end=9; //you can make $start =10; $end=9; for next run and so on..
SELECT * FROM camera limit "'.$start.'","'.$end.'"

You can use limit to select the lines you want after sorting by id with order by . 您可以使用limit通过ID与排序后,选择您想要的线order by First limit 0, 9 then on the second load use limit 9,9 , and so forth. 第一个limit 0, 9然后在第二个负载使用limit 9,9 ,依此类推。

Limit should be after where clause. 限制应在where子句之后。

Syntax : 句法 :

SELECT column_name(s)
FROM table_name
[WHERE]
LIMIT number;

http://www.postgresql.org/docs/9.0/static/sql-select.html http://www.postgresql.org/docs/9.0/static/sql-select.html

I made the entire script load results dynamically (with foreach loop) instead of having to manually add them. 我动态创建了整个脚本加载结果(使用foreach循环),而不必手动添加它们。 Originally I wanted to add the rows manually because the video streams will be limited to a per client basis, so I just added an extra database column to get around this issue. 最初,我想手动添加行,因为视频流仅限于每个客户端,因此我只是添加了一个额外的数据库列来解决此问题。

I'm still limited to 9 feeds per page so I will be using the limit feature for that, thanks for the help and feedback! 我仍然限制为每页9个提要,因此,我将为此使用limit功能,感谢您的帮助和反馈!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM