473,715 Members | 2,260 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to query mysql for data that has "#" in the string?

7 New Member
When I do an select statement in PHP for mysql that contains fields that end in a # sign, I receive an error that indicates that it can't find the table and I receive no results. If I remove the offending field, everything works perfectly:

Expand|Select|Wrap|Line Numbers
  2. $result = mysql_query($selstring);
  3. $row = mysql_fetch_array($result);
Has anyone run into this, and if so, how do I fix it?
Feb 4 '11 #1
14 9622
code green
1,726 Recognized Expert Top Contributor
The hash symbol denotes a comment in php and also MySQL.
Feb 4 '11 #2
6,050 Recognized Expert Expert
Can you post the complete error?
Feb 4 '11 #3
7 New Member
The error I receive is: ErrorUnknown table 'VNDTBL' in field list

This error makes sense if # is terminating the select clause and commenting out the rest of the clause. There would be no table name specified. Several of my tables have fields with #'s in them. Is there any way to accommodate this problem, short of creating a view and renaming the field?
Feb 4 '11 #4
6,050 Recognized Expert Expert
Are you quite sure that table exists?

No - the hash character in a string is of no significance to the PHP interpreter.
Feb 4 '11 #5
code green
1,726 Recognized Expert Top Contributor
I don't know how MySQL will handle this, but you could try wrapping the column names, hashes and all, in backticks
Expand|Select|Wrap|Line Numbers
I know this helps with spaces.
However, I if the PHP code still breaks, maybe single quotes will work
Expand|Select|Wrap|Line Numbers
If not you will need to read the query in from a text file
Expand|Select|Wrap|Line Numbers
  1. $selstring = file_get_contents(myquery.txt)
where myquery.txt contains
Expand|Select|Wrap|Line Numbers
On one line. I recommend this as good practice as queries are then seperated from PHP code
Feb 4 '11 #6
6,050 Recognized Expert Expert
Naughty, code green! I was just about to reply to you, but you edited your post. You removed the part I was going to quote, so nevermind.
Feb 4 '11 #7
7 New Member
Thanks for your responses. It looks like I have two options, use a text file to contain the select string, or use a view to rename the field. If I use a view, will this affect mysql optimization capabilities regarding selecting the most appropriate keyed path to retrieve the data?
Feb 4 '11 #8
7 New Member
By the way, I had already tried using backticks, and it didn't help.
Feb 4 '11 #9
1,206 Recognized Expert Top Contributor
What version of MySQL are you using?

I just tested this out by adding a # to the end of a field in a test database I have on my local machine.

I wrote a small script to run a select using that field name. All results were returned with no errors.

So are you positive that your field and table names are correct?

Another possible option if performance isn't an issue, is to use the wildcard to select all fields in the table and then only use the ones you want in php.
Feb 4 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Michael Flanagan | last post by:
(Bottom line: I think what I'm looking for is an easy way of changing the case of key values in an array.) I've got code that I'm trying to make agnostic about the underlying database system I'm using. That is, I want the code to work whether the underlying db is MS SQL, MySQL, Oracle, etc. I'm using the PEAR DB package, and it's great for what I'm trying to do...except in one area. I get the returned row fields in an associative array,...
by: Simon Wigzell | last post by:
Does anyone have a definitive list of keywords to avoid or prohibit as field names for the tables for an ASP driven program that communicates with a SQL database? I have a website that allows customers to define tables online and I keep hitting the field names I need to prohibit - "Function" is the latest one. I'd like to just compare the users field names with a list and end this problem for good! Also any VBScript words too? Thanks.
by: Andres Eduardo Hernando | last post by:
Hi, I'm not entirely sure this is the right group to ask this question, but I saw a similar one above, and the group's charter is not clear enough about it, so, here I go: ;) What is the standard behavior (if any) when the same name is attached to more than one field in a <form>? An example (that closely resembles the problem I'm working on) will be (this is a standard radio group without a default selected option): <form ...> <input...
by: HD | last post by:
Hello. For the following, I would appreciate if anyone could tell me: if it can be done, how it might done, and/or what search terms I could use to find the solution myself. I would like to create a database in which _adding a new record automatically creates a new field in the table_ (ideally, deleting a record would also delete the field that was created when that record was added). Thus, the number of fields in the table would...
by: Ralph | last post by:
I'm trying to import a range of cells from an Excel spreadsheet into a table in access. The first row contains column labels, but I cannot use those as my field names, both because of their format (number, space, text) and because I've got a whole bunch of similar tables and I need to have consistant field names in these tables for some data manipulation I'm doing in VBA. I've used the following statement to bring the data in with the...
by: kathyburke40 | last post by:
Odd problem. I have a table in the following format: DocID Question1 Question2 Question3 ------------------------------------------------ 298 1, 2, 3 or 0 Each Question field will have 1,2,3 or 0 if not completed. The number of Question fields will vary. I need to count all the questions, then figure out how many have 0
by: Oliver Gabriel | last post by:
Hi, i want to export a table for later import, using vba. That´s my code: export: filename = "C:\HVOtabelle.txt"
by: Billy | last post by:
I do a SELECT * from table command in an ASP page to build a text file out on our server, but the export is not to allow a field name rows of records. The first thing I get is a row with all the field names. Why do these come in if they are not part of the table records? How do I eliminate this from being produced? Here's the ASP code.... <html> <head>
by: | last post by:
Given an XML file (dataset.writexml), here is my output (simplified for this posting): <?xml version="1.0" standalone="yes"?> <NewDataSet> <Category> <CategoryId>80</CategoryId> <Category>MyCat</Category> </Category> </NewDataSet>
by: Wayne | last post by:
I have been given the task of rewriting a database that seems as though it has been written by someone with a very basic understanding of Access. Many of the object names and field names in tables are overly long and contain spaces e.g. "Next of kin phone number" instead of something like "KinPhNo". I am looking for opinions on whether I should spend considerable extra development time and "fix" all the problem names. I know that this...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.