1

i have this variable here:

$prefix=$wpdb->base_prefix  ;

Which prints

wp_

Now, i do have this query, and i need to insert it something like this:

$path = $wpdb->get_results("SELECT * FROM $prefixbckg
 WHERE id = 1");

My output has to be this:

$path = $wpdb->get_results("SELECT * FROM wp_bckg
 WHERE id = 1");

How can i do this? Because i need to make the prefix to be flexible, so i need to add the prefix before, without pre-defining it. Thanks

1
  • $path = $wpdb->get_results("SELECT * FROM {$prefix}bckg WHERE id = 1"); ?.. Commented Dec 27, 2012 at 15:45

2 Answers 2

2

The proper way:

$path = $wpdb->get_results('SELECT * FROM '.$prefix.'bckg WHERE id = 1');

The "I'm new to PHP" way:

$path = $wpdb->get_results("SELECT * FROM {$prefix}bckg WHERE id = 1");
Sign up to request clarification or add additional context in comments.

5 Comments

Why is option number one more 'proper' than the second?
You mean... 'The way I like it' and 'The way it should be done' framework.zend.com/manual/1.12/en/…
@JonahBishop Because it uses single quotation marks so the parser won't waste time in searching for variable inside the statement... Whenever gets to the double quotations the php parser check whole statement for possible variable while with single it doesn't ... It is faster anyway...
It's faster, easier to read, editors can help you and it's the way that most languages do it.
While it may technically be faster, I can't believe it matters in 99.9% of cases. Premature optimization is the root of all evil.
1

Try one of these methods:

$prefix=$wpdb->base_prefix;
$mainPrefix = $prefix . 'bckg';
$path = $wpdb->get_results("SELECT * FROM $mainPrefix WHERE id = 1");

or

$path = $wpdb->get_results("SELECT * FROM {$prefix}bckg WHERE id = 1");

Both results are same:

SELECT * FROM wp_bckg WHERE id = 1

But which one is faster!? I made a simple benchmark program and here is the sequel:

The first method time is: 0.0608940124512

The second method time is: 0.0609350204468

So the first method a bit faster than the second method :-)

Good Luck

Comments

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.