0

my database Contain table user -> fields -> username,userid

    $user = (" SELECT userid,username FROM user ");
    while ($row = @mysql_fetch_assoc( $user) )
    {
   $getalluser[] = $row;
    }

i try to get results in array like this :

$users = array(
 0=>array(
  "Mark",
  "http://localhost/test/image.php?u=1",
  "Mark"
 ),
 1=>array(
  "Anandu",
  "http://localhost/test/image.php?u=2",
  "anandu"
 ),
  2=>array(
  "jeena",
  "http://localhost/test/image.php?u=3",
  "jeena"
 )
);

NOTE :

Mark,Anandu,jeena --> username AND u=1,u=2,u=3 --> userid

omar dealo
  • 93
  • 9
  • 1
    define the url so you don't repeat yourself: `$url = 'http://localhost/test/image.php?u=';`. Then, inside the loop: `$getalluser[] = array( $row['username'], $url . $row['userid'] );` (I didn't include the user twice cause I see no need for it) – FirstOne Jul 27 '16 at 17:50
  • 2
    Please, [stop using mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the [MySQLi](http://php.net/manual/en/book.mysqli.php) or [PDO_MySQL](http://php.net/manual/en/ref.pdo-mysql.php) extension should be used. See also [MySQL: choosing an API](http://php.net/manual/en/mysqlinfo.api.choosing.php) guide and [related FAQ](http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated) for more information. – FirstOne Jul 27 '16 at 17:52
  • @FirstOne , thanks bro .. i will try ur code now – omar dealo Jul 27 '16 at 18:03
  • 1
    My comment might not work properly.. it's just a hint. For example, I just realized you don't really _need_ to declare the url before xD - There might be other things I didn't realize – FirstOne Jul 27 '16 at 18:12
  • @FirstOne , no it's worked as well – omar dealo Jul 27 '16 at 18:40

1 Answers1

0

I have PHP7 installed so I can't use the mysql_xxx functions you are using (they are deprecated). I had to use mysqli_xxx functions in order to test next code:

<?php
$cnx = mysqli_connect( "localhost", "root", "pass", "databasename" );
$data = mysqli_query( $cnx, "SELECT userid,username FROM user" );
$getalluser = array();
while ( $row = mysqli_fetch_array( $data ) )
  $getalluser[] = array( $row[ "username" ],
                         "http://localhost/test/image.php?u=" . $row[ "userid" ],
                         $row[ "username" ] );
var_dump( $getalluser );
?>

An empty array is created before the while. In the while the array is filled with arrays, these arrays contain 3 items each : the username as first and third items, and the URL as second item. This second item has the userid concatenated at the end.