HitByASquirrel
asked on
MySQL, Searching for a nonexistant record
this is a piece of a module detection script for a dynamic content engine i am making.
If a module is in the folder, it adds it to the database when the admin tool is called.
It checks to see if the module is already in the database, and if it is in the folder, but not in the database, then it adds it to the database
the problem is where it says if(!mysql_num_rows($check) ) { It functions perfectly fine, but MySQL outputs an error when it does not find anythign in the database and says that the result from mysql_num_rows() is invalid
thanks a lot if you can help XD
<?php
//search for uninstalled modules
$curdir = opendir("modules");
include($connect_src);
while($dir = readdir($curdir)) {
if(strlen($dir) >= 4){
$dirstr = $dir[0].$dir[1].$dir[2].$d ir[3];
if(is_dir("modules/".$dir) && ($dirstr == "dce_") && is_file("modules/".$dir."/ info.php") ) {
$query = "SELECT dir_name FROM modules WHERE dir_name = '".$dir."'";
$check = mysql_query($query);
if(!mysql_num_rows($check) ) {
include("modules/".$dir."/ info.php") ;
$query = "INSERT INTO modules VALUES ('NULL','".$name."','".$di r."','".$r oot."modul es/".$dir. "','".$ver sion."','" .$vers_out put."','". $creator." ',NOW())";
register_module($connect_s rc,$query) ;
}
}
}
}
?>
If a module is in the folder, it adds it to the database when the admin tool is called.
It checks to see if the module is already in the database, and if it is in the folder, but not in the database, then it adds it to the database
the problem is where it says if(!mysql_num_rows($check)
thanks a lot if you can help XD
<?php
//search for uninstalled modules
$curdir = opendir("modules");
include($connect_src);
while($dir = readdir($curdir)) {
if(strlen($dir) >= 4){
$dirstr = $dir[0].$dir[1].$dir[2].$d
if(is_dir("modules/".$dir)
$query = "SELECT dir_name FROM modules WHERE dir_name = '".$dir."'";
$check = mysql_query($query);
if(!mysql_num_rows($check)
include("modules/".$dir."/
$query = "INSERT INTO modules VALUES ('NULL','".$name."','".$di
register_module($connect_s
}
}
}
}
?>
ASKER
nah, I've already tried that, for some reason, it always returns TRUE, even when there is nothing that fulfills the WHERE statement
simply use
$rowcnt=mysql_num_rows($ch eck);
if($rowcnt==0)
{
include("modules/".$dir."/ info.php") ;
$query = "INSERT INTO modules VALUES ('NULL','".$name."','".$di r."','".$r oot."modul es/".$dir. "','".$ver sion."','" .$vers_out put."','". $creator." ',NOW())";
register_module($connect_s rc,$query) ;
}
and one more thing
$query = "SELECT dir_name FROM modules WHERE dir_name = '$dir'";
regards
$rowcnt=mysql_num_rows($ch
if($rowcnt==0)
{
include("modules/".$dir."/
$query = "INSERT INTO modules VALUES ('NULL','".$name."','".$di
register_module($connect_s
}
and one more thing
$query = "SELECT dir_name FROM modules WHERE dir_name = '$dir'";
regards
ASKER
nope, same thing as before, i think its more of a problem with the query perams rather than how i handle the result
hers the error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/waveform/public_html /wdg-dcm/w dcm_dce_sc an.php on line 29
hers the error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/waveform/public_html
Ok...then try
if ($row = mysql_fetch_array ($check))
Phetu
if ($row = mysql_fetch_array ($check))
Phetu
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
carchitect already mentioned that it should be '$dir' instead of '.$dir.'
:)
:)
Just use
$res = mysql_query("SELECT COUNT(*) FROM modules WHERE dir_name = '$dir'");
$count = mysql_fetch_row($res);
if (!$count[0]) {
do_your_stuff();
}
-baeuml
$res = mysql_query("SELECT COUNT(*) FROM modules WHERE dir_name = '$dir'");
$count = mysql_fetch_row($res);
if (!$count[0]) {
do_your_stuff();
}
-baeuml
$query = "SELECT dir_name FROM modules WHERE dir_name = '".$dir."'";
sorry but use this....
regards
sorry but use this....
regards
ASKER
it didn't fix it, but MySQL said that ther was no databse connection, so i moved the connect to inside the loop, and now it works ^_^ thanks guys!
<?
//search for uninstalled modules
$curdir = opendir("modules");
while($dir = readdir($curdir)) {
if(strlen($dir) >= 4){
$dirstr = $dir[0].$dir[1].$dir[2].$d ir[3];
if(is_dir("modules/".$dir) && ($dirstr == "dce_") && is_file("modules/".$dir."/ info.php") ) {
include($connect_src);
$query = "SELECT dir_name FROM modules WHERE dir_name = '".$dir."'";
$check = mysql_query($query) OR DIE("BAD QUERY '$query' Mysql said : ".mysql_error());
$check_res = mysql_num_rows($check);
if($check_res == 0) {
include("modules/".$dir."/ info.php") ;
$query = "INSERT INTO modules VALUES ('NULL','".$name."','".$di r."','".$r oot."modul es/".$dir. "','".$ver sion."','" .$vers_out put."','". $creator." ',NOW())";
register_module($connect_s rc,$query) ;
}
}
}
}
<?
//search for uninstalled modules
$curdir = opendir("modules");
while($dir = readdir($curdir)) {
if(strlen($dir) >= 4){
$dirstr = $dir[0].$dir[1].$dir[2].$d
if(is_dir("modules/".$dir)
include($connect_src);
$query = "SELECT dir_name FROM modules WHERE dir_name = '".$dir."'";
$check = mysql_query($query) OR DIE("BAD QUERY '$query' Mysql said : ".mysql_error());
$check_res = mysql_num_rows($check);
if($check_res == 0) {
include("modules/".$dir."/
$query = "INSERT INTO modules VALUES ('NULL','".$name."','".$di
register_module($connect_s
}
}
}
}
ASKER
ok, heres the more efficient code:
<?
//search for uninstalled modules
$curdir = opendir("modules");
while($dir = readdir($curdir)) {
if(strlen($dir) >= 4){
$dirstr = $dir[0].$dir[1].$dir[2].$d ir[3];
if(is_dir("modules/".$dir) && ($dirstr == "dce_") && is_file("modules/".$dir."/ info.php") ) {
include($connect_src);
$check = mysql_query("SELECT dir_name FROM modules WHERE dir_name = '".$dir."'") OR DIE("BAD QUERY '$query' Mysql said : ".mysql_error());
$check_res = mysql_num_rows($check);
if(!$check_res) {
include("modules/".$dir."/ info.php") ;
$query = "INSERT INTO modules VALUES ('NULL','".$name."','".$di r."','".$r oot."modul es/".$dir. "','".$ver sion."','" .$vers_out put."','". $creator." ',NOW())";
register_module($connect_s rc,$query) ;
}
}
}
}
<?
//search for uninstalled modules
$curdir = opendir("modules");
while($dir = readdir($curdir)) {
if(strlen($dir) >= 4){
$dirstr = $dir[0].$dir[1].$dir[2].$d
if(is_dir("modules/".$dir)
include($connect_src);
$check = mysql_query("SELECT dir_name FROM modules WHERE dir_name = '".$dir."'") OR DIE("BAD QUERY '$query' Mysql said : ".mysql_error());
$check_res = mysql_num_rows($check);
if(!$check_res) {
include("modules/".$dir."/
$query = "INSERT INTO modules VALUES ('NULL','".$name."','".$di
register_module($connect_s
}
}
}
}
You should use:
if ($check = mysql_query($query))
to check if you got something!
Hth
Phetu