473,395 Members | 1,440 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,395 software developers and data experts.

mysql_query() will not accept multiple queries?

348 100+
I would like to make sure I understand this correctly. I just came from php's site and read that multiple queries are not supported. Does this mean that I would not be able to do the following?

mysql_query($sql1);
mysql_query($sql2);

I have multiple functions set up this way and if it is a problem, I would sure like to know how to handle this.

Thanks,


Frank
Oct 20 '07 #1
7 12952
pbmods
5,821 Expert 4TB
Heya, Frank.

What they mean is that this will not work:

Expand|Select|Wrap|Line Numbers
  1. mysql_query("{$sql1}; {$sql2}");
  2.  
Instead, you'd have to do this:
Expand|Select|Wrap|Line Numbers
  1. mysql_query($sql1);
  2. mysql_query($sql2);
  3.  
The advantage to this is that you can't arbitrarily execute SQL, though it does still potentially make you vulnerable to SQL injection.

As an example, let's suppose you had this insecure code (which makes me shudder just to type):
Expand|Select|Wrap|Line Numbers
  1. $__res = mysql_query("UPDATE `Users` SET `password` = '{$_POST['newPass']}' WHERE `userid` = {$_POST['userid']} LIMIT 1");
  2.  
The good news is, that if someone were to set $_POST['userid'] to "1; DROP TABLE `Users` --", the DROP TABLE part would *not* execute.

HOWEVER, that does not mean that you are safe from SQL injection. Using the example above, what happens when $_POST['userid'] is set to "1 OR TRUE --"?

Now your query looks like this:
Expand|Select|Wrap|Line Numbers
  1. UPDATE `Users` SET `password` = 'somePass' WHERE `userid` = 1 OR TRUE --LIMIT 1
  2.  
The effect of this code is to set the password for EVERY User to 'somePass'.

This is why you should always:
  • Enclose all 'User input' in your queries in either quotes or backticks (even numeric values; I don't know who's been spreading the nasty rumor that this is 'wrong'):
    E.g., Use this:
    Expand|Select|Wrap|Line Numbers
    1. $__sql = "SELECT * FROM `table` WHERE `{$col}` = '{$val}'";
    2.  
    Instead of this:
    Expand|Select|Wrap|Line Numbers
    1. $__sql = "SELECT * FROM `table` WHERE {$col} = {$val}";
    2.  
  • Always cast numeric types to int or float before you use them, and always run string values through mysql_real_escape_string(). If you're using a Database Abstraction Layer, implement an escape() method:
    Expand|Select|Wrap|Line Numbers
    1. $_id = (int) $_GET['id'];
    2. $_name = mysql_real_escape_string($_GET['name'], $dbConn);
    3.  
    4. $__sql = "UPDATE `table` SET `name` = '{$_name}' WHERE `id` = '{$_id}' LIMIT 1";
    5.  

This was not the point of your original inquiry, but what the heck.
Oct 20 '07 #2
Markus
6,050 Expert 4TB
Haha!

You really got going there! Thanks anyway, it taught me something, at least!

=]
Oct 20 '07 #3
fjm
348 100+
I don't quite understand cast numeric types.. I will have to look that up when I get up. Been up all night programming.. Actually, just staring at the screen for the entire night. :)

Is there a difinitive guide to sql injection out there that you can point me to? I would like to make sure that I have all of my bases covered when it comes to security. Is there anything else besides what you mention here in this post that I should be concerned with?

This was not the point of your original inquiry, but what the heck.
Fantastic Post. Thank you very much. Actually, this comes at a great time for me beacuse I have just started coding my insert forms. Man.. thank you again..
Oct 20 '07 #4
pbmods
5,821 Expert 4TB
Heya, Frank.

The PHP Manual has a chapter devoted to security, including SQL Injection.

Ultimately, it all boils down to one thing:

Never trust User input.

Put another way, never make assumptions about values that you get from outside the immediate scope.

Here's an example:
Expand|Select|Wrap|Line Numbers
  1. function getVoltage( $current, $resistance )
  2. {
  3.     return $current / $resistance;
  4. }
  5.  
  6. echo getVoltage( $_GET['current'], $_GET['resistance'] );
  7.  
Works great, right?

Except how do you *KNOW* that $_GET['current'] and $_GET['resistance'] are positive floats?

What if the User got confused on your form and entered 'twenty' as the value of 'current'?

How do you calculate 'twenty' / 45, for example?

The solution is to never trust User input:
Expand|Select|Wrap|Line Numbers
  1. function getVoltage( $current, $resistance )
  2. {
  3.     // Validate parameters.
  4.     $_cur = abs( (float) $current );
  5.     $_res = abs( (float) $resistance );
  6.  
  7.     if( $_res == 0 )
  8.     {
  9.         return 0;
  10.     }
  11.     else
  12.     {
  13.         return $_cur / $_res;
  14.     }
  15. }
  16.  
