可能重复:
How do i “echo” a “Resource id #6” from a MySql response in PHP?
我正在查询查询结果,但它一直给我资源ID#3。
以下是我的代码。
$type = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$typeResult = mysql_query($type);
print_r($typeResult);
我在这里错过了什么步骤?
答案 0 :(得分:2)
您需要获取结果。您所做的只是发送查询。
请注意,如果您要编写新代码,则应使用mysqli_
或PDO
函数,因为您的查询容易受到SQL注入攻击,而mysql_
函数则being deprecated。犹豫不决,下面是mysql_fetch_assoc
的示例。
<?php
$sql = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row[sellingid];
}
mysql_free_result($result);
?>
答案 1 :(得分:0)
$type = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$typeResult = mysql_query($type);
$row = mysql_fetch_array($typeResult);
print_r($row);
答案 2 :(得分:0)
您实际上并未获取查询结果。下面是两个使用WHILE循环将结果作为行获取的示例。然后,您可以获取列值并使用它们。
方法不正确且已弃用,但有效:
$type = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$typeResult = mysql_query($type);
// for each row
while ($row = mysql_fetch_array($typeResult)) {
// grab the columns
$value = $row['column_name'];
}
我建议使用MySQLi或PDO跟随(MySQLi):
$mysqli_connection = new mysqli("hostname", "username", "password", "database");
$type = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$res = $mysqli_connection->query($type);
while($row = $res->fetch_array(MYSQLI_ASSOC)) {
$value = $row['column_name'];
}
$res->free();
$mysqli_connection->close();
答案 3 :(得分:0)