I’m quite new to databases. I’m using phpmyadmin and I’m writing just simple SQL queries. I’m curious if I can bookmark a query with bind parameter so I could set the parameter next time again.
I have the following query
SELECT startOfTest FROM `tblTest` WHERE ID = :myID
and I want to set myID
every time I run the query without rewriting it in the code.
Is it possible?
Advertisement
Answer
Sure, in fact there’s a whole section of the manual devoted to bookmarks and using variables in bookmarks.
First, you’ll need the phpMyAdmin Configuration Storage configured. The Configuration Storage is a database that holds user-based data such as bookmarks. There’s some zeroconfiguration support but I like to import the create_tables.sql file from the sql folder and configure the corresponding settings in config.inc.php. It sounds like you’ve already achieved that step.
Then, you’ll want to create the bookmark with the variable inside of special markup, so your query could become SELECT startOfTest FROM `tblTest` WHERE ID=/*[VARIABLE1]*/
. However, this will give an error because the SQL is invalid for MySQL, so we have to build the query a little creatively. I prefer to use SELECT startOfTest FROM `tblTest` WHERE 0=1 /*OR ID=[VARIABLE1]*/
, the 0=1 part won’t match anything but is valid SQL, so your variable can be saved correctly.
Once you’ve added the bookmark, when you go to run it from the SQL tab a text box will appear where you can fill in the substitution you desire: