Can't account for spaces in my database when echoing out dat

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcc_shane
Forum Newbie
Posts: 22
Joined: Sat May 12, 2012 1:47 pm

Can't account for spaces in my database when echoing out dat

Post by mcc_shane »

Hi Everyone,

What I'm trying to do is echo out information from a mysql database into my URL. I have done that (please see below code) but what I haven't figured out yet is for the code to handle spaces or two words. For example, if you visit the below URLs the page will echo out the city name (which is what I want) but I have a city called "Las Vegas" in my mysql database which is obviously two words and when I type in las vegas, las-vegas or Las Vegas etc ... at the end of the URL the code isn't echoing out anything. I also just recently added a urldecode within my code to see if that works but it appears it isn't. What am I doing wrong? What do I have to change in my syntax? Thanks everyone!

http://whatsmyowncarworth.com/auto/miami
http://whatsmyowncarworth.com/auto/providence
http://whatsmyowncarworth.com/auto/albany
http://whatsmyowncarworth.com/auto/boston
http://whatsmyowncarworth.com/auto/las-vegas <--- not echoing city name
http://whatsmyowncarworth.com/auto/Las%20Vegas <--- not echoing city name

Code: Select all

<?php

include('init.php'); // connection to database

if (isset($_GET['u'])) {
	
	$city = mysql_real_escape_string(urldecode($_GET['u']));
 	// protection against mysql injection
	if (ctype_alnum($city)) {
		$data = mysql_query("SELECT State, City FROM cars WHERE City='$city'" );
		if (mysql_num_rows($data) > 0) {
			while ($row = mysql_fetch_assoc($data)) {
			    echo $row["City"];
			}
		}
	}
}
		
?>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

.htaccess

Code: Select all

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /auto/cars.php?u=$1 [NC]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Can't account for spaces in my database when echoing out

Post by pickle »

Do a var_dump() of $city and see what that looks like. urldecode() is applicable here, so I'm not sure exactly what the issue might be.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Can't account for spaces in my database when echoing out

Post by mikosiko »

ctype_alnum() count blank spaces or special characters like dash?

http://php.net/manual/en/function.ctype-alnum.php
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Can't account for spaces in my database when echoing out

Post by social_experiment »

Not sure if this is useful but from the php manual
The Manual wrote: The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results.
http://se2.php.net/manual/en/function.urldecode.php
Also the last URL does return a value if i visit the page (show's Las Vegas - not sure if you might have fixed it.);
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Can't account for spaces in my database when echoing out

Post by Grizzzzzzzzzz »

looks like you may have fixed it with Las-Vegas.

If you're still deciding on a final way to handle it, from an SEO perspective go the route of using -'s as spaces, it's processed more naturally by search engines.
Post Reply