Jump to content

multiple query help


jake2891

Recommended Posts

does anyone know why the following sql runs successfully in mysql client but not when run in a php page. I can run other queries in the php page but none that chain together.

 

$sql = "select test1 from hello where test1 = 1; DROP TABLE hello ";

$q = mysql_query($sql,$link);

 

thanks

Link to comment
Share on other sites

Simple, mysql_query() does not support multiple queries -

 

Description

resource mysql_query ( string $query [, resource $link_identifier ] )

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier .

 

Because too many php programmers don't properly validate external data that their scripts receive and put into query strings.

 

 

Link to comment
Share on other sites

What he means is, PHP by default doesn't allow two queries to be processed in the same string specifically because developers are generally not smart enough to properly sanitize their incoming user data, so a user puts "'; DROP TABLE `users`;#" inside the "login" form and your site suddenly disappears.  PHP has a lot of "quirks" to protect the dumber members of the web dev community.

 

You will have to put your queries in an array and run them one at a time.

 

-Dan

Link to comment
Share on other sites

just to contribute to the general knowledge of others that could read:

 

PFMaBismAd is right in his answer:  mysql_query() ... or more in general, mysql API doesn't support multi-queries, however the mysqli API support them and they can be used without problem, taking obviously the necessary precautions to sanitize and properly chain the sentences.

 

As has been said, multi-queries have some risks and they must be used only in very specific and controlled situations where the risks are non-existent (or highly minimized).

 

@ManiacDan

"PHP by default doesn't allow two queries to be processed in the same string"

 

that is incorrect, PHP doesn't play any role in allow or not a multi-query; is mysql API (or mysqli) the responsible for that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.