2

1: PDO use prepared statement which the meaning of prepared is something that we use it in many functions i think, and we prepared it for many functions.then i just want to know should i use diffrent sql query in all of my functions or should i define many query at first point in my init.php file(which include functions and connection to db)and then in each function pass them to the function to use these queries?just an example for what i described:

$query1 = "Insert into names(id,name) values (:id,:names)"
$query2 = "insert into games(id,name) values (:id,:names)"

and then for functions somtihing like this

function job($query){
//do the job
}

i ask this question because i read in some place i have to use this prepared statment in more than one place and if its true how should i use some unique query in a function?

2: can i use this :id :names in two prepared statement?or i have to set unique names?

$query1 = "Insert into names(id,name) values (:id,:names)"
$query2 = "insert into games(id,name) values (:id,:names)"

wont they conflict with each other?

2 Answers 2

2

I certainly wouldn't define all of my queries in one place for them to be used later -- I would put the queries where they are going to be used; otherwise it could get very confusing, especially if you name them query1, query2, etc.

If you have the same query that is prepared separately, then you can use the same tokens without any problem:

$stmt1 = $db->prepare($query1);
$stmt2 = $db->prepare($query2);

You can bind completely different values to :id and :name for both stmt1 and stmt2.

Sign up to request clarification or add additional context in comments.

2 Comments

can i use this prepared statement in functions seperatly..somthing like before which i used queries in functions but with pdo?..because i think its realy confusing to define queries like this.just as you mentioned...i just looking for this part of my queastion "i ask this question because i read in some place i have to use this prepared statment in more than one place and if its true how should i use some unique query in a function?"
@HiDd3N you don't have to use prepared statements in more than one place at all. You can use them in zero, one, or more places.
1

If you bind :id and :name right after each query it will be ok, but if you only bind them only once after declaring both query statements the last binded statement will overwrite the values. You can create a function to handle the queries if you desire, I personally write out the queries each time just to keep an eye on what I am doing in case i need last minute changes, but that is fine to stream line your work.

1 Comment

ok that was just in case to know something..then this names just like variables thanks but not completely my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.