简体   繁体   English

如何在mysql中根据其ID检索行的特定数据?

[英]How to retrieve particular data of a row based on its id in mysql?

I would like to know how to retrieve the title of a row based on its id. 我想知道如何根据其ID检索行的标题。

Its for href link based of next and previous buttons. 它的href链接基于下一个和上一个按钮。 The id will be determined by 该ID将由

current id-1= previous id ($pid) and current id+1=next id ($nid) 当前ID-1 =上一个ID($ pid)和当前ID + 1 =下一个ID($ nid)

I have $nid and $pid , but now i want to get the title of that respective id in the database. 我有$nid$pid ,但是现在我想在数据库中获取相应ID的标题。 Now i am getting only the current id title displayed in the url.. 现在我只在URL中显示当前的ID标题。

href="features/headlines/<?php echo $pid; ?>/<?php echo $title; ?>"

i want the $title to show the title corresponding to the $pid but now i am only getting the title of the current id. 我希望$title显示与$pid对应的标题,但现在我只获取当前ID的标题。

I tried <?php echo $pid[title]; ?> 我试过<?php echo $pid[title]; ?> <?php echo $pid[title]; ?>

It would be something to the effect of: 将会产生以下效果:

<?php

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$current_id = 6; // Whatever your current ID is here..

// Notice the subtraction from $current_id
$query = "SELECT `title` FROM `table` WHERE `id` = '".($current_id-1)."'";
// Would output:
// SELECT `title` FROM `table` WHERE `id` = '5'

$result = $mysqli->query($query);
$row = $result->fetch_assoc();

$previous_title = $row['title'];

?>

See: http://php.net/manual/en/mysqli-result.fetch-assoc.php 参见: http : //php.net/manual/en/mysqli-result.fetch-assoc.php

You are trying to use a normalized database schema. 您正在尝试使用规范化的数据库架构。 This is good! 很好! (unless you are talking about some abstract data type in PHP) (除非您正在谈论PHP中的某些抽象数据类型)

If I'm understanding you correctly, you need a special sort of "key" on your column, specifically the primary key with the attribute AUTO_INCREMENT 如果我对您的理解正确,则您的列上需要一种特殊的“键”,特别是具有属性AUTO_INCREMENT的主键

How to make MySQL table primary key auto increment with some prefix 如何使MySQL表主键自动增加一些前缀

MySQL can solve any selection based on its id in O(logn) time using a simple BTREE index. MySQL可以使用简单的BTREE索引基于O(logn)时间中的id来解决任何选择。 Primary keys are keys that basically are a unique representation of each row (in this case, the id is the represenation of title). Primary keys是基本上代表每一行的唯一键(在这种情况下,id是标题的代表)。 You would do a query like this: 您将执行以下查询:

SELECT id, title from table_1 WHERE id = '$id_to_select'

To parse the return result, you have an array of choices: mysqli_fetch_row mysqli_fetch_array() mysqli_fetch_assoc 要解析返回结果,您可以选择一个数组: mysqli_fetch_row mysqli_fetch_array() mysqli_fetch_assoc

I think the best one is mysqli_fetch_array since you can specify how to fetch the returning row: MYSQLI_NUM denotes the index, MYSQLI_ASSOC denotes an associative return. 我认为最好的是mysqli_fetch_array因为您可以指定如何获取返回行: MYSQLI_NUM表示索引, MYSQLI_ASSOC表示关联返回。

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
print_r($row);
//Array(
    'id' => 1,
    'title' => 'lorem ipsum'
)

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

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