简体   繁体   中英

mysql_query doesn't show all query

I want to show all distinct id_did in cc_did_use

function getdidbycc($cccard_from_sipcard){
    $result = mysql_query("SELECT id_did from cc_did_use where id_cc_card='$cccard_from_sipcard'");
    if ($row = mysql_fetch_array($result)) 
    {
    $text = $row['id_did'];
    }
    return $text;
}

I have two id_cc_card="31" and one that value has id_dd="14" and another one = "13" but result just show first one.

How I can show both of them?

and after that I have another function

function get_did ($p){

$result = mysql_query("SELECT did FROM cc_did WHERE id ='$p'");
while ($row = mysql_fetch_array($result)) 
{
 echo $row['id_did'].'<br/>';
}
}

when I run getdidbycc function , it returns two value , 13 and 14 , How I can get did numbers from this two values?

You are fetching result and checking in an if condition which will execute only once.

if ($row = mysql_fetch_array($result)) 
{
    $text = $row['id_did'];
}

You have to fetch the result until there is value in the resultant array. So try with while loop like,

 while($row = mysql_fetch_array($result))   // while there are records
 {
    $text[] = $row['id_did'];    // store the result in array $text
 } 
 return $text;    // return the array

First of all try to use mysqli or pdo instead of mysql otherwise you will face issues in updated version of php

As per your code $text value is gets over write due to loop so use an array something like this

if ($row = mysql_fetch_array($result)) {
    $text[] = $row['id_did'];
  }

return $text;

or you can just return complete data as return $row;

if ($row = mysql_fetch_array($result)) 
    {
    $text = $row['id_did'];
    }

change the above line to this

while($row = mysql_fetch_array($result)) 
    {
    echo $row['id_did'].'<br/>';

    }

Use a while loop like that : http://php.net/manual/en/function.mysql-fetch-array.php

Mysql fetch array returns the current element only

if ($row = mysql_fetch_array($result)) 
{
    $text[] = $row['id_did'];
}
return $text; // returns both

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