[MYSQL] problem with spaces in rows

Hello.

I'm not sure how I can get around this, or what I am doing wrong, but I need some help. :slight_smile:

I want to do an select query looking like this:

SELECT venue, SUM( amount ) 
FROM IWD
WHERE venue = 'Foxy Hollow'

Unfortunately I need to have spaces in the names in these fields,
is there a way to get around it?
THe page I am making has an drop down menu, which is beeing populated by these names, and uppon selecting one of them, the above query will be issued.

THis is the full code that I am trying to use...


<?php
if (isset($_POST['venue']))
{
        $sel_venue = $_POST['venue'];
        if ($sel_venue == "All venues")
        {
                unset($query);
                echo "<div align=\"center\">";
                echo "Total donations collected from each venue <br>";
                echo "<table width=\"398\" height=\"29\" border=\"1\">";
                mysql_connect("localhost", "user", "pass") or die(mysql_error());
                mysql_select_db("waid") or die(mysql_error());
                $query = "SELECT venue, SUM(amount) FROM IWD GROUP BY venue";
                $result = mysql_query($query) or die(mysql_error());
                while($row = mysql_fetch_array($result))

                {
                                                echo "<tr>";
                                                echo "<th width=\"199\" height=\"25\" align=\"left\" valign=\"middle\" scope=\"col\">";
                                                echo $row['venue'] . ":";
                                                echo "</th>";
                                                echo "<th align=\"left\" valign=\"middle\" scope=\"col\">";
                                                echo "$" . $row['SUM(amount)'];
                }
                echo "</div></th></tr></table>";
                mysql_close();
        }
        else
        {
                unset($query);
                echo "Total donations collected from ";
                mysql_connect("localhost", "user", "pass") or die(mysql_error());
                mysql_select_db("waid") or die(mysql_error());
                $query = "SELECT venue, SUM(amount) FROM IWD WHERE venue='$sel_venue'";
                $result = mysql_query($query) or die(mysql_error());
                while($row = mysql_fetch_array($result))
                {
                                                echo "<div align=\"center\">";
                                                echo "<table width=\"398\" height=\"29\" border=\"1\">";
                                                echo "<tr>";
                                                echo "<th width=\"199\" height=\"25\" align=\"left\" valign=\"middle\" scope=\"col\">";
                                                echo $sel_venue . ":";
                                                echo "</th>";
                                                echo "<th align=\"left\" valign=\"middle\" scope=\"col\">";
                                                echo "$" . $row['SUM(amount)'];
                }
                echo "</div></th></tr></table>";
                mysql_close();
        }
}
?>

Everything else is working on the page.. except this. :confused:

Possible try using:

where venue like

instead of

where venue =

Sorry, I am not fully understanding the problem. Perhaps it is because I am reading this post on my mobile phone.

Are you getting an error message in the log file? If so, please post it? Or are you seeing an HTML formatting issue?

---------- Post updated at 07:56 ---------- Previous update was at 03:07 ----------

OBTW (back on my desktop computer), this statement does not look right, or it is not how I would write it:

$query = "SELECT venue, SUM(amount) FROM IWD WHERE venue='$sel_venue'"; 

I would write this:

$query = "SELECT venue, SUM(amount) FROM IWD WHERE venue=".$sel_venue;

or

$query = "SELECT venue, SUM(amount) FROM IWD WHERE venue=$sel_venue"; 

in PHP, single quotes means "take what is inside literally and do not expand". Double quotes means "expand the variables inside". When you mix single and double quotes (inside each other) you can have huge problems in PHP.

Better for this, and considered the least confusing is:

$query = 'SELECT venue, SUM(amount) FROM IWD WHERE venue='.$sel_venue;

The reason for this is that 'SELECT venue, SUM(amount) FROM IWD WHERE venue=' does not require any variable expansion inside the single quotes.

Thank you Neo...

Unfortunately, this didn't work.. and I think the reason behind it (in the case with your code) is because there are blank steps (spaces) in the names that I am using.

Here is an example of what the database looks like:

id venue amount
1 Gaia streams office 160
2 Foxy Hollow 100
3 Foxy Hollow 160
4 Foxy Hollow 160

As you can se, the venue names "Foxy Hollow" or "Gaia Streams Office" has spaces in them, which gives me this error with your code:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Hollow' at line 1

To get back to the root of the problem, as I tried to describe it when I first wrote the post in the forum.. how do I issue the query (basically, not only in PHP, I mean, I am given the same problem when trying in the console) so that I can ask the database to sum the amounts on, for example the venue "Foxy Hollow" as neither single nor doubble quotemarks works when using " ... WHERE venue='Foxy Hollow' "

Sorry, I don't think blanks in your SQL query is the problem. We send MySQL queries with blanks in the string everyday.

