1

I need retrieve data from 2 tables at the same time, the tables are not linked by foreigns keys or such.

$query1 = "select idemployee from employee where address like 'Park Avenue, 23421'";
$query2   "select idcompany from company where bossName  like 'Peter'";

How can I do this with a kinda thread in PHP?. I've heard that threads are no safe in PHP.

UPDATED:

I got an input field that needs to looks data in both tables, is like search on both tables and show the posible results based on the employee address or boss's name, so you can type an address or just the boss's name. It's just a representation on what I need

hakre
  • 193,403
  • 52
  • 435
  • 836
Felix
  • 3,058
  • 6
  • 43
  • 53
  • Retrieve it. What's the problem? – Your Common Sense Sep 01 '10 at 20:24
  • Executing both queries at the same time, I mean in a thread. – Felix Sep 01 '10 at 20:26
  • 2
    You're really confused, I think, or at least haven't described your problem adequately. – Theodore R. Smith Sep 01 '10 at 20:28
  • 1
    php doesn't support threading. – Byron Whitlock Sep 01 '10 at 20:29
  • @Bryon Whitlock You seriously believe that? That hasn't been the case since at least 2005. See http://www.alternateinterior.com/2007/05/communicating-with-threads-in-php.html – Theodore R. Smith Sep 01 '10 at 20:30
  • There is a MYSQLI_ASYNC option, but exactly what is your goal? Why do you want something like a background thread? – VolkerK Sep 01 '10 at 20:31
  • I'm using an ajax text input search like google does, so I need get the data from 2 tables. – Felix Sep 01 '10 at 20:37
  • @hopeseekr: That's not threading, and is not supported. You could just as well use plain old fork(). – Vinko Vrsalovic Sep 01 '10 at 20:38
  • @Felix Guerrero: Maybe it's just me but "ajax text input search like google" doesn't seem to explain why you have to query data from two tables and why this has to happen at the same time/threaded.... Am I missing something? – VolkerK Sep 01 '10 at 20:39
  • I got an input field that needs to looks data in both tables, is like search on both tables and show the posible results based on the employee address or boss's name, so you can type an address or just the boss's name. It's just a representation on what I need. – Felix Sep 01 '10 at 20:43
  • @Felix: For autocomplete to work on a single input field, you just return all the values at once, not separately. If you have two input fields, you can have separate PHP scripts. See http://www.devbridge.com/projects/autocomplete/jquery/ – Vinko Vrsalovic Sep 01 '10 at 20:46
  • @felix, just do the two queries seperately, merge the results into an array, and return that. OR, use a UNION to pull back the results of 2 queries as one result set. What you are describing does not require the additional complexity of multi-threading. – GrandmasterB Sep 01 '10 at 21:07
  • That's what I'm doing right now, I'm agree my question was too FUZZY. – Felix Sep 01 '10 at 21:19

6 Answers6

2

Either use a single query, or look into something like Gearman to have workers performing jobs asynchronously (I assume the current code is only an example: if the queries you have there are performing so badly you want to perform them async. then you most likely have a database problem). Having some deamon processes ready to go to perform tasks is relatively simple.

.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • wow, @wrikken, I don't care if you didn't win the answer, informing me of Gearman was so worth it! – Theodore R. Smith Sep 02 '10 at 11:02
  • Spread the word, just learned about it some 2 months ago here on SO, and already there are some incredible resource & time saving production processes running here, it works like a charm :) – Wrikken Sep 02 '10 at 11:27
1

Um...

$query1 = "select idemployee from employee where address like ?";
$query2 =  "select idcompany from company where bossName  like ?";

$stmt1 = $pdo->prepare($query1);
$stmt1->execute(array('Park Avenue, 23421'));
$employee = $stmt1->fetch();

$stmt2 = $pdo->prepare($query2);
$stmt2->execute(array('Peter'));
$company = $stmt2->fetch();

What am I missing?

Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91
1

You could use MYSQLI_ASYNC and http://docs.php.net/mysqli.poll (both only available with php 5.3+ and mysqlnd).
But then you'll need a separate connection to the MySQL server for each query.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
0

Depends on what do you want to do with those queries. If, for example, you are using an AJAX form and can make two requests, you should create separate scripts, where each returns the results for each query. That is effectively running them in separate processes, so they execute simultaneously.

There is no such thing as threading per se in PHP, you can see a hack around it here (using full fledged processes.)

Community
  • 1
  • 1
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
  • 1
    While your answer certainly works on paper, I doubt it is an architecturally-sound decision. If he *really* wants them in the same query, he needs to create some sort of Xref table and do the appropriate JOINs. – Theodore R. Smith Sep 01 '10 at 20:31
  • 1
    @hopeseekr: In general, I agree that combining unrelated queries is not architecturally sound. But I don't think that inventing some sort of Xref table where none exist to have them in the same query is any more sound. The basic problem is why does the OP need threads, every thread simulation will either be a hack or just plain fork(). – Vinko Vrsalovic Sep 01 '10 at 20:34
  • What makes you think he needs threads at all? I think he's honestly confused ;-) – Theodore R. Smith Sep 01 '10 at 20:40
  • 1
    @hopeseekr: I just tend to answer the questions, not guess motives (my fault! :). Although I agree he's probably confused – Vinko Vrsalovic Sep 01 '10 at 20:42
0

Counter answer to my previous:

Create a new table

CREATE TABLE EmployeeBossXref (
    id INT auto_increment,
    employee_id INT,
    boss_id INT,
    company_id INT,
    FOREIGN KEY (employee_id) REFERENCES Employee(id),
    FOREIGN KEY (boss_id) REFERENCES Employee(id),
    FOREIGN KEY (company_id) REFERENCES Company(id)
) ENGINE=InnoDB;

Then change SQL to:

select Employee.name, Boss.name, Company.name FROM Employee 
JOIN EmployeeBossXref ebx ON ebx.employee_id=Employee.id 
JOIN Employee Boss ON Boss.id=ebx.boss_id
JOIN Company ON Company.id=ebx.company_id
WHERE Employee.address LIKE 'Park Avenue, 23421'
  AND Boss.name LIKE 'Peter';

With this system, all bosses are employees (which they logically are!), employees can have more than one, or no boss.

Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91
0

You dont. Do you have an engineering reason you need to do this?

Making two queries simultaneously is still going to hit the same database, and the database is going to do the same amount of work. Its not going to make anything faster, and, you'll have the overhead of the additional threads/processes being created.

If you really need better concurrency, consider a 2nd (or 3rd or 4th) real-time replicated database for SELECT queries, to offload some of the work from the main database.

GrandmasterB
  • 3,396
  • 1
  • 23
  • 22