0

I have this table:

------------------------------
|ID | name   | employee_code |
------------------------------
|24 | Robert |    20234      |
------------------------------

AND

-------------------------------------
|ID |   job_code   |     team       |
-------------------------------------
|24 | 241124 | Robert, Eduard, Etc. |
-------------------------------------

I want to search in second table by employee code and i try something like this:

$sql=mysql_query("SELECT * FROM works WHERE (SELECT name FROM employee WHERE employee_code LIKE '%".$_GET['employee_code']."%' AS searchname) team Like %searchname% ");

Result:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
3
  • 1
    your query is failing. try adding mysql_error() to your query - $sql=mysql_query("SELECT * FROM ...) or die(mysql_error()); Commented Jul 9, 2013 at 7:59
  • do you mean job_code from 2nd table must be the same as employee code form 1st one? Commented Jul 9, 2013 at 8:03
  • Something else to note, please consider that mysql functions are now deprecated. Please look at using PDO or mysqli instead. Commented Jul 9, 2013 at 9:11

5 Answers 5

2

Try this query -

$employee_code = mysql_real_escape_string($_GET['employee_code']);

$sql=mysql_query("SELECT w.* 
                  FROM employee e
                  JOIN works w 
                  ON w.team LIKE CONCAT('%', e.name ,'%')
                  WHERE employee_code LIKE '%$employee_code%'");

see this SQLFiddle example - http://sqlfiddle.com/#!2/8f8b7/1

Sign up to request clarification or add additional context in comments.

Comments

0

You should be looking at a join .

select * from table1 inner join table2 using (`ID`) where job_code = ....

Then you have 1 row with both tables joined together

also your using mysql_* functions, These are no longer maintained please update to mysqli_* or PDO.

Also you need to escape your queries, There an SQL injection attack waiting to happen in that code

Comments

0

This would probably tell you exactly what was wrong.

$sql=mysql_query("SELECT * FROM works WHERE (SELECT name FROM employee WHERE employee_code LIKE '%".$_GET['employee_code']."%' AS searchname) team Like %searchname% ");
if (!$sql)
    echo mysql_error();

You should never just assume that your query has worked and then carry on to use the resource in another command without checking that it did in fact work.

Another thing you should not do is just put user input directly into SQL queries without any form of escaping as it will enable anyone to take complete control of your database.

SELECT * FROM works WHERE (SELECT name FROM employee WHERE employee_code LIKE '%".mysql_real_escape_string($_GET['employee_code'])."%' AS searchname) team Like %searchname% "

4 Comments

I know but i post on question only sql query.
ok so what was the error you got but didn't put in your question? Kind of an important bit of information to include wouldn't you think?
@Anigel - Regardless of the error, the query he is trying to use is clearly malformed and invalid. That's the issue.
@webnoob, I get this, but just saying use this query instead does not help the OP learn how to debug his own issues and just increases dependance on other people to answer their problems.
0

Your SQL query is wrong Try like this

 SELECT * FROM works WHERE works.ID=employee.ID AND 
   employee.employee_code=".$_GET['employee_code']."

Comments

0
SELECT * FROM table1 t1 INNER JOIN table2 t2 ON t1.employee_code = t2.job_code

or

SELECT t1.id, t1.name, t2.team FROM table1 t1 INNER JOIN table2 t2 ON t1.employee_code = t2.job_code

for cleaner result

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.