You have a different problem, I think. You can easily test it by using the same query and putting an entry in the DB without a space in the string.

I wonder in that case what my problem is...

I issue the following query in phpMyAdmin:

SELECT venue, SUM( amount ) 
FROM IWD
WHERE venue = 'Foxy Hollow'

And get this result:
venue SUM(amount)
NULL NULL

If I take away the single quotes, I get an error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Hollow' at line 1
SELECT venue, SUM( amount ) 
FROM IWD
WHERE venue = Foxy Hollow

If I have entries without spaces (for example FoxyHollow) the query works like a charm..

I guess I could convert the names to replace blanks with underscores.. but I'd rather not. :confused:

Edit: Damn it, id Didn't work.. but, now I really don't understant this.. it worked yesterday.. I HAVE made some changes somewhere, dut damn me if I could remember what.. there IS something wrong with the query.. my apologies... now.. I just need to find out WHAT is wrong with my query.. :confused:

Also, you can try replacing the space in your query with the ASCII char for a space. I am on my mobile and don't have that char off the top of my head.

Hmm....

Hmm.. I wonder...could this have something to do with it all?
What I did change yesterday, was to make sure the spaces gets sent, BUT, these are changed %20

Could it by any chance be that the null results I am getting, is simply because when it is written to the database, it gets written as "Foxy%20Hollow".
If so, do you have any suggestions on how to get arount this?
Because as far as I know, it will either gets sent as "FoxyHollow" or "Foxy%20Hollow".
(I course make it so that it will be "Foxy<any char>Hollow" aswell, could I in the register.php change the <any char> before sending it to the database? And in that case, how?)

When I look in the phpMyAdmin interface, I see them as it should be, with blanks, not %20.. I havn't actually checked in the console yet.

---------- Post updated at 12:00 PM ---------- Previous update was at 11:40 AM ----------

I have to apologize again to anyone that have tried to helo me so far.. to see exactly what was going in to the database, I checked the logs of http-access....
All entries have (up untill now) gotten entered as "%20Venue%20Name" so all of them had a blank space in front of them.. no wonder I never found them when trying WHERE venue = 'Foxy Hollow".. it didn't exist.

Problem solved, thank you! :slight_smile:

---------- Post updated at 12:35 PM ---------- Previous update was at 12:34 PM ----------

Ok, one more problem.. (hope you guys arn't fed up with me yet) :slight_smile:

I have managed to strip it all diwn, as I said earlier, from "%20Venue%20Name" to "Venue%20Name". But, I just detected something else...

If the registered venue name is for example "Foxy Hollow" (Foxy%20%20%20Hollow) I get the same kind of error, it can't be found as I only get "Foxy%20Hollow" outputed in my dropdown menu.

Is there a way to already at the point of inserting the values in the database, reduce the blanks. So that if I get a value like "This%20Venue%20%20Name%20Is%20%20%20Long" in the $_POST[venue] variable in register.php, it can get reduced to "This%20Venue%20Name%20Is%20Long". I guess the answer is str_replace. If I knew how many blanks there were, I wouldn't have any problems with this. But I have no way of knowing the exact amount of blanks will be used in the name, so I have no clue of how to use str_replace for this.

I mean, str_replace(' ', ' '); would only convert two blanks into one, right?

I recommend you use preg_replace() with a regular expression to remove any leading, trailing and "more than one space in the middle" of your string. This is really easy to do with one PHP preg_replace() statement. Or you could use more than one preg_replace() if easier for you at first, breaking the problem down into parts first.

Thank you Neo. :slight_smile:

A few minutes after I send the post, I got a friend online who suggested this:

$venue = $_GET[venue];
$venue = trim(ereg_replace(' +', ' ', $venue));

which seems to work pretty good :slight_smile:

Ofcourse, this doesn't remove leading and trailing spaces, but I managed to fix that in the code that is actually sending the http-request. Unfortunately that coding language is quite limited and won't remove the extra spaces in between words on an very efficient way. :slight_smile:

trim() will remove leading and trailing spaces.

PS: I suggest you do a bit of your own research before posting. For example, at a minimum look at the PHP man pages of the commands you are using!

Also, this:

$venue = $_GET[venue];

should be

$venue = $_GET['venue'];

... for faster execution time.

I will leave it to you as an exercise to Google the net and find out why.

And please, do more of your own research before asking questions :smiley:

If you get in the habit of looking at your log files when trouble shooting a problem, and also reading, at a minimum, the man pages of the PHP commands you are using, you will find that you actually will save more time v. less time.

Edit: I closed this thread because it is not related to MySQL; it is related to poor string formatting. If you wish to work on PHP string formatting, please start a new thread on a specific issue. Thanks.