You are not logged in.

  • "Sebastian S." is male
  • "Sebastian S." started this thread

Posts: 595

Location: Stuttgart

Occupation: KFZ-Mechatroniker

  • Send private message

1

Friday, August 17th 2012, 4:48pm

Mehrere Werte in einer Abfrage

Guten Tag, ich möchte gerne mehrere Werte in einer Abfrage herausholen und diese entsprechend ablegen. Dafür habe ich mir eine Abfrage geschrieben, die allerdings nicht funktioniert.

Gibt es eine Möglichkeit, das umzusetzen? Hier mal mein versuch

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$sql "SELECT pid1, pid2, pid3, pid4 
    AS pid1, pid2, pid3, pid4
    FROM ".METIN2_PLAYER_INDEX_TABLE."
    WHERE id = ".$this->accountID."";
$result $newDB->sendQuery($sql);
$row $newDB->fetchArray($result);
$this->firstPID $row['pid1'];
$this->secondPID $row['pid2'];
$this->thirdPID $row['pid3'];
$this->fourthPID $row['pid4'];
print_r($row['pid1']);
print_r($row['pid2']);
print_r($row['pid3']);
print_r($row['pid4']);
exit;


#edit: schon gut, dass die Klammern so eine größe Auswirkung haben. Nur entfernen und gut ist..
##edit: Wieso bleiben die Rückgabewerte alle auf 0? mindestens pid1 ist mit dem wert 1439644 in der Tabelle gefüllt.
Mit freundlichen Grüßen
Sebastian S.

This post has been edited 2 times, last edit by "Sebastian S." (Aug 17th 2012, 7:03pm)


2

Saturday, August 18th 2012, 8:43am

Select pid1, pid2, pid3, pid4 FROM ...
oder
SELECT pid1 AS firstPID, pid2 AS secondPID,

  • "Sebastian S." is male
  • "Sebastian S." started this thread

Posts: 595

Location: Stuttgart

Occupation: KFZ-Mechatroniker

  • Send private message

3

Saturday, August 18th 2012, 1:03pm

Alles klar, danke dir :)
Geht das eigentlich auch, wenn ich verschiedene Bedinungen aber nur eine Spalte hab? Hab das folgende Query 4 mal stehen, das einzige, das anders ist ist der Wert der gescrhieben wird und die WHERE clause - dort ändert sich die vorrausgegangene PID von 1 - 4

PHP Source code

1
2
3
4
5
6
$sql "SELECT ".METIN2_PLAYER_JOB_COLUMN." AS fourthJob
    FROM ".METIN2_PLAYER_TABLE."
    WHERE ".METIN2_ACCOUNT_ID_COLUMN." = ".$this->fourthPID."";
$result $newDB->sendQuery($sql);
$row $newDB->fetchArray($result);
$this->fourthJob $row['fourthJob'];
Mit freundlichen Grüßen
Sebastian S.

This post has been edited 1 times, last edit by "Sebastian S." (Aug 18th 2012, 1:10pm)


4

Saturday, August 18th 2012, 2:47pm

Was genau hast du vor?
Du kannst eine Spalte abfragen und eine andere als die abfragte in die WHERE-Bedingungen setzen,ja

  • "Sebastian S." is male
  • "Sebastian S." started this thread

Posts: 595

Location: Stuttgart

Occupation: KFZ-Mechatroniker

  • Send private message

5

Saturday, August 18th 2012, 2:58pm

also ich habe ja die PID mit dem query, firstPID, - fourthPID - die ID brauche ich für die weitere abfrage.

Pseudo datensätze: (id = firstPID - fourthPID, job = firstJob - fourthJob)
id | job
1 | 3
2 | 2
3 | 0
usw.

Jetzt würde ich gerne in firstJob - fourthJob all die "job" werte haben, die mit den ids zusammenhängen, alles aber in einer Abfrage und nicht in 4. Jetzt gerade sieht das so aus

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public function getJob() {
        $newDB = new MySQLDatabaseExtended(METIN2_MYSQLHOSTMETIN2_MYSQLUSERMETIN2_MYSQLPASSWORDMETIN2_MYSQLDATABASE_PLAYERfalsetruetrue);
        
        $sql "SELECT ".METIN2_PLAYER_JOB_COLUMN." AS firstJob
            FROM ".METIN2_PLAYER_TABLE."
            WHERE ".METIN2_ACCOUNT_ID_COLUMN." = ".$this->firstPID."";
        $result $newDB->sendQuery($sql);
        $row $newDB->fetchArray($result);
        $this->firstJob $row['firstJob'];
        
        $sql "SELECT ".METIN2_PLAYER_JOB_COLUMN." AS secondJob
            FROM ".METIN2_PLAYER_TABLE."
            WHERE ".METIN2_ACCOUNT_ID_COLUMN." = ".$this->secondPID."";
        $result $newDB->sendQuery($sql);
        $row $newDB->fetchArray($result);
        $this->secondJob $row['secondJob'];
        
        $sql "SELECT ".METIN2_PLAYER_JOB_COLUMN." AS thirdJob
            FROM ".METIN2_PLAYER_TABLE."
            WHERE ".METIN2_ACCOUNT_ID_COLUMN." = ".$this->thirdPID."";
        $result $newDB->sendQuery($sql);
        $row $newDB->fetchArray($result);
        $this->thirdJob $row['thirdJob'];
        
        $sql "SELECT ".METIN2_PLAYER_JOB_COLUMN." AS fourthJob
            FROM ".METIN2_PLAYER_TABLE."
            WHERE ".METIN2_ACCOUNT_ID_COLUMN." = ".$this->fourthPID."";
        $result $newDB->sendQuery($sql);
        $row $newDB->fetchArray($result);
        $this->fourthJob $row['fourthJob'];
    }
Mit freundlichen Grüßen
Sebastian S.


6

Saturday, August 18th 2012, 3:39pm

Du könntest
SELECT * FROM ... WHERE id=1 or id=2 or ID=3 or ID=4
machen und dann in PHP
foreach(...){
if($row['id']==$this->firstPID) $this->firstJob=$row['job'];
...
if($row['id']==$this->fourthPID) $this->forthJob=$row['job'];
}

7

Saturday, August 18th 2012, 3:57pm

oder mit "IN" :

MySQL queries

1
2
3
SELECT ".METIN2_PLAYER_JOB_COLUMN." AS jobColumn,  ".METIN2_ACCOUNT_ID_COLUMN." AS idColomn
    FROM ".METIN2_PLAYER_TABLE."
    WHERE ".METIN2_ACCOUNT_ID_COLUMN." IN (".$this->firstPID.", ".$this->secondPID.", ".$this->thirdPID.", ".$this->fourthPID.")";

Und dann in der foreach eben jeweils:

PHP Source code

1
if($row['idColomn']==$this->firstPID$this->firstJob=$row['jobColumn'];

usw.

Vom Prinzip ändert sich aber nix ;)

  • "Sebastian S." is male
  • "Sebastian S." started this thread

Posts: 595

Location: Stuttgart

Occupation: KFZ-Mechatroniker

  • Send private message

8

Sunday, August 19th 2012, 2:03am

Mit foreach-schleifen hab ich überhaupt keine Erfahrung. Ich versteh zwar den theoretischen aufbau $var as $key, verstehe aber nicht, wie ich das anwenden kann/soll. Könnte mir das kurz jemand für total blöde erklären?
Mit freundlichen Grüßen
Sebastian S.


9

Sunday, August 19th 2012, 8:14am

Eventuell solltest du vor dem Programmieren von Plugins mal die PHP-Grundlagen durchgehen. ;)

PHP Source code

1
2
3
4
$deinArrayName=array('rot','grün','blau');
foreach($deinArrayName as $farbe){
echo "Farbe: ".$farbe."\r\n";
}


Ergibt
Farbe: rot
Farbe: grün
Farbe: blau

  • "Sebastian S." is male
  • "Sebastian S." started this thread

Posts: 595

Location: Stuttgart

Occupation: KFZ-Mechatroniker

  • Send private message

10

Sunday, August 19th 2012, 2:37pm

Scheinbar mach ich noch was falsch

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public function getJob() {
    $newDB = new MySQLDatabaseExtended(METIN2_MYSQLHOSTMETIN2_MYSQLUSERMETIN2_MYSQLPASSWORDMETIN2_MYSQLDATABASE_PLAYERfalsetruetrue);
        
    $sql "SELECT ".METIN2_PLAYER_JOB_COLUMN." AS job, ".METIN2_ACCOUNT_ID_COLUMN." AS id
        FROM ".METIN2_PLAYER_TABLE."
        WHERE ".METIN2_ACCOUNT_ID_COLUMN." = ".$this->firstPID."
        OR ".METIN2_ACCOUNT_ID_COLUMN." = ".$this->secondPID."
        OR ".METIN2_ACCOUNT_ID_COLUMN." = ".$this->thirdPID."
        OR ".METIN2_ACCOUNT_ID_COLUMN." = ".$this->fourthPID."";
    $result $newDB->sendQuery($sql);
    $row $newDB->fetchArray($result);
        
    foreach($row as $job) {
        if($row['id'] == $this->firstPID$this->firstJob $row['job'];
        if($row['id'] == $this->secondPID$this->secondJob $row['job'];
        if($row['id'] == $this->thirdPID$this->thirdJob $row['job'];
        if($row['id'] == $this->fourthPID$this->fourthJob $row['job'];
    }
    print_r($this->firstPID);
    echo "<br />";
    print_r($this->secondPID);
    echo "<br />";
    print_r($this->thirdPID);
    echo "<br />";
    print_r($this->fourthPID);
    echo "<br />";
    echo "<br />";
    print_r($this->firstJob);
    echo "<br />";
    print_r($this->secondJob);
    echo "<br />";
    print_r($this->thirdJob);
    echo "<br />";
    print_r($this->fourthJob);
    exit;
}


Ergibt:

Source code

1
2
3
4
5
6
7
8
9
1439644
12
0
0

2
0 ##SOLL > 5
0
0
Mit freundlichen Grüßen
Sebastian S.


11

Sunday, August 19th 2012, 3:58pm

PHP Source code

1
2
3
4
5
6
    while($row $newDB->fetchArray($result)) {
        if($row['id'] == $this->firstPID$this->firstJob $row['job'];
        if($row['id'] == $this->secondPID$this->secondJob $row['job'];
        if($row['id'] == $this->thirdPID$this->thirdJob $row['job'];
        if($row['id'] == $this->fourthPID$this->fourthJob $row['job'];
    }

Eine foreach-Schleife ist da komplett fehl am Platz.