Jump to content

Recommended Posts

How does one go about converting an SQL query such as:

 

SELECT * FROM members WHERE userid='$userid' AND code=$code'

 

into a prepared statement for PHP. I understand how to use prepared statements however I'm having trouble with the * part of the query and I'd rather not have to write out all the table coulmns because I have about 20.

 

EDIT: I'm using mysqli procedural

Edited by MFA
Link to comment
https://forums.phpfreaks.com/topic/275719-select-in-prepared-statements/
Share on other sites

Do you actually need every single column? Generally you'll only need a handful so you should be listing them out.

 

But that has nothing to do with prepared statements.

SELECT * FROM members WHERE userid = ? AND code = ?
The parameters are only for actual values in the query, not names or syntax.

 

Do you actually need every single column? Generally you'll only need a handful so you should be listing them out.

 

But that has nothing to do with prepared statements.

SELECT * FROM members WHERE userid = ? AND code = ?
The parameters are only for actual values in the query, not names or syntax.

 

I was just going to type "Yes, I know but then how can i bind the result for each column without having to type out a new variable for each one. Is there not a similar function to mysqli_fetch_assoc for prepared statements?" BUT then it hit me that it is exactly the same thing as using mysqli_fetch_assoc still requires me to write out the column name anyway!

 

 If you are trying to limit overhead then you would write out each colum you need.

 

What is limit overhead?

 

 

One final question about prepared statements, will the example below work or must all paramaters be replaced by a '?' - The solution would obviousluy be to create a variable to replace '1' but I'm wondering if this is unnecessary?

 

"SELECT email,password FROM members WHERE email = ? AND password = ? AND validate=1"

What is limit overhead?

If you select fields that you are not using, then it requires a bunch of extra processing and memory that may not be necessary if you limit the selection to only fields you need. The more you select, the more work mysql has to do in order to locate the data and pull it out of the database initially, then your result set is larger and requires more time and memory to transfer from mysql to your PHP script.

 

So by listing out only the fields you need your scripts would likely work quicker w/ less memory, and having the fields listed also provides some implicit documentation about what fields the script uses and what their names are (so you dont have to reference the db table structure when you work on the script in the future).

 

 

 

One final question about prepared statements, will the example below work or must all paramaters be replaced by a '?'

It will work fine, you can have non-parameterized values along with parameterized values in the same query if you want. Constant values like the 1 in that query should just be left as it is and not converted to a parameter.

kicken gave the explanation. Selecing * grabs all the columns. In small applications this may have no consquences however in larger more complex applications you should try to limit your use to gettting exactly what you need, when you need it.

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.