Oct 20 '07 #5
fjm
348 100+
Heya, Frank.

The PHP Manual has a chapter devoted to security, including SQL Injection.

Ultimately, it all boils down to one thing:

Never trust User input.

Put another way, never make assumptions about values that you get from outside the immediate scope.

Here's an example:
Expand|Select|Wrap|Line Numbers
  1. function getVoltage( $current, $resistance )
  2. {
  3. return $current / $resistance;
  4. }
  5.  
  6. echo getVoltage( $_GET['current'], $_GET['resistance'] );
  7.  
Works great, right?

Except how do you *KNOW* that $_GET['current'] and $_GET['resistance'] are positive floats?

What if the User got confused on your form and entered 'twenty' as the value of 'current'?

How do you calculate 'twenty' / 45, for example?

The solution is to never trust User input:
Expand|Select|Wrap|Line Numbers
  1. function getVoltage( $current, $resistance )
  2. {
  3. // Validate parameters.
  4. $_cur = abs( (float) $current );
  5. $_res = abs( (float) $resistance );
  6.  
  7. if( $_res == 0 )
  8. {
  9. return 0;
  10. }
  11. else
  12. {
  13. return $_cur / $_res;
  14. }
  15. }
  16.  
Hey Pbmods..

Thanks a whole bunch! I see through your example what you are driving at.. Let me ask you one more question if I may..

Is mysql_real_escape_string() a "catch all" for all user POST vars or would I have to also use the others like html_special_characters or whatever its called? :)

I mean, if I filtered all of the user input through mysql_real_escape_string(), would I be ok with regard to injection? Of course I would cast types as well.
Oct 21 '07 #6
pbmods
5,821 Expert 4TB
Heya, Frank.

mysql_real_escape_string() escapes all SQL injection-type characters (quotes, backslashes and the delimiter character [which is usually ';'] -- which is why it requires an open database connection resource). However, it does not touch HTML or PHP code.

You are correct; you'll also want to run html_special_characters() and/or strip_tags().

Note also that you'll want to check for magic_quotes, or else the slashes that get automagically added will be escaped themselves!

e.g., "This string's mine!" gets turned into "This string\\'s mine!", which will not lead to desirable results.

You can check to see if magic_quotes has been activated by using get_magic_quotes_gpc() (the 'gpc' stands for GET, POST, COOKIE).
Oct 21 '07 #7
realin
254 100+
hi fjm,

From what i understand is mysql_real_escape_string is jus to escape the quotes which my result in the SQL injection, it appends the quotes with the slashes, hence removing the chance for the queries like
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM users WHERE user='aidan' AND password='' OR ''='' 
to execute. .


while htmlspecialchars, change the symbols into their corresponding HTML entities, e.g. > is conevrted into >
this actually minimizes the chances for user to run any vulnerable script from the text area.
I am still a newbie at php, so this is waht i know :)

cheers !!
Oct 21 '07 #8

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

Similar topics

2
by: caro | last post by:
Hi I am trying to write two Select * statements to the same text file using bcp (from a stored procedure). But cannot find a way of appending to a file using bcp. Does anyone know if this...
3
by: Gord | last post by:
I would like to create a summary report from the results of 11 queries (based on 2 tables). All the queries have the same format and return 3 numbers (Count, Current Year Bal, Last Year Bal.)...
2
by: JMCN | last post by:
is it possible to export multiple queries into one workbook, having each query as separate worksheet? i cannot specify a range because the records will change on a daily basis as for query size. i...
4
by: Dave Edwards | last post by:
I understand that I can fill a datagrid with multiple queries, but I cannot figure out how to fill a dataset with the same query but run against multiple SQL servers, the query , table structure...
1
by: mattcatmattcat | last post by:
I have a VB7 aspx file I am creating that requires multiple queries each dependant on the previous queries results. If I run these queries in foxpro, I just run a query then create a cursor with...
8
by: beretta819 | last post by:
Ok, so I apologize in advance for the wordiness of what follows... (I am not looking for someone to make this for me, but to point me in the right direction for the steps I need to take.) I was...
7
by: vaiism | last post by:
I am creating a report that outputs the contact information and details about a water treatment plant, and needs to include information about people who work there. If I tie all the information...
4
by: Akhenaten | last post by:
I am currently using enterprise manager to run multiple queries on a single table in a DB. I refresh these queries every few minutes. Due to the huge number of them I was looking for a better way...
2
by: DigiLife | last post by:
Greetings all, I am trying to create a form in access 2007 that will return multiple queries (count). I have an appointment table and based off of the date selection I want a grid similar to "Cheap...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
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...

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.