Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 249 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MySQLi Example

#1
Just showing a MySQLi (MySQL Improved) example, so that anyone who checks here wanting to learn how to use SQL may have a head start on creating more secure scripts (:

Basic Concept:
The basic concept of MySQLi, is to use a Prepared Statement, which instead of providing the values directly like

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

It will seperate the values and create a SQL Syntax like

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

And then pass the values you want through a seperate function, binding them to the query in a secure way that eliminates some possibly injections.

Executing the Concept:
This was written in the thread editor, so don't blame me if it errors :biggrin:
Code:
SELECT * FROM x WHERE i='{$i}'

Now, if we were to breaking the code down, it would become quite simple and obvious.

First, we create a variable called "c" (Connection), which holds out MySQLi connection/

We then use the connection to prepare our SQL statement, and bind the prepared statement to "Result".

We then check to see if Result is set to false, if not we continue, otherwise it has failed to prepare the statement and you should most likely end the script.

If Result was able to prepare the SQL, then we go ahead and bind the Values that we wish to use, in this case, we are binding a Integer, so i provide it was "i", for it's location. If you were to use a string, then you should use "S", there was also another option, but i don't remember it :biggrin:

For Bind_Param, it expects a string, then depending on the string length, the same amount of variables, for example:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


We now execute the prepared SQL, with all the variables set in place (execute is essentially a standard Query)

We store the result (This is only needed when dealing with Select statements, if you are inserting you do not need to store the result, but that can be in another example if you guys want.)

We check to see if the number of rows is equal to 1, You could change this to a true/false check, because it'll only return 0 when no results (which then you do not want to continue)

We then bind the results to some variables, you need atleast 1 variable for each row in your table, however if you select say "User, Pass" from "ID, User, Pass, Email", then you will only need 2 variables provided.

We then fetch the relevant data, and finish of by closing the $Result to prevent any memory leaks~

If you read through this wall of text, congratulations! otherwise ;( Sorry to hear that you want to learn the easy way, aka copy and paste everything.
Reply

#2
Nice thread, nikey. It shows you how SQLi actually works and why it works, I like threads like this.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through