1

So I'm trying to create a dynamic SELECT query that will only show the columns that were chosen previously and stored in a string $stANCol.

$idServ = 17; //$_GET(Selected);
$queryIsSelected = "SELECT $stANCol FROM camposservico WHERE idServico = ? ;";              
$stmt = $mydb->prepare($queryIsSelected);        
$stmt->bind_param("i", $idServ);                
$stmt->execute();

The echo from $stANCol is

morada, dieta, dataIntervencao, sondaVesical, ecd,
sinaisVitais, riscoQueda, riscoAlergia, riscoTransfusao, riscoMultiRisco, obsQuadro, obs

and these are the columns that may change depending on what is selected or not.

The echo from $queryIsSelected is

SELECT morada, dieta, dataIntervencao, sondaVesical, ecd, 
sinaisVitais, riscoQueda, riscoAlergia, riscoTransfusao,
riscoMultiRisco, obsQuadro, obs FROM camposservico WHERE idServico = ? ;

And this query works just fine on phpMyAdmin, hence my question. Am I doing something wrong or is it impossible to have a string variable used on the SELECT?

EDIT: Turns out there was nothing wrong with any of this code, had a pending statement lurking on my page that was messing with this one. Thanks for all the replies everyone :)

2
  • Does SELECT * FROM camposservico WHERE idServico = ? work? Commented Mar 12, 2014 at 12:29
  • Oh, actually now it doesn't anymore. I messed up somewhere else then. I guess the question is irrelevant now. Thanks for the reply, sorry for wasting time. Commented Mar 12, 2014 at 12:36

3 Answers 3

3

remove the ; from your query

your's:

$queryIsSelected = "SELECT $stANCol FROM camposservico WHERE idServico = ? ;"; 

after removing ;

$queryIsSelected = "SELECT $stANCol FROM camposservico WHERE idServico = ? "; 

it will get execute.

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

8 Comments

Thanks for the reply but that didn't make a difference. And I had it without ";" before, I actually added it when I wrote the question here :)
what does ? stands for in idServico = ?
The idServico in the WHERE clause is being set on the bind_param, hence the "?". It will always be an int that is also chosen by the users.
what error are you getting? try, manually insert the value instead of ?.. and see whether its getting execute or not? e.g. $queryIsSelected = "SELECT $stANCol FROM camposservico WHERE idServico = 111 ";
That's exactly what I tried first. I keep getting the "Fatal error: Call to a member function execute() on a non-object ...".
|
0

I have added single quotes in your query

$queryIsSelected = "SELECT $stANCol FROM `camposservico` WHERE `idServico` = ? ";

1 Comment

Thanks for the reply but it's stil the same. Most of my queries don't have any single quotes, and still execute. The problem must be something else that is really getting on my nerves
0

Try this

$queryIsSelected = "SELECT $stANCol FROM camposservico WHERE idServico = ? ;";              
$stmt = $mydb->prepare($queryIsSelected);        
$stmt->bind_param("i", $idServ);  

// move this to here
$idServ = 17;

$stmt->execute();

also try to use something like this for debugging

if($stmt === false) {
   trigger_error('Wrong SQL: ' . $queryIsSelected . ' Error: ' . $mydb->errno . ' ' . $mydb->error, E_USER_ERROR);
}

1 Comment

Updated answer, maybe something is going wrong with your statement.

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.