简体   繁体   中英

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). Here is some of the php code I have...

<?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. 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. The unique identifier will be their $id.

What would be the easiest way to select these rows in php?

Thanks in advance!

J.

use limit in query for specific rows like this example

SELECT * FROM camera limit 0,9

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 . First limit 0, 9 then on the second load use limit 9,9 , and so forth.

Limit should be after where clause.

Syntax :

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

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. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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