This forum is archived. Posts are preserved for historical reference. For current help, join us on Discord.

db where var1=x AND var2=y AND var3=q

In UserSpice 4.4 · Started by Ruud on 2019-01-05 6:10 pm · 12358 views · 5 replies

I can't figure out how to solve a right query, what do I wrong?

sql query: 
DELETE FROM `tbl_resource` WHERE (`ID_resource`=".$var1."  AND `ID_veld`=".$var2." AND `type_resource`='P') LIMIT 1;

first try:
$db->delete("tbl_resource",
[ "AND", 
["ID_resource","=",$db->result()[0]->ID_papo], 
["ID_veld", "=", $lesblokId ],
["type_resource", "=", "P"]
]);

second try:
$db->delete("tbl_resource",
[ "AND", 
[ "AND",[
["ID_resource","=",$db->result()[0]->ID_papo], 
["ID_veld", "=", $lesblokId ]
], 
["type_resource", "=", "P"]
]
]);
fallback scenario below used, but please let me know what's the best way to work ;-)

$db->query("DELETE FROM `tbl_resource` WHERE (`ID_resource`= ? AND `ID_veld`= ? AND `type_resource`='P') LIMIT 1;",[$papoId, $lesblokId]);
What happens if you do
$db->query("DELETE FROM tbl_resource WHERE ID_resource = ? AND ID_veld= ? AND type_resource=? LIMIT 1",[$papoId, $lesblokId,'P']);
And btw, table name is really tbl_resource not resource and the columns are ID_veld not veld right?

And btw, table name is really tbl_resource not resource and the columns are ID_veld not veld right?

the normal query works (my second post-entry), but was wondering why it should not work like my try in a first manner.

but I understand it only works for two variables or isn't it so?
Hi. Sorry. Pretty much all the devs have been away this weekend. Sorry for the slow response.

As far as the first vs second, I'll take another look. the way I wrote the query is shorter. In general if you can avoid the paranthesis and ` vs ' there are fewer chances for a mistake.

Ready for something that will change your life and I went 2 years without knowing? ...
After your query that doesn't work, you can just type
dump($db->errorInfo()); and you will get PDO's best guess as to what's wrong with your query. If you get some array with just 012, that means it worked as far as PDO/MySQL is concerned.

You can bind as many variables as you want with the ? thing if that's what you're talking about. And you really don't have to bind that P, like I moved from the query to the bound parameters, but I always do it in case I want to sub it out with a variable later.

I think the $db->delete only works with one parameter. I usually only use that with an ID. I like to write out my delete queries, personally.