dtvnnhh8992 2013-11-30 00:04
浏览 55

PHP mysqli从不同的表中提取数据并使用2个函数回显

I'm stuck coding a part of a website. I'm extracting from the first table the friendship for the account you are using.

Then I make a while to get all usernames of my friends then I query the profiles table to take out other two information like sex and birth date.

My problem is that the first if(query) and while(friend_username) works fine and I can take out all the data and print the list of users but when I do the second query with the second while(sex,birth_date) nothing works and I cant find the problem it's the second day I try to fix it but I can't find a solution..

Sorry for my bad English and also sorry for my bad coding skills.

I don't know what I'm doing wrong.

<?php

$query = "SELECT friend_username FROM friends WHERE username = ? AND amicizia = '1' AND ban = '0' ";
if($stmt = $mysqli->prepare($query))
{
    // bind parameter
    $stmt->bind_param('s', $username );
    // execute
    $stmt->execute();
    // bind result to a variable
    $stmt->bind_result($amici);
    // iterate through the results
    echo "$amici";
    while ($stmt->fetch())
    {
        $queryu = "SELECT sesso , data_nascita FROM profiles WHERE username = ? ";
        if($stmtp = $mysqli->prepare($queryu))
        {
            // bind parameter
            $stmtp->bind_param('s', $amici );
            // execute
            $stmtp->execute();
            // bind result to a variable
            $stmtp->bind_result($sesso, $data_nascita);
            while ($stmtp->fetch())
            {
                // FARE CICLO WHILE CHE ESTRAPOLA LE INFORMAZIONE DALLA TABELLA PROFILES MA SISTEMARE PRIMA HE AL LOGIN COMPILI I CAMPI SULLA TAB PROFILES
                echo "
                <tr>
                <td>
                <div class=\"gallery\" style=\"height: 76px;\">
                <a href=\"img/profilo.jpg\"><img class=\"align-left\" src=\"img/amico.png\" width=\"60\" height=\"60\" /></a>
                </div>
                </td>
                <td>
                <ul style=\"height:45px;\">
                <li style=\"padding: 0;\">$amici</li>
                <li style=\"padding: 0;\">$sesso</li>
                <li style=\"padding: 0;\">$data_nascita</li>
                </ul>
                </td>
                <td>
                <p>Lorem Ipsum è un testo segnaposto utilizzato nel settore della tipografia e della stampa. Lorem Ipsum è considerato il testo segnaposto standard sin dal sedicesimo secolo</p>
                </td>
                <td>
                <a href=\"\"><i class=\"icon-user icon-2x\"></i></a>
                <a href=\"\"><i class=\"icon-envelope icon-2x\"></i></a>
                <a href=\"\"><i class=\"icon-heart-empty icon-2x\"></i></a>
                <a href=\"\"><i class=\"icon-trash icon-2x\"></i></a>
                </td>
                </tr>
                ";
            }
        }
    }
}

?>
  • 写回答

1条回答 默认 最新

  • doukang7858 2013-11-30 00:49
    关注

    If possible, you should use a table join to fetch all required data in one query. Using the tables from your code snippet it could look like this:

    SELECT a.friend_username, b.sesso, b.data_nascita FROM friends as a 
    JOIN profiles as b ON(b.username=a.friend_username)
    WHERE a.username = ? AND a.amicizia = '1' AND a.ban = '0'
    

    To answer your actual question, it is possible to do nested queries but for your example to work you would need to create a second mysql connection (ex: $mysqli2 = new mysqli(...)), and use that in the outer while loop.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错