Jump to content

Recommended Posts

<?php


// Function to get the User Data
function user_info()
{
			$connection = db_connect();

			$query = "SELECT user.id as id, user.fname, user.lname, user.email, user.cellphone, user.username, user.created_date as created, user.last_login as last, user.login_count as count FROM user";

			$result = mysql_query($query);

			$number_of_users = mysql_num_rows($result);
			if ($number_of_users == 0)
			{
							return false;
			}

			$result = result_to_array($result);

			return $result;


}

// Function to Convert Results into an ARRAY
function result_to_array($result)
{
			// Defining an array
			$result_array = array();
			// Creating the Array of all users
			for ($i = 0; $row = mysql_fetch_array($result); $i++)
			{
							$result_array[$i] = $row;
			}

			// returns the array of all users by Multi Dimensional Array
			return $result_array;
}


// CALLING THE FUNCTION
$row = user_info();

//PRINTING IT
print_r($row);
?>

 

Am getting the RESULTS I WANT.. but am getting duplcates in the array as shown below

 

Array
(
    [0] => Array
        (
            [0] => 1
            [id] => 1
            [1] => Harsha
            [fname] => Harsha
            [2] => Vantagudi
            [lname] => Vantagudi
            [3] => hmvrulz@gmail.com
            [email] => hmvrulz@gmail.com
            [4] => 9901646877
            [cellphone] => 9901646877
            [5] => hmv
            [username] => hmv
            [6] => 2008-10-01 15:28:13
            [created] => 2008-10-01 15:28:13
            [7] => 2008-10-03 15:28:29
            [last] => 2008-10-03 15:28:29
            [8] => 1
            [count] => 1
        )

    [1] => Array
        (
            [0] => 2
            [id] => 2
            [1] => Vachana
            [fname] => Vachana
            [2] => Vantagudi
            [lname] => Vantagudi
            [3] => vachana@gmail.com
            [email] => vachana@gmail.com
            [4] => 9916681420
            [cellphone] => 9916681420
            [5] => vachu
            [username] => vachu
            [6] => 2008-10-02 16:36:11
            [created] => 2008-10-02 16:36:11
            [7] => 2008-10-03 16:36:15
            [last] => 2008-10-03 16:36:15
            [8] => 1
            [count] => 1
        )

)

 

Guess the problem lies in my  result_to_array() function. can some one give an alternate solution or modify mine.

The issue is that am getting the output but in duplicates with the index value aslo

Link to comment
https://forums.phpfreaks.com/topic/126917-solved-mysql-query-into-an-array/
Share on other sites

assuming you only have two columns in your row:

 

$new_array = array();
$row = mysql_fetch_array($result)
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    $new_array["$row[0]"] => $row[1];
}
// returns the array of all users by Multi Dimensional Array
return $new_array;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.