Jump to content

Recommended Posts

I am a beginner with MySQL and was wondering what method advanced users would recommend to perform the simple task of looking through two tables to find one row. I don't need code from you, just a general idea of what type of query is best for this situation. If you can offer an explanation of why you like the method, that's even better.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/272133-finding-one-row-in-a-couple-tables/
Share on other sites

First of all, you should be explicit with your join.

 

A join would look like this:

SELECT t1.fields, t2.fields 
FROM t1
LEFT (or INNER) JOIN t2 
ON t1.fk = t2.pk
WHERE t1.field = 'value';

 

However, you need to explain your goal a little better. " looking through two tables to find one row" doesn't make much sense.

Edited by Jessica

Actually, I have another question related to this. At runtime, my script actually has the table value stored in a PHP variable. Is it possible to use bind_params when preparing the query to plug in the table name?

 

$stmt = $mysqli->prepare("SELECT * FROM ? WHERE `id` = ?;");
$stmt->bind_param('ss', $tbl, $id);

 

I tried the code above and it isn't working.

I found the same answer at this link, Jessica.

 

It basically says that you need to plug the table name in using a variable, which means you'll have to sanitize/validate it first.

 

Thanks for your time everyone!

Edited by nodirtyrockstar
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.