Jump to content


Implode is making me explode!!


  • Please log in to reply
12 replies to this topic

#1 Guest_huey4657_*

Guest_huey4657_*
  • Guests

Posted 14 July 2006 - 02:02 PM

Hi all,
Can someone help me with this function...

I used the implode function but the problem I am now getting is that after using implode I can only search the database and return a result when there is only one item stored in the array, when there is more than one item (Blah1,Blah2,Blah3,Blah4) in the array it returns 0 Rows. Here is the code...
<?php

  /* Program: Stats.php

     Desc:    Allows users to select the required
              variables to create statistics. */

  /* Function to start session */

  session_start();

?>

<html>

 <head>

  <title>Horse Racing Stats</title>

 </head>

 <body topmargin    = "0"
       leftmargin   = "0"
       marginheight = "0"
       marginwidth  = "0">



   <table width       = "100%"
          border      = "0"
          cellpadding = "5"
          cellspacing = "0">

    <tr>

     <!-- Create page header -->

     <td height  = "10%"
         colspan = "3"
         align   = "center"
         valign  = "top">

      <font color = "gray"
            size  = "+2">

          <b>Selected statistics</b>

      </font>

     </td>

    </tr>

<?php

     /* Read file that stores connection variables */

     include("Lame.inc");

     /* Connect to server and database */

     $MakeCon = mysql_connect($hostserver, $username, $pass)
                or die ("Could not connect to server.");

     $ConnDB = mysql_select_db($db, $MakeCon)
                or die ("Could not connect to database.");

     $Coursearray = implode(",", $Course);

     $SqlQueryA = "SELECT Course_Name 
                   FROM tblMeeting 
                   WHERE Course_Name IN ('$Coursearray')";

     $SqlResA = mysql_query($SqlQueryA)
               or die ("Could not execute query");

     $NumRows = mysql_num_rows($SqlResA);

     /** This shows the inside of the array for testing purposes**/

     echo "$Coursearray\n\n";

     /** This shows how many records have been returned for testing purposes **/

     echo "$NumRows ROWS";

?>

</table></body></html>

Here is an image of the result (that works) when only one item in the array:-
http://
Here is an image of the result (that fails) when more that one item is in the array:-

Please can someone help me... thanks

[attachment deleted by admin]

#2 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 14 July 2006 - 02:04 PM

I I LIKE THE CATCHY HEADER, MADE ME LAUGH

#3 CheesierAngel

CheesierAngel
  • Members
  • PipPipPip
  • Advanced Member
  • 105 posts
  • LocationBelgium

Posted 14 July 2006 - 03:02 PM

How are you fiilling your $Course array ?
Or can you show us what's in the array ?
<?php
  echo "<pre>";
  print_r($course);
  echo "</pre>";
?>

Would really help solving your probl.

#4 Guest_huey4657_*

Guest_huey4657_*
  • Guests

Posted 14 July 2006 - 03:07 PM

Thanks please go to PHP HELP > Multiple checkbox / sql search problem topic the code is posted there

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 14 July 2006 - 03:09 PM

<?php
$Course = array('blah1', 'blah2', 'blah3');
$Coursearray = implode("','", $Course);      // <-- inner quotes

     $SqlQueryA = "SELECT Course_Name 
                   FROM tblMeeting 
                   WHERE Course_Name IN ('$Coursearray')";
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 Oldiesmann

Oldiesmann
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationCincinnati, Ohio

Posted 14 July 2006 - 03:15 PM

Almost there :)

<?php
$Course = array('blah1', 'blah2', 'blah3');
$Coursearray = implode("','", $Course);      // <-- inner quotes

     $SqlQueryA = "SELECT Course_Name 
                   FROM tblMeeting 
                   WHERE Course_Name IN ($Coursearray)"; // <-- No quotes needed here...
?>

Since the query statement is already in double quotes, you don't need single quotes around $Coursearray.
The Oldiesmann
SMF Project Manager
SMF+Gallery2 - Beta3.1 now available

#7 Guest_huey4657_*

Guest_huey4657_*
  • Guests

Posted 14 July 2006 - 03:17 PM

Hi Guys,
The array comes from muliple checkbox selections from the user.
The inside of the array looks like this after implode (Blah1,Blah2,Blah3) when this is used in the SELECT query (SELECT Course_Name FROM tblMeeting WHERE Course_Name IN ('$Coursearray')). I think the problem is it is reading the array as one string?

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 14 July 2006 - 03:20 PM

Almost there :)

<?php
$Course = array('blah1', 'blah2', 'blah3');
$Coursearray = implode("','", $Course);      // <-- inner quotes

     $SqlQueryA = "SELECT Course_Name 
                   FROM tblMeeting 
                   WHERE Course_Name IN ($Coursearray)"; // <-- No quotes needed here...
?>

Since the query statement is already in double quotes, you don't need single quotes around $Coursearray.


