如何在不连接表的情况下在PHP中执行2个或更多SQL查询

时间:2016-04-05 12:58:01

标签: php mysql sql mysqli multi-query

使用以下代码从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()) { ?>
...

你有什么想法吗?

1 个答案:

答案 0 :(得分: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