[英]How to retrieve details for a particular id from MYSQL database
Getting data from the database depending on the value of the id in the URL. 根据URL中id的值从数据库获取数据。
$id= $_GET['id'];
$strSQL = "SELECT * FROM people WHERE id='$id'";
$rs = mysql_query($strSQL);
This is working, But it's displaying a blank page. 这是可行的,但它显示的是空白页。
How can i fix this ? 我怎样才能解决这个问题 ?
[1]. [1]。 mysql_*
is deprecated. mysql_*
已弃用。
[2]. [2]。 Your code is VERY vulnerable to sql injection. 您的代码非常容易受到sql注入的攻击。 Sanitize $_GET['id']
with mysql_real_escape_string($_GET['id'])
( mysqli
if you update to that) 用mysql_real_escape_string($_GET['id'])
消毒$_GET['id']
(如果更新为mysqli
则为mysqli
)
[3]. [3]。 The results are returned as resources. 结果作为资源返回。 Loop through it with: 使用以下命令循环遍历:
$rs = mysql_query($strSQL);
while($r = mysql_fetch_array($rs)) { echo $r['someField']; }
I don't recommend using mysql_query since it is deprecated. 我不建议使用mysql_query,因为它已被弃用。
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. 自PHP 5.5.0起不推荐使用此扩展,以后将删除。
I recommend using MySQLi or PDO . 我建议使用MySQLi或PDO 。
But if you insist on using the mysql_
functions you can use mysql_fetch_assoc to get the details returned in an associative array key'd on field names. 但是,如果您坚持使用mysql_
函数,则可以使用mysql_fetch_assoc来获取在字段名称的关联数组键中返回的详细信息。
$id= $_GET['id'];
$strSQL = "SELECT * FROM people WHERE id='$id'";
$rs = mysql_query($strSQL);
if (!$rs) {
echo 'Could not run query: ' . mysql_error(); //A bit of error checking
exit;
}
$row = mysql_fetch_assoc($rs);
if($row){
echo $row['field']; // An array key'd on the table's field names is returned
}
If a field in your people
table is named first_name
then that value would be stored in $row['first_name']
in the returned associative array. 如果您的people
表中的字段名为first_name
则该值将存储在返回的关联数组中的$row['first_name']
中。
I assume id
is a unique field and at most you expect one row returned. 我假设id
是一个唯一字段,并且您最多希望返回一行。 If that is not the case use a loop with the condition while($row = mysql_fetch_assoc($rs))
to loop through all returned rows. 如果不是这种情况,请使用条件为while($row = mysql_fetch_assoc($rs))
的循环遍历所有返回的行。
As I mentioned in my original comment: 正如我在原始评论中提到的:
"You need to use a loop in order to fetch the data, then echo it." “您需要使用循环来获取数据,然后回显它。”
Others have given you examples and not to use your present method, here is a PDO method: 其他人给了您示例,而不使用您现有的方法,这是PDO方法:
<?php
$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';
$id = $_GET['id'];
try {
$pdo= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
exit( $e->getMessage() );
}
try {
$stmt = $pdo->prepare('SELECT * FROM people WHERE id = :id');
$stmt->bindParam(':id', $id);
$stmt->execute();
} catch (PDOException $e) {
echo $e->getMessage();
}
/* optional method
foreach($stmt as $row) {
echo $row['id'] . ' ' . $row['name'] . ' ' . $row['email'];
}
*/
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$id = $row['id'];
$n = $row['name'];
$e = $row['email'];
}
echo $id;
echo "<br>";
echo $n;
echo "<br>";
echo $e;
?>
Another PDO method: 另一种PDO方法:
<?php
$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';
$id = $_GET['id'];
// $id= 4; // my own test id number
try {
$pdo= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
exit( $e->getMessage() );
}
try {
$sql = "SELECT * FROM people WHERE id='$id'";
$results = $pdo->query($sql);
} catch (PDOException $e) {
echo $e->getMessage();
}
/* optional method
foreach($results as $row) {
echo $row['id'] . ' ' . $row['name'] . ' ' . $row['email'];
}
*/
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$id = $row['id'];
$n = $row['name'];
$e = $row['email'];
}
echo $id;
echo "<br>";
echo $n;
echo "<br>";
echo $e;
?>
You have to fetch result. 您必须获取结果。
try like this: 尝试这样:
$id= $_GET['id'];
$strSQL = "SELECT * FROM people WHERE id='$id'";
$rs = mysql_query($strSQL);
while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
echo $row['id']; //$row is an array of each row of your table
}
mysql_*
functions are deprecated you should switch to mysqli_*
or pdo
不建议使用mysql_*
函数,应切换到mysqli_*
或pdo
$id= $_GET['id'];
$strSQL = "SELECT * FROM people WHERE id='$id'";
$rs = mysql_query($strSQL);
$row = mysql_fetch_array($rs);
$num_rows = mysql_num_rows($row);
if($num > 0)
{
foreach($row as $r)
{
echo $r;
}
}
else
{
echo "No rows "
}
This code will display the values . 此代码将显示值。 Note: mysql_query will be deprecated in later version of PHP. 注意:在更高版本的PHP中将不建议使用mysql_query。 So better start with mysqli or PDO's in PHP. 因此最好从PHP中的mysqli或PDO开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.