If you you remove those quotes the query becomes

....WHERE Course_Name IN ( blah1','blah2','blah3) ";

which is now clearly wrong
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 Guest_huey4657_*

Guest_huey4657_*
  • Guests

Posted 14 July 2006 - 03:25 PM

Thats right mate But I did not create the array, the user creates the array from the multiple options they select. Here is the code:-
<?php

  /* Program: Stats.php

     Desc:    Allows users to select the required
              variables to create statistics. */

  /* Function to start session */

  session_start();

?>

<html>

 <head>

  <title>Horse Racing Stats</title>

 </head>

 <body topmargin    = "0"
       leftmargin   = "0"
       marginheight = "0"
       marginwidth  = "0">



   <table width       = "100%"
          border      = "0"
          cellpadding = "5"
          cellspacing = "0">

    <tr>

     <!-- Create page header -->

     <td height  = "10%"
         colspan = "3"
         align   = "center"
         valign  = "top">

      <font color = "gray"
            size  = "+2">

          <b>Select statistics</b>

      </font>

     </td>

    </tr>

    <?php

     /* Read file that stores connection variables */

     include("Lame.inc");

     /* Connect to server and database */

     $MakeCon = mysql_connect($hostserver, $username, $pass)
                or die ("Could not connect to server.");

     $ConnDB = mysql_select_db($db, $MakeCon)
                or die ("Could not connect to database.");

     /* Create query that gets all race
        courses in alphabetical order */

     $SqlQuery1 = "SELECT Course_Name 
                  FROM tblRaceCourse 
                  ORDER BY Course_Name";

     /******* Irrelevant code cut here*******/

     /* Execute query */

     $SqlRes1 = mysql_query($SqlQuery1)
               or die ("Could not execute query");
 
      /******* Irrelevant code cut here*******/

    ?>

    <tr>

     <!-- Create table with blue bachground -->

     <td width   = "80%"
         rowspan = "2"
         align   = "left"
         valign  = "middle">

       <!-- Create form containing stats list -->

       <?php

       echo "<form action = 'CreateStats.php' 
                   method = 'POST'>\n";

       echo "<table cellpadding = '5' 
                    border      = '0'>\n";

       echo "<tr><td><font size = '2'><b>Course</b></font></td>";

       /* reset counter */

       $RowCnt1 = 0;

       /* For each category record fetched
          Create a checkbox */
                                             
       while($Row1 = mysql_fetch_array($SqlRes1))
               
            {

            /* Extract record into variables named
               after their database field name */

            extract($Row1);

            /* Create checkbox */

            echo "<td>

                   <input type  = 'checkbox' 
                          name  = 'Course[]' 
                          value = '$Course_Name'";

            /* Create checkbox label */

            echo "checked><font size = '1'>$Course_Name</font></td>\n";

            /* Increase count by one */
                            
            $RowCnt1++;

            /* Variable equals modulus 
               of count and 9 */

            $NewLine1 = $RowCnt1 % 5;

            /* If modulus is equal to zero
               then create new table row,
               this is to stop horizontal
               scroll bar from appearing */

            if($NewLine1 == 0)

              {

              echo "</tr><tr><td></td>";

              }
                                                
            }

     /******* Irrelevant code cut here*******/

       echo "</tr></table>\n";

       /* Create submit form button */                            

       echo "<p>

              <input type  = 'submit' 
                     value = 'Submit Course'>\n";

       echo "</form>\n";

      ?>

 </body>

</html>


#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 14 July 2006 - 03:38 PM

Yes, but I wanted demonstrate that with  an array of string values you need to implode with "','" and not just a comma
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 Guest_huey4657_*

Guest_huey4657_*
  • Guests

Posted 14 July 2006 - 03:40 PM

Thank-you Barand you beauty.

#12 Oldiesmann

Oldiesmann
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationCincinnati, Ohio

Posted 14 July 2006 - 04:04 PM

Almost there :)

<?php
$Course = array('blah1', 'blah2', 'blah3');
$Coursearray = implode("','", $Course);      // <-- inner quotes

     $SqlQueryA = "SELECT Course_Name 
                   FROM tblMeeting 
                   WHERE Course_Name IN ($Coursearray)"; // <-- No quotes needed here...
?>

Since the query statement is already in double quotes, you don't need single quotes around $Coursearray.


If you you remove those quotes the query becomes

....WHERE Course_Name IN ( blah1','blah2','blah3) ";

which is now clearly wrong


For some reason I was thinking that implode("', '") would put the ' at the beginning and end as well... Not trying to cause any problems :)
The Oldiesmann
SMF Project Manager
SMF+Gallery2 - Beta3.1 now available

#13 Guest_huey4657_*

Guest_huey4657_*
  • Guests

Posted 14 July 2006 - 05:36 PM

Yes, no worries oldiesmann! you was just trying to help. right or wrong the more advice the better so we can learn while trying things out. thks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users