对于php和mysql,我有点像周末战士。我使用Dreamweaver 8来构建我的代码,但在必要时在TextWrangler中进行一些手动编码。
我有一个包含28列的表,目前有1224行(并且正在增长)。我使用DW8来构建高级搜索页面的代码。它工作得很好,除了,如果一行或多列中的行(其中很多列都有),它将不会检索该记录。这是个问题。我需要忽略NULL值并检索记录。
我发现如果我修改表中的所有记录以获得除NULL之外的其他内容,如空格或破折号,查询将查找记录。输入新记录后,表中会添加新的NULL,这些记录将无法搜索。
这些列必须允许NULL,因为“添加记录”页面会插入此表以及另一个表中,并且它们必须按顺序排列。我发现,如果我将列设置为不接受NULL,则一个表接受数据,另一个表拒绝数据,然后主键关闭同步。
<?php require_once('Connections/mediadb.php'); ?>
<?php $currentPage = $_SERVER["PHP_SELF"]; ?>
if (isset($_GET['pageNum_rsResults'])) {
$pageNum_rsResults = $_GET['pageNum_rsResults'];
}
$startRow_rsResults = $pageNum_rsResults * $maxRows_rsResults;
$varkind_rsResults = "-1";
if (isset($_GET['searchkind'])) {
$varkind_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchkind'] : addslashes($_GET['searchkind']);
}
$varcomments_rsResults = "-1";
if (isset($_GET['searchnotes'])) {
$varcomments_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchnotes'] : addslashes($_GET['searchnotes']);
}
$varrating_rsResults = "-1";
if (isset($_GET['searchrating'])) {
$varrating_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchrating'] : addslashes($_GET['searchrating']);
}
$varloc_rsResults = "-1";
if (isset($_GET['searchlocation'])) {
$varloc_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchlocation'] : addslashes($_GET['searchlocation']);
}
$vardiscnum_rsResults = "-1";
if (isset($_GET['searchdisc_number'])) {
$vardiscnum_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchdisc_number'] : addslashes($_GET['searchdisc_number']);
}
$vardisccnt_rsResults = "-1";
if (isset($_GET['searchdisc_count'])) {
$vardisccnt_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchdisc_count'] : addslashes($_GET['searchdisc_count']);
}
$vartrackcnt_rsResults = "-1";
if (isset($_GET['searchtrack_count'])) {
$vartrackcnt_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchtrack_count'] : addslashes($_GET['searchtrack_count']);
}
$vartracknum_rsResults = "-1";
if (isset($_GET['searchtrack_number'])) {
$vartracknum_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchtrack_number'] : addslashes($_GET['searchtrack_number']);
}
$varyear_rsResults = "-1";
if (isset($_GET['searchyear'])) {
$varyear_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchyear'] : addslashes($_GET['searchyear']);
}
$varmod_rsResults = "-1";
if (isset($_GET['searchmodified'])) {
$varmod_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchmodified'] : addslashes($_GET['searchmodified']);
}
$varadd_rsResults = "-1";
if (isset($_GET['searchdate_added'])) {
$varadd_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchdate_added'] : addslashes($_GET['searchdate_added']);
}
$varrecnum_rsResults = "-1";
if (isset($_GET['searchrecnum'])) {
$varrecnum_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchrecnum'] : addslashes($_GET['searchrecnum']);
}
$vartime_rsResults = "-1";
if (isset($_GET['searchtime'])) {
$vartime_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchtime'] : addslashes($_GET['searchtime']);
}
$vargenre_rsResults = "-1";
if (isset($_GET['searchgenre'])) {
$vargenre_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchgenre'] : addslashes($_GET['searchgenre']);
}
$vargroup_rsResults = "-1";
if (isset($_GET['searchgrouping'])) {
$vargroup_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchgrouping'] : addslashes($_GET['searchgrouping']);
}
$varalbum_rsResults = "-1";
if (isset($_GET['searchalbum'])) {
$varalbum_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchalbum'] : addslashes($_GET['searchalbum']);
}
$varcomp_rsResults = "-1";
if (isset($_GET['searchcomp'])) {
$varcomp_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchcomp'] : addslashes($_GET['searchcomp']);
}
$varartist_rsResults = "-1";
if (isset($_GET['searchartist'])) {
$varartist_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchartist'] : addslashes($_GET['searchartist']);
}
$varname_rsResults = "-1";
if (isset($_GET['searchname'])) {
$varname_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchname'] : addslashes($_GET['searchname']);
}
mysql_select_db($database_mediadb, $mediadb);
$query_rsResults = sprintf("SELECT recnum, name, artist, composer, album, `grouping`, genre, `time`, year, date_modified, date_added, kind, comments, my_rating, location, track_number, track_count, disc_number, disc_count
FROM media_lib
WHERE name LIKE '%%%s%%' AND artist LIKE '%%%s%%' AND composer LIKE '%%%s%%' AND album LIKE '%%%s%%' AND `grouping` LIKE '%%%s%%' AND genre LIKE '%%%s%%' AND `time` LIKE '%%%s%%' AND recnum LIKE '%%%s%%' AND date_added LIKE '%%%s%%' AND date_modified LIKE '%%%s%%' AND year LIKE '%%%s%%' AND track_number LIKE '%%%s%%' AND track_count LIKE '%%%s%%' AND disc_number LIKE '%%%s%%' AND disc_count LIKE '%%%s%%' AND kind LIKE '%%%s%%' AND comments LIKE '%%%s%%' AND my_rating LIKE '%%%s%%' AND location LIKE '%%%s%%'", $varname_rsResults,$varartist_rsResults,$varcomp_rsResults,$varalbum_rsResults,$vargroup_rsResults,$vargenre_rsResults,$vartime_rsResults,$varrecnum_rsResults,$varadd_rsResults,$varmod_rsResults,$varyear_rsResults,$vartracknum_rsResults,$vartrackcnt_rsResults,$vardiscnum_rsResults,$vardisccnt_rsResults,$varkind_rsResults,$varcomments_rsResults,$varrating_rsResults,$varloc_rsResults);
$query_limit_rsResults = sprintf("%s LIMIT %d, %d", $query_rsResults, $startRow_rsResults, $maxRows_rsResults);
$rsResults = mysql_query($query_limit_rsResults, $mediadb) or die(mysql_error());
$row_rsResults = mysql_fetch_assoc($rsResults);
if (isset($_GET['totalRows_rsResults'])) {
$totalRows_rsResults = $_GET['totalRows_rsResults'];
} else {
$all_rsResults = mysql_query($query_rsResults);
$totalRows_rsResults = mysql_num_rows($all_rsResults);
}
$totalPages_rsResults = ceil($totalRows_rsResults/$maxRows_rsResults)-1;
$queryString_rsResults = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_rsResults") == false &&
stristr($param, "totalRows_rsResults") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_rsResults = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_rsResults = sprintf("&totalRows_rsResults=%d%s", $totalRows_rsResults, $queryString_rsResults);
?>
答案 0 :(得分:0)
您应该能够使用ifnull
将NULL
转换为(比方说)WHERE
子句中的空字符串。例如,name LIKE '%%%s%%'
将变为ifnull(name, '') LIKE '%%%s%%'
。
与您当前的代码一样,它会假设您设置了各种$_GET
参数 - 如果未设置参数,它将与您使用的-1
不匹配。