我正在尝试制作一个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);


    ?>

最佳答案

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


// 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注入

例如:假设有人放了“';放下桌子mytable;”作为网址中的ID。然后将此传递给mysql将删除您的表。

注意2:输出文章时,请确保转义某些字符:您应输入&lt和​​&gt而不是

Recommended tutorial

here复制的脚本

10-02 00:44
查看更多