Hello all, I've been trying to do something with no luck whatsoever, read several google results on it and I can't find a good working answer that makes my code work.
I need to check if a row exists, so that if it doesn't I create a button that will create the row and if it does exist I create a statement that will allow me to modify it but I can only make the code work at a 50%.
This is my code:
PHP Code:
$catName = $_GET['name']; //get the name from URL
$search = array('-','%28','%29'); //look for dashes and hex %28 %29
$replace = array(' ','(',')'); //replace for spaces,( and )
$cleanName = str_replace($search, $replace, $catName); //clean the name so it matches with the one in DB
$result = mysql_query("SELECT * FROM `cp_l2` WHERE `cp_cat_name` = '".$cleanName."' ");
while($row = mysql_fetch_array($result)){
if(mysql_num_rows($result) > 0){echo 'I\'m here';}
else{echo 'I\'m not here';}
}
I say it's working at a 50% because if the row exists the code echoes "I'm here", the problem is when the row doesn't exist, it wont echo I'm not here...
I need some help please, also, if there is a better method please feel free to post it, thanks in advance for any help provided.
Did you try dying? and see if there is an error message? If you query is not running, it would be neither, so you may think it is your else and expect to see "I'm not here", when in fact it may be nothing, because the whole while loop is not happening... Just a theory.
Last edited by Daria; 05-04-2011 at 11:37 AM.
Absence of a result is a result, unless defined otherwise.
You need to check mysql_num_rows() before you start fetching your results. Because now if there are no rows it will not go into the while loop and therefore not check num rows.
Hope that made sense
I'm sorry to say I don't know what you mean with checking mysql_num_rows(), I don't consider myself an expert php coder yet, could you please give me an example?
$catName = $_GET['name']; //get the name from URL
$search = array('-','%28','%29'); //look for dashes and hex %28 %29
$replace = array(' ','(',')'); //replace for spaces,( and )
$cleanName = str_replace($search, $replace, $catName); //clean the name so it matches with the one in DB
$result = mysql_query("SELECT * FROM `cp_l2` WHERE `cp_cat_name` = '".$cleanName."' ");
$number_of_results = mysql_num_rows($result);
if( $number_of_results > 0 )
{
echo $number_of_results.' Results Found!';
while($row = mysql_fetch_array($result))
{
echo 'I\'m here';
}
}
else
{
echo 'There are no results!';
echo 'I\'m not here';
}
Last edited by Daria; 05-04-2011 at 12:07 PM.
Absence of a result is a result, unless defined otherwise.
Bookmarks