Jump to content

Multiple queries issue when included in another file.


Go to solution Solved by mac_gyver,

Recommended Posts

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();

?>

$chef is not defined in instructors.php.

 

Global applies only to the page it is on (and is bad practice). Use a session variable if you want to preserve the value for use on another page.

session_start();
...
$chef = $row['Instructor'];
$_SESSION['chef'] = $chef ;

then in instructor.php

session_start();
...
$chef = $_SESSION['chef'];
  • Solution

in addition to what Barand stated about not using global, you should not be trying to paste together web pages using php include/require statements. you should also not be making database connections/running queries inside of loops.

 

displaying classes and the instructor information for each class, can be accomplished using one JOIN'ed query. you can even select the formatted date/time in the query. this will result in very little code - build and run ONE query, loop over the result from that query and display the result the way you want.

 

i suspect the reason your existing code doesn't work is because the actual $row['Instructor'] value that is being used either contains some white-space as part of the data (in the ft_form_7 table) or the column name isn't exactly 'Instructor' (there would be a php undefined index error if you have php's error reporting turned on all the way.)

Thank you both very much.  I definitely want to do this the right way.

 

I did some reading on using JOIN, and that seems like the way to go.  I am a little stuck on building that query, but I will keep working to get it right!

 

Thanks again!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.