The basic idea is that SQL injection is more effectively eliminated by properly "escaping" any values that may have come from possibly unreliable data sources like user input. "Escaping" means that you precede quotes and newline chars and that sort of thing with a backslash. Read the docs on [man]mysqli_real_escape_string[/man] to get a clearer idea of what I'm talking about. If you properly enclose strings in quotes and remember to escape them, then you'll prevent some evil user from attacking php code like this:
$sql = "SELECT * FROM users WHERE username=\"" . $_GET["u"] . "\"";
by carefully concocting a query string that defines $_GET["u"] so that query ends up looking something like this:
SELECT * FROM users WHERE username="some_wealthy_customer" LEFT JOIN credit_card_data cc ON cc.id=1
In this case, the evil user supplied a query string that defined $_GET as this:
some_wealthy_customer" LEFT JOIN credit_card_data cc ON cc.id=1
And, depending on how the page is constructed, this may have allowed them to look at all the credit card numbers in a database one by one.
The approach you use above will not work. What will work is if you escape $_GET before feeding it into your query. Escaping it will turn that one " into \" which, instead of exposing credit card data, will try to locate a user with the name some_wealthy_customer" LEFT JOIN credit_card_data cc ON cc.id=1 which will probably harmlessly fail.
In practice, you should ALWAYS escape any string you are feeding into a query unless you are completely certain that its format is safe. In practice, escaping strings manually by using myqli_real_escape_string and concatenating your SQL together is tedious and unnecessary. If you use PDO to connect to your database as Laserlight and Bonesnap suggest, then you can use prepared statements to handle this for you. Your code will be safer and easier to read and you will appear more professional and you'll make more money and generally be a happier person.