Hi all:

I am learning to use arrays through a lot of trial and error (mostly error right now :eek🙂

Here is my challenge.

I have an array as follows:


$pShowDateArray = $_POST["showDate"];



print_r($pShowDateArray )   =  Array ( [0] => 3/30/2012 [1] => 3/31/2012 [2] => 4/1/2012 ) 

I want to run the following. If any of the dates in the array are currently in the database for the user and a matching show ID then they redirect. They are not redirecting.


foreach ($pShowDateArray as $vAdmissionDate){

	if ($stmt = mysqli_prepare($link, "SELECT userName FROM Discountees WHERE freeAdmission = ? AND userName= ? AND showID= ? ")); {
		mysqli_stmt_bind_param($stmt, "sss", $vAdmissionDate, $slusername, $pShowID);
		mysqli_stmt_execute($stmt);
		mysqli_stmt_bind_result($stmt, $rUsername);
		mysqli_stmt_fetch($stmt);

		mysqli_stmt_close($stmt);

		if (isset($rUsername)){
			mysqli_close($link);
			header( 'Location: http://www.mysite.com/msc/mscErrors.php?error=FA&showDate='.urlencode($vAdmissionDate)) ;
			exit();
		}
	} // Close if ($stmt = mysqli_prepare($link, "
} // Close foreach ($pShowDateArray as $vAdmissionDate){

When I echo $vAdmissionDate I get "3/30/20123/31/20124/1/2012" so that seems to be the problem but I am just not sure.

Any help appreciated!

    Square1;10998711 wrote:

    When I echo $vAdmissionDate I get "3/30/20123/31/20124/1/2012" so that seems to be the problem but I am just not sure.

    Did you include any separator in your echo statement? If not, then that would be the output I would expect based on the print_r() output you showed; why does that seem to be a problem to you?

    Also, why are you trying to SELECT the UserName column even when you already know what information it will contain? Seems a bit silly to do all of that work to make the DBMS tell you something you already know.

    Additionally, I would ditch the queries inside of a loop altogether and just use a single SQL query. Note that you don't have to do a strict comparison for each date - you can use an IN() expression to search for rows that match any number of dates in a single query.

    I would also expect that you'd simply need to check the number of rows returned (rather than trying to fetch anything) since you're only interested in knowing if that number is 0 or more than 0.

      Thank you Brad. I switched to a row count and dumped the username. I left the loop because I wanted the individual dates to appear on the tail of the redirect querystring. The other problem was that the dates in the array were in n/j/Y format. They needed to be switched to Y-m-d format. That leads to another aesthetic issue but that will be for another day :queasy:

        Square1;10998724 wrote:

        I left the loop because I wanted the individual dates to appear on the tail of the redirect querystring.

        Why would that still require the multiple queries? (Hint: It doesn't. 😉)

        Square1;10998724 wrote:

        The other problem was that the dates in the array were in n/j/Y format. They needed to be switched to Y-m-d format.

        Aye, and probably the easiest way to do that would be to use MySQL's STR_TO_DATE() function.

          OK, you got me on the loop thing.

          If I DON'T loop it then I will get "3/30/20123/31/20124/1/2012" in the querystring variable instead of just "3/30/2012"...won't I? That is what happened when I triedit without the loop.

            Now that you mentioned the date format, I realized I completely forgot about that.

            If the dates were in the proper YYYY-MM-DD format, all you'd need to do is use [man]implode/man so that you get a string like:

            '2012-03-30', '2012-03-31', '2012-04-01'

            which you'd then use with an IN() expression as mentioned previously.

            EDIT: Note that's not to say that the single query approach isn't applicable with the dates in the current format; it would just look a bit messier since you'd have something like:

            WHERE dateCol IN(
                STR_TO_DATE('%m/%d/%Y', '3/30/2012'),
                # etc. for all dates
            )
              Write a Reply...