[英]Get data from <td> on the same table row specified by row id and perform computation
[英]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.