MySQL Forums
Forum List  »  Newbie

Select an array from one databank and compare it to one from another databank
Posted by: Frank Olschewski
Date: November 24, 2015 01:35PM

I know the title is not the best, but it somehow summarizes the problem: I run a page on which clients can place a request for a mentor in a specific subject. Now I have a databank for my mentors with their particular subjects and a databank for the incoming requests from the clients. What I want to do now is, to write an SQL Command that compares the subject of the incoming request to the subjects of my mentors, so that the teachers, who teach this subject, will see the request when they log in. So as an example: Mother Anita asks for a teacher in maths -> sends the request. The code checks which teachers teach maths und display the request to those teachers when they log in. Please keep in mind that I am a bloody beginner.

The code I currently is the following:

// Connect to the teacher databank and select the subjects of the teacher that has logged in

$connect = mysql_connect('xxx', 'xxx', 'xxx');
mysql_set_charset("utf8", $connect);
mysql_select_db('xxx');

$sql = "SELECT subjects FROM teachers WHERE teacher LIKE '%". $_SESSION['user'] ."%'" ;
$erg = mysql_query(@$sql);

if (false === $erg) {
die (mysql_error());
}

$speicher = mysql_fetch_array($erg, MYSQL_ASSOC);
print_r ($speicher);  

// Until here it functions and the array ($speicher) contains the subjects of the teacher

// Now connect to the other Databank (with the requests)

$connect = mysql_connect('xxx', 'xxx', 'xxx');
mysql_set_charset("utf8", $connect);
mysql_select_db('xxx');

$sql = "SELECT street, class, subject, school FROM requests WHERE subject IN ('%s')";

$sql1 = sprintf($sql, join("','", $speicher));

echo $sql1;

// It correctly spills out the SELECT Query but it is wrong because of the '' around the subjects are missing -> see output

$erg = mysql_query($sql1);

if (false === $erg) {
die (mysql_error());
}

$content = mysql_fetch_array($erg, MYSQL_ASSOC);
print_r ($content);

And the output from this code:

SELECT street, class, subject, school FROM requests WHERE subject IN ('English, German, Mathematics')

So I don't know how to set it up that it is correct, currently it does not work because of the missing '' between the subjects, but I don't know any other way. I am quite much a beginner. And I know, I really need to change it to MySQLi and I will do it as soon as this problem is solved.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.