-1

要从 1 个表中选择数据,我使用以下代码:

if(isset($UserID)) {    

    $users = $con->prepare("
    SELECT DISTINCT
         d.FirstName                
        ,d.LastName                 
        ,d.Picture  
    FROM Details d 
    WHERE d.UserId = ?
    ");

    $users->bind_param('i', $GetUserId);
    $users->execute();

    $users->bind_result(        
        $FirstName,             
        $LastName,          
        $Picture
    );


} else {
    echo "There is no User ID detected, try to refresh browser.";   
}

while ($users->fetch()) { ?>

    <div class="grid-group">
    <span>Personal Details</span>
        <div class="grid-column">
            <div class="grid-item header">First Name </div>             
            <div class="grid-item header">Last Name </div> 
            <div class="grid-item header">Picture </div> 
        </div>
        <div class="grid-column">
            <div class="grid-item"><?php echo $FirstName; ?></div>          
            <div class="grid-item"><?php echo $LastName; ?></div>
            <div class="grid-item"><?php echo $Picture; ?></div>
        </div>
    </div>
<?php } ?>

如何添加 1 个查询以从另一个表中选择一些数据?就像是:

SELECT Foo, Bar, FooBar
FROM Bizz
WHERE UserId = ?

我应该使用mysqli_multi_query吗?但是我怎样才能使用它和bind_result它之后呢?我无法实现正确的语法。或者还有其他方法吗?

我将需要从这样的 2 个表中打印数据:

while ($users->fetch()) { ?>

    <div class="grid-group">
    <span>Personal Details</span>
        <div class="grid-column">
            <div class="grid-item header">First Name </div>             
            <div class="grid-item header">Last Name </div> 
            <div class="grid-item header">Picture </div> 
        </div>
        <div class="grid-column">
            <div class="grid-item"><?php echo $FirstName; ?></div>          
            <div class="grid-item"><?php echo $LastName; ?></div>
            <div class="grid-item"><?php echo $Picture; ?></div>
        </div>
    </div>

    <div class="grid-group">
    <span>Foo Bar</span>
        <div class="grid-column">
            <div class="grid-item header">Foo </div>            
            <div class="grid-item header">Bar </div> 
            <div class="grid-item header">FooBar </div> 
        </div>
        <div class="grid-column">
            <div class="grid-item"><?php echo $Foo; ?></div>            
            <div class="grid-item"><?php echo $Bar; ?></div>
            <div class="grid-item"><?php echo $FooBar; ?></div>
        </div>
    </div>
<?php } ?>

由于某些原因,我无法使用JOINS. 你有什么想法吗?

更新

如果我在下面准备 2 个查询,我会得到空白屏幕:

if(isset($UserID)) {    

    $users = $con->prepare("
    SELECT DISTINCT
         d.FirstName                
        ,d.LastName                 
        ,d.Picture  
    FROM Details  
    WHERE d.UserId = ?
    ");

    $users->bind_param('i', $GetUserId);
    $users->execute();

    $users->bind_result(        
        $FirstName,             
        $LastName,          
        $Picture
    );

    $users2 = $con->prepare("
    SELECT DISTINCT
          Foo               
        , Bar               
        , FooBar 
    FROM Bizz  
    WHERE UserId = ?
    ");

    $users2->bind_param('i', $GetUserId);
    $users2->execute();

    $users2->bind_result(       
        $Foo,           
        $Bar,           
        $FooBar
    );

} else {
    echo "There is no User ID detected, try to refresh browser.";   
}
while ($users->fetch()) { ?>
...

你有什么想法吗?

4

1 回答 1

0

要知道为什么你得到一个空白屏幕,你必须打开 error_reporting,在你的 php 代码的开头添加以下内容,然后再添加:

error_reporting(-1);

还要添加一些代码来显示 MySQL 中可能发生的错误:

if(isset($UserID)) {    

$users = $con->prepare("
SELECT DISTINCT
     d.FirstName                
    ,d.LastName                 
    ,d.Picture  
FROM Details  
WHERE d.UserId = ?
");
if (!$users) {
    echo 'MySQL Connect Error in Query: (' . $mysqli->errno . ') ';
}

$binding = $users->bind_param('i', $GetUserId);
if (!$binding) {
    echo 'MySQL Connect Error in Query: (' . $mysqli->errno . ') ';
}

if($users->execute() == false) {
    echo 'Error: ' . $con->error;
}

$binding_results = $users->bind_result(        
    $FirstName,             
    $LastName,          
    $Picture
);
if (!$binding_results) {
    echo 'MySQL Connect Error in Query: (' . $mysqli->errno . ') ';
}

$users2 = $con->prepare("
SELECT DISTINCT
      Foo               
    , Bar               
    , FooBar 
FROM Bizz  
WHERE UserId = ?
");
if (!$users2) {
    echo 'MySQL Connect Error in Query: (' . $mysqli->errno . ') ';
}

$binding2 = $users2->bind_param('i', $GetUserId);
if (!$binding2) {
    echo 'MySQL Connect Error in Query: (' . $mysqli->errno . ') ';
}

if($users2->execute() == false) {
    echo 'Error: ' . $con->error;
}

$binding_results2 = $users2->bind_result(       
    $Foo,           
    $Bar,           
    $FooBar
);
if (!$binding_results2) {
    echo 'MySQL Connect Error in Query: (' . $mysqli->errno . ') ';
}

我可以在您的第一个查询中看到一个错误。您正在使用 d.FirstName ,其中表名是 Details

于 2016-04-05T13:18:12.973 回答