[英]way to check for if sql row exists?
我的问题是
我正在通过它获取一个mysql行
$sql_istorrenthere = $this->query_silent("SELECT media_type
FROM " . DB_PREFIX . "auction_media WHERE
auction_id='" . $item_details['auction_id'] . "'");
$row = mysql_fetch_array($sql_istorrenthere);
然后用这个来称呼它
if ($row['media_type'] == 4)
{
$display_output = GMSG_TORRENT;}
else
{
$display_output = GMSG_NOTORRENT;
}
}
但是,media_type有多个值,(1,2,3,4)如何编写它以便检查是否存在4? 因为现在我相信它正在检查media_type是否等于4,那是错误的,这给了我错误的display_output
您可以使用mysql_num_rows确定是否返回了任何行,这可以通过在查询中添加搜索条件并在末尾添加“ AND media_type = 4”来实现
if(mysql_num_rows($sql_istorrenthere)) {
} else {
}
// You can loop through records by doing the following, this prints out every media type :)
while ($row = mysql_fetch_array($sql_istorrenthere)) {
echo $row['media_type'] . '<br />';
}
// Comment the next line after you get what it is
@print ("Value of media type is: >>>".$row['media_type']."<<<"); // Line to be commented
if (isset($row['media_type']) && $row['media_type'] == 4) {
$display_output = GMSG_TORRENT;
}
else {
$display_output = GMSG_NOTORRENT;
}
要获取所有媒体类型:
<?php
$sql_istorrenthere = $this->query_silent("SELECT media_type FROM " . DB_PREFIX . "auction_media");
while ($row = mysql_fetch_array($sql_istorrenthere)) {
// Comment the next line after you get what it is
@print ("Value of media type is: >>>".$row['media_type']."<<<"); // Line to be commented
if (isset($row['media_type']) && $row['media_type'] == 4) {
$display_output = GMSG_TORRENT;
}
else {
$display_output = GMSG_NOTORRENT;
}
}
您可以在查询中添加“ AND media_type ='4'”。 但是您确实应该使用参数化查询。
查询查询为“ AND media_type ='4'”后,您可以检查RowCount。
可能有更好的方法,但这是一个想法。
$media_type_ids = explode(',', $row['media_type']);
if (array_search(4, $media_type_ids) !== FALSE) {
// found
}
甚至有可能在数据库查询中就地执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.