开发者

PHP and Mysql get data and print

开发者 https://www.devze.com 2023-02-14 09:25 出处:网络
I query from database in one query \"SELECT username, name, surnaname FROM users\" its 3 rows USERNAME, NAME, SURNAME each has 2 values

I query from database in one query "SELECT username, name, surnaname FROM users" its 3 rows USERNAME, NAME, SURNAME each has 2 values i need to print in browser NAME + SURNAME + USERNAME. code looks like this

$users = mysql_query("SELECT username, name, surnaname FROM users");
$N=1;
while($cell = mysql_fetch_array($users)) { 
  foreach ($cell as $key => $value) {
    if ($key == "username") {    
     $array_of_usernames[n] = $value;
    } elseif ($key == "name") {
        $array_of_names[n] = $value; 
      } elseif ($key == "surname") {
          $array_of_surnames[n] = $value;
    $N++;
  }
};
for (N=1; N = count($array_of_usernames); N++) {
echo "Username: ".$array_of_usernames[N]." Name: ".$array_of_names[n]." Surname: ".$array_of_surnames[n];
}

the problem is $N increase each loop and only one $key and $value (header and cell value) extracted from database per loop. I think it can be fixed if i create 3 counters A,B,C for each array and increase each on if match (just figured while i was writing this question)...

But is there more efficient way to do what i need because this seems to complicated and not universal.

The reason why i 开发者_C百科even started to do it this way is that $users is not array i needed to get array somehow if there's way for this i appreciate for any info.


$users = mysql_query("SELECT username, name, surnaname FROM users");

while($cell = mysql_fetch_assoc($users)) { 
  echo "Username: ".$cell["username"]." Name: ".$cell["name"]." Surname: ".$cell["surname"];
}


Well, you don't need all this code :

$result = mysql_query('SELECT username, name, surname FROM users');
while($user = mysql_fetch_array($result)) { 
    echo 'Username: ',$user['username'],' Name: ',$user['name'],' Surname: ',$user['surname'];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消