Search the Community
Showing results for tags 'multiple queries'.
-
Main script gets class information from a database and prints them so long as the class start date or end date is after today (actually includes today). Main script calls "instructors.php". It queries another database based on the instructor name ($chef) and then prints the bio information for that instructor. "instructors.php" works fine on it's own, when I add "$chef = "Chef Name" ("Chef Name" is in the Instructors database). When it's called from the main script, nothing shows up in that area - even though "Chef Name" is in the database. All of the other data is printed fine, just not anything from instructors.php. I verified that it's actually including the file, as I can add "echo "test";" to the top of instructors.php and it prints fine in the main script. Any ideas of what I'm missing? Main Script <?php // Get required login info include "/path/to/login/info/file.php"; // Get required login info - changed for this post. $db = new mysqli('localhost', $username, $password, $database); // Connect to DB using required login info if($db->connect_errno > 0){ die('Unable to connect to database [' . $db->connect_error . ']'); } unset($username);// put these variables back to null unset($password);// put these variables back to null unset($database);// put these variables back to null //query db $sql = <<<SQL SELECT * FROM `ft_form_7` WHERE DATE(class_start_date) >= CURDATE() OR DATE(class_end_date) >= CURDATE() ORDER BY class_start_date ASC SQL; if(!$result = $db->query($sql)){ // if there is an error in running the query, show error message. die('There was an error running the query [' . $db->error . ']'); } while($row = $result->fetch_assoc()){ // Get start date information $start_date = $row['class_start_date']; // Get event_start_date for conversion and call it $start_date $start_date_formatted = date("l M d, Y", strtotime($start_date)); // Convert start_date $end_date = $row['class_end_date']; // Get event_end_date for conversion and call it $start_date $end_date_formatted = date("M d, Y", strtotime($end_date)); // Convert start_date // Get time information. $start_time = $row['class_start_time']; // Get event_start_time for conversion and call it $start_time $start_time_formatted = date("h:i A", strtotime($start_time)); // Convert start_time $end_time = $row['class_end_time']; // Get event_end_time for conversion and call it $end_time $end_time_formatted = date("h:i A", strtotime($end_time)); // Convert end_time // echo information... echo "<h2>" , $row['class_name'],"</h2>" ; // echo event name echo "<p><strong>",$start_date_formatted; // echo the start date if (empty($start_time)) { echo ''; } else { echo " (", $start_time; } // echo start time if (empty($end_date)) { echo ''; } else { echo " -","<br />", $end_date_formatted; } // echo end date if (empty($end_time)) { echo ')'; } else { echo " - ", $end_time, ")"; } // echo end time // if there is no start time, echo nothing. (otherwise it seems to echo 4pm). If it does contain a time, echo the time. echo "</strong><br />"; $chef = $row['Instructor']; global $chef; if ($chef != NULL) { require ('instructors.php'); } echo $row['class_description'], "<br />"; echo "<strong>" , $row['type'], " - Cost: $",$row['cost'] , " - #" , $row['course_number'] , "</strong><br />" , "</p>"; // echo class type and cost } $db->close(); $result->free(); ?> instructors.php <?php include "/path/to/login/info/file.php"; // Get required login info - changed for this post. $db_instructors = new mysqli('localhost', $username, $password, $database); // Connect to DB using required login info if($db_instructors->connect_errno > 0){ die('Unable to connect to database [' . $db_instructors->connect_error . ']'); } unset($username);// put these variables back to null unset($password);// put these variables back to null unset($database);// put these variables back to null //query db $sql_instructors = <<<SQL SELECT * FROM ft_form_8 WHERE chef_name = '$chef' SQL; if(!$result_instructors = $db_instructors->query($sql_instructors)) { // if there is an error in running the query, show error message. die('There was an error running the query [' . $db_instructors->error . ']'); } while($row_instructors = $result_instructors->fetch_assoc()) { $chef_full_name = $row_instructors['chef_name']; $chef_id = $row_instructors['submission_id']; $full_bio = $row_instructors['full_bio']; echo "<a href=\"#\" class=\"clickme\">" , $chef_full_name , "</a>"; echo "<div class=\"box\">"; echo $full_bio , "</div>"; } $db_instructors->close(); $result_instructors->free(); ?>
- 3 replies
-
- multiple queries
- mysqli
-
(and 1 more)
Tagged with:
-
Hi everyone, I would like to know if: I have three different forms to create three different mysql codes. Now my code will generate the three mysql codes correctly but each time I tried to generate the second mysql code using another form, the previous will get wiped and therefore I can only have one mysql code working at the moment. (I apologize in advanced if my issue is not clearly stated.) Attached is the code that I have right now. I am trying to get all three mysql codes (Searchsql, Filtersql, Panelsql) correct so I can pass all three mysql queries to a function where it can combine all three queries into one array using the array_intersect function. Please kindly let me know your opinion on the problem and I would really appreciate the help. $object = new Three; class Three { public $Panel, $Search, $Filter; function save_Three() { while($Prow = sqli_fetch_array($Panel, MYSQLI_BOTH)) { $PanelName = $Prow ['name']; } while($Srow = sqli_fetch_array($Search, MYSQLI_BOTH)) { $SearchName = $Srow ['name']; } while($Frow = sqli_fetch_array($Filter, MYSQLI_BOTH)) { $FilterName = $Frow ['name']; } $Combined = array_intersect($Panel, $Search, $Filter); foreach ($Combined as $item) print $item; } } $Panelsql = "SELECT DISTINCT aID, name from test.article"; if (isset($_GET['pid'])) { $pID= $_GET['pid']; echo "$pID"; $Panelsql = "SELECT DISTINCT aID, name from test.a_m, test.platform, test.message, test.article where aid in (SELECT DISTINCT assocAID from test.a_m, test.platform, test.message where assocMID in (SELECT mID from test.message, test.platform where pID=$pID and pID = assocPID))"; } else if (isset($_GET['mid'])) { $mID= $_GET['mid']; $Panelsql = "SELECT DISTINCT aID, name from a_m, message, article where aid in (SELECT DISTINCT assocAID from a_m, message where mID=$mID and mID = assocMID)"; } else { $Panelsql = "SELECT DISTINCT aID, name from article"; } $object->Panel = $Panelsql; $aRegions = $_POST['RegionSelected']; $Filtersql = 'SELECT * FROM test.article'; if(isset($aRegions)) { $Filtersql .= ' WHERE 1 AND ('; foreach ($aRegions as $word) if($word==$aRegions[count($aRegions)-1]) { $Filtersql .= " Region = '". $word ."')"; } else { $Filtersql .= " Region = '". $word ."' OR"; } } //Population filtering function $aPopulations = $_POST['PopulationSelected']; if(isset($aPopulations)) { if(!isset($aRegions)){$Filtersql .= ' WHERE 1 AND ('; } else { $Filtersql .= ' AND ('; } foreach ($aPopulations as $word) if($word==$aPopulations[count($aPopulations)-1]) { $Filtersql .= " Population = '". $word ."')"; } else { $Filtersql .= " Population = '". $word ."' OR"; } } $object->Filter = $Filtersql; $Searchsql="SELECT * FROM test.article"; if (isset($_POST['search'])){ $st= ($_POST['search_box']); $Searchsql .= " WHERE name LIKE '%{$st}%' OR abstract LIKE '%{$st}%' OR Summary LIKE '%{$st}%' OR Keyword LIKE '%{$st}%' OR population LIKE '%{$st}%'OR region LIKE '%{$st}%'"; } $object->Search = $Searchsql;