如何通过查询同一行中的另一个值来返回特定 mysql 单元中的内容?

发布于 2024-11-09 12:21:45 字数 580 浏览 0 评论 0原文

我正在尝试制作一个可以导航到的 php 页面,例如... http://mydomain.com?id =12345

在我的mysql表中有一个id列和一个text列....我怎样才能让我的php页面获取ID,找到它,然后返回同一行中文本单元格中的内容并回显它在页面上吗?

这是我到目前为止所想到的……我主要停留在我应该如何处理 Mysql 查询上。然后如何实际将数据放入可以回显到页面的变量中。谢谢!

编辑:取得了一些进展......

    <?php 

    mysql_connect("my.mysql.com", "user", "pass");
    mysql_select_db("mydb");

    $id= $_GET['id'];

   $result = mysql_query("SELECT text FROM mytable WHERE id='$id'")
or die(mysql_error());  


echo nl2br($result);


    ?>

I'm trying to make a php page that i can navigate to like... http://mydomain.com?id=12345

in my mysql table there is a id column and a text column....how can I make my php page get the ID, find it, then return whats in the text cell in the same row and echo it on the page?

Here's what i've come up with so far..i'm mostly stuck on what I should do with the Mysql query. then how to actually get the data into a variable that I can echo to the page. Thanks!

EDIT: made a little progress...

    <?php 

    mysql_connect("my.mysql.com", "user", "pass");
    mysql_select_db("mydb");

    $id= $_GET['id'];

   $result = mysql_query("SELECT text FROM mytable WHERE id='$id'")
or die(mysql_error());  


echo nl2br($result);


    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ら栖息 2024-11-16 12:21:45

构建查询后,将其传递到数据库并获取结果


// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
    echo $row['field1'];
    echo $row['field2'];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);

重要说明:在将数据输入数据库之前,应始终对数据进行清理,以避免 sql 注入

例如:想象一下有人把“'; drop table mytable;”作为 url 中的 id。然后将其传递给 mysql 将删除您的表。

注意2:输出帖子时,请确保转义某些字符:您应该输入&而不是<>。 lt>

推荐教程

此处复制的脚本

Right after you've constructed your query, you pass it to the database and fetch the results


// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
    echo $row['field1'];
    echo $row['field2'];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);

Important note: your data should always be sanitised before inputting it in a database, to avoid sql injection

Eg.: imagine someone put "'; drop table mytable;" as the id in the url. Then passing this to mysql would delete your table.

Note2: when outputting your post, make sure to escape certain characters: instead of < and > you should put < and >

Recommended tutorial

Script copied from here

难理解 2024-11-16 12:21:45

指定 SELECT 之后要提取的字段

SELECT field1, field2
  FROM mytable
  WHERE id=:id

Specify the fields to extract after SELECT

SELECT field1, field2
  FROM mytable
  WHERE id=:id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文