SQLi和检索特定记录 [英] SQLi and retreiving a specific record

查看:49
本文介绍了SQLi和检索特定记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环顾四周,看到了很多MySQL答案,但没有MySQLi .. 我试图返回我选择的1行. 目前,我只能返回第一行.

looked around, saw a lot of MySQL answers but not MySQLi.. Im attempting to return 1 line of my choosing. at the moment I can return only the first line.

我要去的是,用ID链接我的主数据库,当您单击ID时,仔细查看记录在另一页上.

What im trying to get to is, have my main database be linked by ID, when you click the ID, a closer look at the record is on another page..

<?php

$connect = mysqli_connect("localhost", "root", "", "mydb");
$query = "SELECT name, surname FROM info ORDER BY id";
$record = mysqli_query($connect, $query);
@$num_results = mysqli_num_rows($record);

$row = mysqli_fetch_assoc($record);

$fname = $row['name'];
$surname  = $row['surname'];

print $fname;
print $surname;



?>

推荐答案

为了执行您要的操作,首先创建一个用户列表:

In order to do what you're asking, first create a list of users:

$connect = mysqli_connect("localhost", "root", "", "mydb");
$query = "SELECT name, surname FROM info ORDER BY id";
$record = mysqli_query($query, $connect);

while($row = mysqli_fetch_assoc($record)){
    $user = $row['name'] . ' ' . $row['surname'];
    echo '<a href="user.php?uid=' . $row['id'] . '">' .$user . '</a></br>';
}

这将创建一个所有用户的列表,如下所示:

The will create a list of all your users which look like:

<a href="user.php?uid=1">Bart Simpson</a></br>
<a href="user.php?uid=2">Matt Damon</a></br>

以此类推.

当您单击原始页面中的用户链接时,该链接应由user.php中的代码处理:

When you click the user's link in the original page, it should be processed by the code in user.php:

$connect = mysqli_connect("localhost", "root", "", "mydb");
$query = "SELECT name, surname FROM info WHERE id = ?"; // returns one line identified by id - you can use something else if you're guarateed the value is unique in your table
$stmt = mysqli_prepare($connect, $query);
mysqli_stmt_bind_param($stmt, 'i', $_GET['uid']);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $name, $surname);
mysqli_stmt_fetch($stmt);

我敢打赌,您可以猜出现在发生了什么,不是吗?没错,您可以在此页面上回显单个用户的数据:

I'll bet you can guess what happens now, can't you? That's right, you can echo out the data for the individual user on this page:

$user = $name . ' ' . $surname;
echo $user;

注意:

  1. 连接代码可以放在单独的文件中,并在需要的情况下包含在页面中.
  2. 您可以编写一个函数来处理您编写的每个查询.
  3. 为了防止SQL注入的可能性,我为 prepared 语句href ="http://php.net/manual/en/mysqli.quickstart.prepared-statements.php" rel ="nofollow noreferrer"> MySQLi .甚至转义字符串也不安全!
  4. 通常,我的编码会更加一致,每次都以相同的方式执行查询.这样做将减少故障排除时间,并使您的代码更易于他人阅读.
  1. The connection code could be placed in a separate file and included in pages where needed.
  2. You could write a function to handle every query you write.
  3. In order to prevent the possibility of SQL Injection I have used prepared statements for MySQLi. Even escaping the string is not safe!
  4. Generally I would be a lot more consistent with my coding, performing queries the same way each and every time. doing so will reduce troubleshooting time as well as making your code easier for others to read.

这篇关于SQLi和检索特定记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