php SELECT * FROM Table Where ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17438313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:53:13  来源:igfitidea点击:

SELECT * FROM Table Where ID

phpmysql

提问by qweqweqwe

I am trying to retrieve information from my database depending on the ID a user types into my URL.

我正在尝试根据用户在我的 URL 中键入的 ID 从我的数据库中检索信息。

For example: If USER A went to www.exampleurl.com/index.php?id=1 it would echo out the user's information which has an ID of 1. Same thing if the id was 2, 3, etc. Users are entering their information via a form in a different file called submit.php.

例如:如果用户 A 访问 www.exampleurl.com/index.php?id=1,它会回显用户的 ID 为 1 的信息。如果 id 为 2、3 等,则相同。用户正在输入他们的信息是通过一个名为 submit.php 的不同文件中的表单进行的。

Here is my code to retrieve data depending on ID :

这是我根据 ID 检索数据的代码:

<?php
    $id = $_GET['id'];

        //Variables for connecting to your database.
        $hostname = "";
        $username = "";
        $dbname = "";
        $password = "";
        $usertable = "";

        //Connecting to your database
        $con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to 
        connect to database! Please try again later.");
        mysql_select_db($dbname, $con);

        $query = "SELECT * FROM $usertable WHERE id = $id LIMIT 1";
        $result = mysql_query($query, $con);

    echo "Hello, " . $result['name'];

 ?> 

Any ideas on if my SELECT request is wrong?

关于我的 SELECT 请求是否错误的任何想法?

EDIT

编辑

Here is my code for showing the data altogether in a table. This works fine.

这是我在表格中完全显示数据的代码。这工作正常。

 <?php
        //Variables for connecting to your database.
        $hostname = "";
        $username = "";
        $dbname = "";
        $password = "!";
        $usertable = "";

        //Connecting to your database
        $con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to 
        connect to database! Please try again later.");
        mysql_select_db($dbname, $con);

        //Fetching from your database table.
        $query = "SELECT * FROM $usertable";
        $result = mysql_query($query, $con);

        echo "<table border=1>
        <tr>
        <th> ID </th>
        <th> Name </th>
        <th> Age </th>

        </tr>";

        while($record = mysql_fetch_array($result)) {
            echo "<tr>";
            echo "<td>" . $record['id'] . "</td>";
            echo "<td>" . $record['name'] . "</td>";
            echo "<td>" . $record['age'] . "</td>";
            echo "</tr>";
        }

        echo "</table>";
 ?>

回答by BIT CHEETAH

→ Try This:

→ 试试这个:

You should consider using PHP PDO as it is safer and a more object oriented approach:

您应该考虑使用 PHP PDO,因为它更安全且更面向对象:

$usertable = "";
$database  = new PDO( 'mysql:host=localhost;dbname=DB_NAME', 'DB_USER_NAME', 'DB_USER_PASS' );

$statement = $database->prepare('SELECT * FROM $usertable');
$statement->execute();

$count = $statement->rowCount();

if( $count > 0 ) {

     $R = $statement->fetchAll( PDO::FETCH_ASSOC );

     for( $x = 0; $x < count($R); $x++ ) {

        echo "<tr>";
        echo "<td>" . $R[ $x ]['id'] . "</td>";
        echo "<td>" . $R[ $x ]['name'] . "</td>";
        echo "<td>" . $R[ $x ]['age'] . "</td>";
        echo "</tr>";

     }

}
else { echo "Error!"; }

回答by Miguel Borges

you need to use mysql_fetch_assoc functionfor retrieve the results.

您需要使用mysql_fetch_assoc 函数来检索结果。

 $result = mysql_fetch_assoc(mysql_query($query, $con));    
 echo "Hello, " . $result['name'];

回答by immulatin

You should be error checking your mysql_querys:

您应该在检查 mysql_querys 时出错:

$query = "SELECT * FROM $usertable WHERE id = $id LIMIT 1";
$result = mysql_query($query, $con);
if(!result)
    echo mysql_error();

You should also retrieve the results:

您还应该检索结果:

$array = mysql_fetch_assoc($result);

回答by orafaelreis

I'll consider some secure features like

我会考虑一些安全的功能,比如

  1. Check if $_GET['id']is set and if is int

  2. Apply Mysql escape with mysql_escape_string()function

  1. 检查是否$_GET['id']设置,如果是int

  2. mysql_escape_string()函数应用Mysql转义