SQL Query and much frustration

I’m pretty new to PHP, at least, as an overall thing. I know it well enough to easily work in Wordpress and create custom templates and the like.

But now I’m stuck working on a PhpMyDirectory site and its just set up very differently so I’m running up against my lack of comprehensive knowledge.

All I need to do is query the DB to join to tables to output a list of city names.

But when I get to the front end, all I get is either “Array” or only the first city (I tried two different solutions).

Could someone please point me in the right direction, I’ve spent way too long on something that shouldn’t be this difficult. The way PHPmyDirectory is set up, there’s a php code page, and then a .tpl template page for output.

This code got me the “array” output on the frontend:
PHP page:
[php] $city_titles = $db->GetAll(“SELECT SQL_CALC_FOUND_ROWS r.*, r.city_id AS city_id, locations.title AS location_title FROM “.T_LISTINGS_CITIES.” r LEFT JOIN “.T_LOCATIONS.” locations ON locations.id=city_id WHERE listing_id=”.$listing[‘id’]." ");
$city_serve=mysqli_query($city_titles);
while($cities=mysqli_fetch_assoc($city_serve)):
$city_title = $city_serve[‘location_title’];

			$output .= '<li>' . $city_title . '</li>';
			
			endwhile;

		$template_content->set('city_listings', $city_titles);[/php]

This code got me the one city result combined with a while loop on the frontend template:
[php]$cities_records = $db->getall(“SELECT city_id FROM “.T_LISTINGS_CITIES.” WHERE listing_id=”.$listing[‘id’]." ");
// die(print_r($listing_records));
if(count($cities_records) >= 1) {

		$new_counter = 0;
		foreach($cities_records as $city_served) {
		
			$new_counter ++;
			
			$city_title = $db->getrow("SELECT title FROM ".T_LOCATIONS." WHERE id=".$city_served['city_id']." ");
                           
						
			}
			
			$city_text =  $city_title['title'];
          
		$template_content->set('city_listings', $city_text);	

}
[/php]

frontend code:
[php]<?php if($city_listings) {

while ($city = $city_listings) { ?>

   <li><?php echo $city; ?></li>
<?php } ?>
	<?php } ?>[/php]

It appears I’m not getting my loop to loop but I don’t see what I’m missing. I’d really appreciate some help, as I’m getting very frustrated. Thank you very much.

First, You aren’t actually using a join anywhere.

Try some checks,
[php]if ( is_array($city_listings)){
foreach ( $city_listings as $city ) {[/php]

The first query listed - is that not a join? Entirely possible I’m using wrong terminology!! It says LEFt JOIN in there so I just figured…

[php]SELECT SQL_CALC_FOUND_ROWS r.*, r.city_id AS city_id, locations.title AS location_title FROM “.T_LISTINGS_CITIES.” r LEFT JOIN “.T_LOCATIONS.” locations ON locations.id=city_id WHERE listing_id=".$listing[‘id’]." ");[/php]

Thanks! I’ll try your suggestion. I think part of my problem is the way this is setup - so its hard to know if my php page is messed up, or the template page that calls the content.

I don’t know why I’m having such a hard time wrapping my head around it…

Yeah, that is a join. I didn’t scroll to see the rest of the first query. The next issue would be not using perameterized queries. I don’t know how the system you are using (is it using a framework?) implements them.

Well, I know that query works, more or less as is, because I used it on an admin page to create a table pulling the same parameters from the various tables, and using parameterized queries.

The second block of code is pretty close, as it is returning results from the DB, but only one result. So, somehow, i must not be looping, right?

I don’t know what $db->GetAll does, I would expect it to be a wrapper, but from your usage, it doesn’t look like it does that.

If you do this, what does it return?
[php]print_r( $db->GetAll(/your sql statement/);[/php]
Also, there should be a difference in $db->getall and $db->GetAll

That print_r code breaks the page for me - it won’t load the listings.

the $db is a global query built into the phpmydirectory framework:

phpMyDirectory has two main variables that are generally available in every file and class if needed.
Variable Description

$PMDR Registry object that is used for getting other objects and performing very high level
common tasks (i.e. getting a configuration value).
$db Database object allowing easy querying and manipulation of database result sets.

I am guessing you didn’t add your query within the method that was in the print_r statement? It should show you what is being returned.

I am not wondering about $db. That should be a database object. It’s what is returned from GetAll() or getall() and if they are two separate methods. If they are, it is a really poor design.

Ah - yeah, I just assumed GetAll was correct as its used elsewhere in the code.

I did place my query into the print_r brackets - as you indicated - but who knows - this framework is weird to work in…

FYI: I finally got it working by bypassing the PMD $db query syntax altogether and doing this:
[php] $mysqli = new mysqli($dbserver, $dbuser, $dbpass, $dbnamepmd);

$city_titles = “SELECT SQL_CALC_FOUND_ROWS r.*, r.city_id AS city_id, locations.title AS location_title FROM table_listings_cities r LEFT JOIN table_locations locations ON locations.id=city_id WHERE listing_id=”.$listing[‘id’]." ";
$allRows = array();
$cty = $mysqli->query($city_titles);
while($crow = $cty->fetch_assoc()) {
$allRows[] = $row;

$serves .= "

“.$crow[“location_title”].”
";
$template_content->set(‘serves’,$serves);

}
$mysqli->close();[/php]

It works… that’s all I can ask, I guess! :slight_smile:

Thanks for working with me - it helps just to have someone to engage with sometimes - I was getting so frustrated!

The print_r code was missing a parentheses

[php]print_r( $db->GetAll(/your sql statement/);[/php]
->
[php]print_r( $db->GetAll(/your sql statement/) );[/php]

It the syntax error was breaking your page you need to enable displaying all errors on your development environment.

Yay :smiley:

I can’t tell you how many times stupid syntax errors have screwed me up for hours! I just need to become as familiar with PHP as I am with html and css, so I see right away when the syntax is wrong… sigh.

Thanks!

If you used a decent ide, it would tell you or show you there are errors.

Sponsor our Newsletter | Privacy Policy | Terms of Service