我尝试从合并表中的特定行获取存储在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号指定的行中获取数据?
答案 0 :(得分:2)
您已尝试对另一个mysql_query
的结果执行mysql_query
!
让我们假设您的SQL目前是正确的,并处理其余代码。首先,您需要使用MySQLi或PDO,因为mysql
扩展名已弃用。所以在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'];
}
请注意?
所准备的SQL语句中的$id
。这是变量的占位符,然后与$stmt->bind_param('i',$id);
绑定(i
表示整数,s
将用于字符串)。然后,您必须执行结果并获得结果集,然后才能对其执行任何操作。
如果您的SQL出错,则错误将输出到浏览器,查询将无法执行。
希望这有帮助。