我正在寻找查询的结果,但它一直给我资源 id #3。
以下是我的代码。
$type = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$typeResult = mysql_query($type);
print_r($typeResult);
我在这里缺少什么步骤?
我正在寻找查询的结果,但它一直给我资源 id #3。
以下是我的代码。
$type = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$typeResult = mysql_query($type);
print_r($typeResult);
我在这里缺少什么步骤?
您需要获取结果。您所做的就是发送查询。
请注意,如果您正在编写新代码,则应该使用mysqli_
orPDO
函数,因为您的查询容易受到 SQL 注入的攻击,并且mysql_
函数正在被弃用。犹豫不决,下面是一个示例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);
?>
$type = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$typeResult = mysql_query($type);
$row = mysql_fetch_array($typeResult);
print_r($row);
您实际上并没有获取查询结果。下面是两个使用 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();
更清晰的提示 - 使用 MySQLi 类/函数,请阅读:
http://lt1.php.net/manual/en/mysqli-result.fetch-assoc.php
或者如果你更喜欢 OOP 方法