iseriouslyneedhelp Posted August 5, 2011 Share Posted August 5, 2011 What's wrong with this code? There are values that match in the tables for each and if I only search by the date, it works fine. It's probably very obvious but I can't seem to pinpoint it. elseif ($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email', 'Appointment'))) { $thisyear = date('Y-m-d'); $todotype = $_GET['type']; $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; } Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/ Share on other sites More sharing options...
Maq Posted August 5, 2011 Share Posted August 5, 2011 Echo out $query to see the actual value, also put or die(mysql_error()) after your mysql_query() call to see if there are any SQL errors. Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252741 Share on other sites More sharing options...
iseriouslyneedhelp Posted August 5, 2011 Author Share Posted August 5, 2011 Thanks. Yes it is echo out, I just didn't put that part in. The error I'm getting is "supplied argument is not a valid MySQL result resource" I've checked all then table, row names and values and all checks out... Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252745 Share on other sites More sharing options...
Maq Posted August 5, 2011 Share Posted August 5, 2011 Thanks. Yes it is echo out, I just didn't put that part in. The error I'm getting is "supplied argument is not a valid MySQL result resource" I've checked all then table, row names and values and all checks out... Can I see that code? The mysql_error() part. Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252749 Share on other sites More sharing options...
iseriouslyneedhelp Posted August 5, 2011 Author Share Posted August 5, 2011 $result=mysql_query($query) or die(mysql_error()) ; while ($row = mysql_fetch_array($result)) { Tells me query is empty! Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252751 Share on other sites More sharing options...
Maq Posted August 5, 2011 Share Posted August 5, 2011 Did you put the or die() here? $result=mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252754 Share on other sites More sharing options...
iseriouslyneedhelp Posted August 5, 2011 Author Share Posted August 5, 2011 my appologies, see updated post above. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252756 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 So what does it echo OP? I don't anything in the above post. Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252758 Share on other sites More sharing options...
iseriouslyneedhelp Posted August 5, 2011 Author Share Posted August 5, 2011 not sure what you mean by "OP" but the message I get on page is "query is empty" what does that mean? Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252760 Share on other sites More sharing options...
Maq Posted August 5, 2011 Share Posted August 5, 2011 It means your $query string is empty, aka your elseif block is not being executed. Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252763 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 as MAQ said, try this for testing purposes. elseif (($_GET['reminder'] == 'thisyear') && (isset($_GET['type'])) && (in_array($_GET['type'], array('Call', 'Email', 'Appointment')))) { $thisyear = date('Y-m-d'); $todotype = $_GET['type']; $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; $result = mysql_query($query) or die(mysql_error()); print mysql_num_rows($query); } If successful you should get past that query and see the number of rows found. Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252764 Share on other sites More sharing options...
iseriouslyneedhelp Posted August 5, 2011 Author Share Posted August 5, 2011 Yeah, why would that be, if I change it to the following it works fine: ($_GET['reminder'] == 'thisyear') { $thisyear = date('Y-m-d'); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) ORDER BY contacttodo.reminder ASC"; But when I do the full code it doesn't, here that is: ($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email'))) { $thisyear = date('Y-m-d'); $todotype = $_GET['type']; $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; } Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252765 Share on other sites More sharing options...
iseriouslyneedhelp Posted August 5, 2011 Author Share Posted August 5, 2011 If successful you should get past that query and see the number of rows found. Still gives me "query is empty" Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252768 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 alright post your full code YouSeriouslyNeedHelp. Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252771 Share on other sites More sharing options...
Maq Posted August 5, 2011 Share Posted August 5, 2011 Then one of these conditions is not true. ($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email'))) { Do a var_dump($_GET); and post the output. Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252774 Share on other sites More sharing options...
iseriouslyneedhelp Posted August 5, 2011 Author Share Posted August 5, 2011 Thank you everyone for your help and patience with me. If successful you should get past that query and see the number of rows found. I used the code below, what you gave me and I had made a mistake when I input the code, what happend is it printed out 1 line with no information in it from the DB. elseif ($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email', 'Appointment'))) { $thisyear = date('Y-m-d'); $todotype = $_GET['type']; $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; $result = mysql_query($query) or die(mysql_error()); print mysql_num_rows($query);} alright post your full code YouSeriouslyNeedHelp. Here is my full code: <?php $ID=$_GET['ID']; require("xxx.php"); if ($_GET['reminder'] == '') { $today = date('Y-m-d', strtotime('today')); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder = '$today' ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'lastweek') { $lastweek = date('Y-m-d', strtotime('last sunday')); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$lastweek' - INTERVAL 7 DAY AND '$lastweek' ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'thisweek') { $thisweek = date('Y-m-d', strtotime('last sunday')); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$thisweek' AND '$thisweek' + INTERVAL 7 DAY ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'nextweek') { $nextweek = date('Y-m-d', strtotime('next sunday')); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$nextweek' AND '$nextweek' + INTERVAL 7 DAY ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'thismonth') { $thismonth = date('Y-m-d'); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND MONTH(reminder) = MONTH(CURDATE())ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'thisquarter') { $thisquarter = date('Y-m-d'); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND QUARTER(reminder) = QUARTER(CURDATE())ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email'))) { $thisyear = date('Y-m-d'); $todotype = $_GET['type']; $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; } $result=mysql_query($query) or die(mysql_error()) ; while ($row = mysql_fetch_array($result)) { ?> <tbody> <tr> <td width=""> <input type="checkbox" name="" id="" value="<?php echo $row['ID']; ?>"></td> <td width=""><a href="/backend/contacts/editcontact.php?ID=<?php echo $row['ID']; ?>"><strong><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></strong></a></td> <td width=""><?php echo $row['phonecell']; ?> </td> <td width=""><a href="<?php echo $row['email']; ?>"><?php echo $row['email']; ?></a> </td> <td width=""><?php echo $row['contactstatus']; ?> </td> <td width=""><?php echo $row['contacttype']; ?> </td> <td width=""><?php echo $row['type']; ?> </td> <td><? echo date("F d, Y", strtotime($row['reminder'])); ?></td> <td><a href="/backend/contact/notes.php?ID=<? echo $row['tasknotes']; ?>">Notes</a></td> <td><a href="/backend/call-center/callcenter.php?ID=<? echo $row['ID']; ?>">Call</a></td> <td><a href="mailto:<? echo $row['email']; ?>">Email</a></td> <td><a href="/backend/contacts/editcontact.php?ID=<? echo $row['ID']; ?>">Edit</a></td> <td><a href="/backend/contacts/deletesuccess.php?ID=<? echo $row['ID']; ?>">Delete</a></td> </tr> </tbody> <?php mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252782 Share on other sites More sharing options...
Maq Posted August 5, 2011 Share Posted August 5, 2011 I would also suggest an 'else' statement in case you don't match anything. Otherwise, you're going to run into the problem you have now. Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252784 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 Np, Alright you need to properly nest your if's and elseif's conditions, since it prints out 1 it means that the query is valid and there is a row found. Try this <?php $ID=$_GET['ID']; require("xxx.php"); if ($_GET['reminder'] == '') { $today = date('Y-m-d', strtotime('today')); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder = '$today' ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'lastweek') { $lastweek = date('Y-m-d', strtotime('last sunday')); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$lastweek' - INTERVAL 7 DAY AND '$lastweek' ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'thisweek') { $thisweek = date('Y-m-d', strtotime('last sunday')); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$thisweek' AND '$thisweek' + INTERVAL 7 DAY ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'nextweek') { $nextweek = date('Y-m-d', strtotime('next sunday')); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$nextweek' AND '$nextweek' + INTERVAL 7 DAY ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'thismonth') { $thismonth = date('Y-m-d'); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND MONTH(reminder) = MONTH(CURDATE())ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'thisquarter') { $thisquarter = date('Y-m-d'); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND QUARTER(reminder) = QUARTER(CURDATE())ORDER BY contacttodo.reminder ASC"; } elseif ($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email'))) { $thisyear = date('Y-m-d'); $todotype = $_GET['type']; $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; }else{ print "No conditions Met"; } $result=mysql_query($query) or die(mysql_error()) ; while ($row = mysql_fetch_array($result)) { ?> <tbody> <tr> <td width=""> <input type="checkbox" name="" id="" value="<?php echo $row['ID']; ?>"></td> <td width=""><a href="/backend/contacts/editcontact.php?ID=<?php echo $row['ID']; ?>"><strong><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></strong></a></td> <td width=""><?php echo $row['phonecell']; ?> </td> <td width=""><a href="<?php echo $row['email']; ?>"><?php echo $row['email']; ?></a> </td> <td width=""><?php echo $row['contactstatus']; ?> </td> <td width=""><?php echo $row['contacttype']; ?> </td> <td width=""><?php echo $row['type']; ?> </td> <td><? echo date("F d, Y", strtotime($row['reminder'])); ?></td> <td><a href="/backend/contact/notes.php?ID=<? echo $row['tasknotes']; ?>">Notes</a></td> <td><a href="/backend/call-center/callcenter.php?ID=<? echo $row['ID']; ?>">Call</a></td> <td><a href="mailto:<? echo $row['email']; ?>">Email</a></td> <td><a href="/backend/contacts/editcontact.php?ID=<? echo $row['ID']; ?>">Edit</a></td> <td><a href="/backend/contacts/deletesuccess.php?ID=<? echo $row['ID']; ?>">Delete</a></td> </tr> </tbody> <?php mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252785 Share on other sites More sharing options...
iseriouslyneedhelp Posted August 5, 2011 Author Share Posted August 5, 2011 I would also suggest an 'else' statement in case you don't match anything. Otherwise, you're going to run into the problem you have now. Thanks, I took it out in an effort to figure this out. Although, my next question will be how to write an else statement that says, if nothing matches, print nothing...but first thing is I need to figure out why this query isn't working for "this year" Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252788 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 Dump the information of your variable and see if this is correct var_dump($_GET); Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252789 Share on other sites More sharing options...
iseriouslyneedhelp Posted August 5, 2011 Author Share Posted August 5, 2011 Np, Alright you need to properly nest your if's and elseif's conditions, since it prints out 1 it means that the query is valid and there is a row found. Okay...well that just drives me nuts because after I put in your code, I get the message "No conditions Met" however, in my database I have in table contacts ID=1 firstname=John lastname=Smith in table contacttodo contacts id = 1 reminder = 08/29/2011 type = Call and it is saying that I have no conditions that met...doesn't make any sense... Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252790 Share on other sites More sharing options...
iseriouslyneedhelp Posted August 5, 2011 Author Share Posted August 5, 2011 Dump the information of your variable and see if this is correct var_dump($_GET); Where does this go? Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252791 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 You need to relax here, its not the query itself but the if condition... just do as 2 of us already told you, var_dump($_GET); edit: Just put it right after "require", it doesn't matter. Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252792 Share on other sites More sharing options...
Maq Posted August 5, 2011 Share Posted August 5, 2011 Please read what you wrote here. You added these 2 conditions, if the elseif is not executing then it's b/c of one or both of these. isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email')) Now, var_dump($_GET); so we can see what the value of 'type' is and figure out what part is not evaluating to true. Yeah, why would that be, if I change it to the following it works fine: ($_GET['reminder'] == 'thisyear') { $thisyear = date('Y-m-d'); $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) ORDER BY contacttodo.reminder ASC"; But when I do the full code it doesn't, here that is: ($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email'))) { $thisyear = date('Y-m-d'); $todotype = $_GET['type']; $query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; } Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252794 Share on other sites More sharing options...
iseriouslyneedhelp Posted August 5, 2011 Author Share Posted August 5, 2011 You need to relax here, its not the query itself but the if condition... just do as 2 of us already told you, var_dump($_GET); edit: Just put it right after "require", it doesn't matter. Thank you, here is the message: array(1) { ["reminder"]=> string( "thisyear" } No conditions metQuerywas empty Quote Link to comment https://forums.phpfreaks.com/topic/243967-php-code-not-working/#findComment-1252795 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.