简体   繁体   中英

Get multiple values from table, then echo them

I have a PHP script that is trying to get multiple values from a table and store them in an array. I am querying the table for "camtronius2" and I want to get the associated values for "rank" and "guild" EACH time "camtronius2" is found.

Here Is My Table

So far from the code below I am able to get one of the values "BRNT", but there is another value that is not being shown. I should be seeing an output of "BRNT" and "BAMM" for guilds. I havent started trying to get the "rank" part yet...

Could someone help me? See code below:

$invcheckquery = "SELECT _to, _from, rank, guild FROM guildinvite WHERE _to ='" . $username . "';"; 
$invcheck = mysqli_query($con, $invcheckquery) or die("error code : invite already sent!"); 
$invinfo = mysqli_fetch_assoc($invcheck);

$check_num_rows=mysqli_num_rows($invcheck);

while($row = mysqli_fetch_array($invcheck, MYSQLI_ASSOC))
{
echo ($row['guild']);  // The number
}

You have two fetches, one outside the loop, and one inside, I am guessing you want something more like this:

$invcheckquery = "SELECT _to, _from, rank, guild FROM guildinvite WHERE _to ='" . 
$username . "';"; 
$invcheck = mysqli_query($con, $invcheckquery) or die("error code : invite already sent!"); 
while ($invinfo = mysqli_fetch_assoc($invcheck)) {
{
      echo ($row['guild']);  // The number
}

Not exactly sure, why you are getting the number of rows, when you aren't in your snippet using it.

Just try this:

$invcheckquery = "SELECT * FROM guildinvite WHERE _to='$userName'";

I have checked this on my local machine and found it working.

 $invcheck = mysqli_query($con, $invcheckquery);

 while($row = mysqli_fetch_array($invcheck, MYSQLI_ASSOC)) {
    echo ($row['guild'] . '----');  // The number
 }

Output:

BRNT----BAMM----

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM