繁体   English   中英

从ID指定的合并表中获取特定的数据行

[英]Getting a specific row of data from a merged table specified by an ID

我尝试从合并表的特定行中获取存储在SQL中的数据

这是我到目前为止尝试过的

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

$getdetails = mysql_query("SELECT 
scholarship.scholarshipid,
scholarship.scholarshipname, 
scholarship.shortdescription, 
scholarship.scholarshippicture,

scholarshipdetail.fulldescription,
scholarshipdetail.degreetype,
scholarshipdetail.location,
scholarshipdetail.deadline
FROM scholarship, scholarshipdetail
WHERE scholarship.scholarshipid = scholarshipdetail.scholarshipid AND scholarship.scholarshipid = $id ");

$retval = mysql_query( $getdetails, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}

?>

id是从theurl.php?id = IDNUMBER获得的,但事实证明它无法获取数据。 如何从PHP的ID号指定的行中获取数据?

你试图执行mysql_query从另一个结果mysql_query

让我们假设您的SQL目前是正确的,并且可以处理其余的代码。 首先,由于不推荐使用mysql扩展,因此您需要使用MySQLi或PDO。 因此在MySQLi中;

$mysqli = new mysqli('host', 'user', 'password', 'db'); // fill in your details

$id = $_GET['id'];
if($stmt = $mysqli->prepare("SELECT 
scholarship.scholarshipid,
scholarship.scholarshipname, 
scholarship.shortdescription, 
scholarship.scholarshippicture,
scholarshipdetail.fulldescription,
scholarshipdetail.degreetype,
scholarshipdetail.location,
scholarshipdetail.deadline
FROM scholarship, scholarshipdetail
WHERE scholarship.scholarshipid = scholarshipdetail.scholarshipid
AND scholarship.scholarshipid = ?")) {

    $stmt->bind_param('i',$id);
    $stmt->execute();
    $result = $stmt->get_result();
}
else {
    echo $mysqli->error;
}

while($row = $result->fetch_assoc()) {
    // do stuff
    // cols stored as $row['col_name'];
}

注意? $id所在的准备好的SQL语句中。 这是变量的占位符,然后与$stmt->bind_param('i',$id);绑定$stmt->bind_param('i',$id); i意思是整数, s将用于字符串)。 然后,您必须执行结果并获取结果集,然后才能对其执行任何操作。

如果您的SQL中有错误,则该错误将输出到浏览器,并且不会执行查询。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM