Nasıl table1.UID = 1 ve table2.UID = 2 ve adını onlara erişmek için değerler elde edebilirsiniz?

0 Cevap php

Bu iki veritabanı tabloları düşünün:

  Users
UID Name
1   Shawn
2   Sean

  Photos
UID Description
1   Beauty
2   Ugliness

Ben bu gibi bir dizi almak istiyorum:

Array
(
    [0] => Array
        (
            [UID] => 1
            [Description] => Ugliness
        )
)

UID 1 fark, ancak Açıklama Fotoğraflar tablonun UID 2. karşılık gelir. Aslında ne istiyorum tablo Kullanıcılar UID 1 için bilgi almak, ancak tablo Resimler UID 2 etmektir. Yani teorik olarak, Bu gibi benim SQL yazarsınız:

SELECT * FROM Users, Photos WHERE Users.UID = '1' AND Photos.UID = '2'

Here are my attempts at obtaining what I wish:

$myArray_both = array();
$sql = "SELECT * FROM Users, Photos WHERE Users.UID = '1' AND Photos.UID = '2'";
$results = mysql_query($sql);
while($result = mysql_fetch_array($results, MYSQL_BOTH))    // MYSQL_BOTH is the default behaviour but I added it in for clarity
{
    array_push($myArray_both, $result);
}
echo("<h2>Using MYSQL_BOTH</h2>");
echo("<pre>");
print_r($myArray_both);
echo("</pre>");
/* This has the following output:
Array
(
    [0] => Array
        (
            [0] => 1
            [UID] => 2
            [1] => Ugliness
            [Description] => Ugliness
        )
)
**which is not good for me because I want to be able to access the UID with $result['UID'], but I want the UID returned to be 1, not 2.**
*/

$myArray_assoc = array();
$sql = "SELECT * FROM Users, Photos WHERE Users.UID = '1' AND Photos.UID = '2'";
$results = mysql_query($sql);
while($result = mysql_fetch_array($results, MYSQL_ASSOC))
{
    array_push($myArray_assoc, $result);
}
echo("<h2>Using MYSQL_ASSOC</h2>");
echo("<pre>");
print_r($myArray_assoc);
echo("</pre>");
/* This has the following output:
Array
(
    [0] => Array
        (
            [UID] => 2
            [Description] => Ugliness
        )
)
**This also doesn't work because I want to obtain UID 1, not 2**
*/

$myArray_num = array();
$sql = "SELECT * FROM Users, Photos WHERE Users.UID = '1' AND Photos.UID = '2'";
$results = mysql_query($sql);
while($result = mysql_fetch_array($results, MYSQL_NUM))
{
    array_push($myArray_num, $result);
}

echo("<pre>");
print_r($myArray_num);
echo("</pre>");
/* This has the following output:
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => Ugliness
        )
)
**This gets the correct values, but I can't access them by name..**
*/
mysql_close($liendb);   // Closes the database connection
?>

Yani! Ben ne yapabilirim?

0 Cevap