Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

First things first:

I've spent most of today going over these forums; the stackoverflow forums; the php.net manual; and a bunch of random sites I found through Google Search - so, if I've missed the answer and it seems like I'm asking an already answered question I'm sorry. I've probably just been looking at the same problem for way too long and nothing is making sense.

The background:

We've inserted some custom tables into the wordpress database, so I can connect to these tables through $wpdb. The code for the working connections is as follows:

global $wpdb;
// get list of localities
$localityRow = $wpdb->get_results( "SELECT * FROM tblYBSLocality ORDER BY Name ASC" );
// get list of schools
$schoolRow = $wpdb->get_results( "SELECT * FROM tblYBSSchool ORDER BY Name ASC" );
// get school ID if form has been submitted
$SelectedSchoolID = $_GET['schools'];
// get location ID if form has been submitted
$SelectedLocationID = $_GET['localities'];

Then later in my page I make it do stuff (scorcery!)

// Create a drop down box for the user to select their location (and one has been done
// for school but I left that out because you get the idea
<select name="localities" style="width: 300px">
<?php
foreach ($localityRow as $locality) {
$LocalityID = $locality->LocalityID;
$LocalityName = $locality->Name;
?>

<option value=<?php echo $LocalityID; ?> <?php if(isset($SelectedLocationID) &&
$SelectedLocationID == $LocalityID) print(" selected")?>><?php echo $LocalityName;?>
</option>
<?php
}
?>
</select>

Note - Yes I know using * is bad, I'll fix that later - right now I just want it to work.

Using this I can then go through and generate a bunch of stuff using the following:

// get school ID if form has been submitted
$SelectedSchoolID = $_GET['schools'];
// get location ID if form has been submitted
$SelectedLocationID = $_GET['localities'];

This isn't really important to the rest of my issue, so I'll move on.

The issue:

I'll start with the code. This is supposed to connect to some other tables and do stuff - based on the connections and data retrieved above:

$selectedSchoolName = $wpdb->get_results( "SELECT Name FROM tblYBSSchool WHERE
SchoolID=".$SelectedSchoolID );
// get selected location name
$selectedLocationName = $wpdb->get_results( "SELECT Name FROM tblYBSLocality WHERE
LocalityID=".$SelectedLocationID );
// get route ID
$getRouteID = $wpdb->get_results( "SELECT * FROM tblYBSRoute WHERE RouteID IN ( SELECT
RouteID FROM tblYBSRouteSchool WHERE SchoolID
=".$SelectedSchoolID.")".$SelectedLocationID);

Now, I THINK the issue comes with the ".$SelectedSchoolID ); part (and the location one but this is already getting tldr).

What I want to do later in the page (once the user has made their selections and submitted the form), is get results (fancy that!).

There are 2 parts to the results:

1st I want to say "Search results for routes between:" the location name and the school name. 2nd, I want to display those results. But I can't even get past the 1st point, so let's fix that first.

I have tried 3 different approaches:

// FIRST APPROACH
// This kinda works, it echoes the last locality name from the table
// which is better than nothing
<b><?php echo $LocalityName;?></b> and <b><?php echo $SchoolName;?></b><br />

// SECOND APPROACH
// This gives me the white page of death. Which is hilarious because it's pretty
// much the exact same as what was used in the option - which worked
<b><?php if(isset($SelectedLocationID) && $SelectedLocationID == $LocalityID) echo
$LocalityName;?></b> and <b><?php if(isset($SelectedSchoolID) && $SelectedSchoolID ==
$SchoolID) echo $SchoolName;?></b>

// THIRD APPROACH
// This also gives me the white page of death. And now I'm out of ideas.
<b><?php if(isset($SelectedLocationID) && $selectedLocationName == $LocalityName
echo $LocalityName ;?></b> and <b><?php if(isset($SelectedSchoolID) &&
$selectedSchoolName == $SchoolName echo $SchoolName ;?></b>

In my head (HAH!) the last 2 should work. Am I correct in thinking that it's the ".$SelectedSchoolID ); part? Or have I forked something else up entirely?

Sorry for the TLDRness, but I thought I should provide as much detail as possible. Any and all help is greatly appreciated. I'm going to go smack my head into the desk a few times now :P

share|improve this question

2 Answers 2

up vote 0 down vote accepted

Not sure why you're getting a WSOD, but immediately I can see that in the following:

$selectedSchoolName = $wpdb->get_results( "SELECT Name FROM tblYBSSchool WHERE
SchoolID=".$SelectedSchoolID );
// get selected location name
$selectedLocationName = $wpdb->get_results( "SELECT Name FROM tblYBSLocality WHERE
LocalityID=".$SelectedLocationID );
// get route ID
$getRouteID = $wpdb->get_results( "SELECT * FROM tblYBSRoute WHERE RouteID IN ( SELECT
RouteID FROM tblYBSRouteSchool WHERE SchoolID
=".$SelectedSchoolID.")".$SelectedLocationID);

...you should be using get_var (which returns the first value of the first row, regardless of the query), not get_results (which returns an array of objects, regardless of the query).

Have you enabled debugging?

share|improve this answer
    
Thanks TheDeadMedic. I've enabled debugging which has helped a bit, and am now plugging away at a couple of different possibilities. I'll change the get_results to get_var and see how that goes, I'll post an update when I next get stuck. –  Robodashy May 8 at 0:39

BANG FRIKKIN BANG! Fixed it. Kinda. If I can't fix the next issue I'll make a new post, but big thanks to bcworkz (from diff forum) for pointing out the debug option - made things so much easier.

I'd vote up your answer TDM (lol tedium), but rep 15 required.

Like you said - the problem was that I was using $wpdb->get_results on all of my calls to the database. Use $wpdb->get_var (get_row works too) when you are trying to select a specific row, not a bunch of rows.

Thanks again, closing this one off.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.