473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

question about passing field name in sql statement as variable.

hey there,
can anyone tell me if this is legal php/mysql ?
i am trying to write a simple function to get a single stat from a
single table.

eg..

$field = 'phone_number';
$customer = 'fred';

$query = mysql_query("SELECT `'{$field}'` FROM `customers` WHERE `name`
= '{$fred}' ");
// note the back quotes around $field

or is this an accident waiting to happen ?

thanks.

Aug 30 '06 #1
5 1751
On 30 Aug 2006 14:14:27 -0700, "nephish" <ne*****@gmail.comwrote:
>can anyone tell me if this is legal php/mysql ?
Did you try running it?
>i am trying to write a simple function to get a single stat from a
single table.

$field = 'phone_number';
$customer = 'fred';

$query = mysql_query("SELECT `'{$field}'` FROM `customers` WHERE `name`
= '{$fred}' ");
// note the back quotes around $field
The single quotes inside the back quotes will break it.
>or is this an accident waiting to happen ?
Probably, yes; depends where the data from the $field and $customer variables
comes from.
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Aug 30 '06 #2

Andy Hassall wrote:
On 30 Aug 2006 14:14:27 -0700, "nephish" <ne*****@gmail.comwrote:
can anyone tell me if this is legal php/mysql ?

Did you try running it?
i am trying to write a simple function to get a single stat from a
single table.

$field = 'phone_number';
$customer = 'fred';

$query = mysql_query("SELECT `'{$field}'` FROM `customers` WHERE `name`
= '{$fred}' ");
// note the back quotes around $field

The single quotes inside the back quotes will break it.
or is this an accident waiting to happen ?

Probably, yes; depends where the data from the $field and $customer variables
comes from.
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
thanks for the quick reply,
Did you try running it?
yep, didn't work. i think the quotes did broke it it too. I got this:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL
result resource in
yadda yadda.
Tried it without the quotes and got a blank page.
i dont get that because i know the values are there.
so, will go ahead and make the individual queries.

thanks for your time.
sk

Aug 30 '06 #3
nephish wrote:
Andy Hassall wrote:
>>On 30 Aug 2006 14:14:27 -0700, "nephish" <ne*****@gmail.comwrote:

>>>can anyone tell me if this is legal php/mysql ?

Did you try running it?

>>>i am trying to write a simple function to get a single stat from a
single table.

$field = 'phone_number';
$customer = 'fred';

$query = mysql_query("SELECT `'{$field}'` FROM `customers` WHERE `name`
= '{$fred}' ");
// note the back quotes around $field

The single quotes inside the back quotes will break it.

>>>or is this an accident waiting to happen ?

Probably, yes; depends where the data from the $field and $customer variables
comes from.
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool


thanks for the quick reply,
>Did you try running it?

yep, didn't work. i think the quotes did broke it it too. I got this:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL
result resource in
yadda yadda.
Tried it without the quotes and got a blank page.
i dont get that because i know the values are there.
so, will go ahead and make the individual queries.

thanks for your time.
sk
What does mysql_error() say when it fails?

ALWAYS check the results from a mysql call. In the case of mysql_query,
a return of false indicates an error in the query.

Try echoing the sql string before executing it - see if it's what you
think it is.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 30 '06 #4

Jerry Stuckle wrote:
nephish wrote:
Andy Hassall wrote:
>On 30 Aug 2006 14:14:27 -0700, "nephish" <ne*****@gmail.comwrote:
can anyone tell me if this is legal php/mysql ?

Did you try running it?
i am trying to write a simple function to get a single stat from a
single table.

$field = 'phone_number';
$customer = 'fred';

$query = mysql_query("SELECT `'{$field}'` FROM `customers` WHERE `name`
= '{$fred}' ");
// note the back quotes around $field

The single quotes inside the back quotes will break it.
or is this an accident waiting to happen ?

Probably, yes; depends where the data from the $field and $customer variables
comes from.
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

thanks for the quick reply,
Did you try running it?
yep, didn't work. i think the quotes did broke it it too. I got this:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL
result resource in
yadda yadda.
Tried it without the quotes and got a blank page.
i dont get that because i know the values are there.
so, will go ahead and make the individual queries.

thanks for your time.
sk

What does mysql_error() say when it fails?

ALWAYS check the results from a mysql call. In the case of mysql_query,
a return of false indicates an error in the query.

Try echoing the sql string before executing it - see if it's what you
think it is.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
wow, echo the query string. in a year of learning / using php and mysql
i swear i have never thought of that. Good result too. It wasn't
exactly what i thought. There was an extra space in the customer name.
thanks.

Aug 30 '06 #5

nephish wrote:
wow, echo the query string. in a year of learning / using php and mysql
i swear i have never thought of that. Good result too. It wasn't
exactly what i thought. There was an extra space in the customer name.
thanks.
Yep, echoing what you are really sending to the database can save you
hours of pain...
You might also want to look at doing your SQL transactions this way (
http://au3.php.net/manual/en/functio...li-prepare.php ) it tends to
lead to more maintainable code and other advantages (for example is you
use prepared statements with Oracle (PHP 5.1-PDO version-
http://au3.php.net/manual/en/function.pdo-prepare.php ) they will be
optimised more readily by Oracle than straight text queries (I'm not
sure if this advantage is true for MySQL but it may be.)) I tend to do
this the OO way but there is no problem doing it procedurally way
either.

Hope this is some use to you...

Aug 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul | last post by:
I want to use sessions to cover myself in case the user switches off cookies so I am passing the session ID manually through a hidden input field. This is what I have so far. index.php page...
5
by: Jim Bo | last post by:
Hi, I have a drop down menu that is being populated by a query to the access database ---- Code --- Skill needed <SELECT size="1" NAME="SkillType" VALUE="SkillType"> <OPTION>...
15
by: C White | last post by:
I've got another drop list problem I am using the following code where users select a name, but it should pass a name and email into the table <select name="user"> <option value="<%...
5
by: Jim Banks | last post by:
Greetings I'm opening a pop up window with a html form, (in one document) and I want to pass a variable to the html form called from the hyperlink. Here's the code I'm using to pop up the...
5
by: Colleyville Alan | last post by:
I have built a SQL statement that is trying to loop through the fields of a table that was built from a spreadsheet and hence is "short and fat". So rather than hard-coding, I have a loop from...
10
by: Colleyville Alan | last post by:
I am trying to turn a short and fat (63 columns) table into one that is tall and skinny (7 columns). Basically, I am trying to create a "reverse crosstab" using a looping structure in VBA along...
3
by: John young | last post by:
I have been looking for an answer to a problem and have found this group and hope you can assist . I have been re doing a data base I have made for a car club I am with and have been trying to...
11
by: kennthompson | last post by:
Trouble passing mysql table name in php. If I use an existing table name already defined everything works fine as the following script illustrates. <?php function fms_get_info() { $result =...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...

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.