Jump to content

Recommended Posts

Hi there,

 

Just a simple question for all you coders.

I'm trying to make a PHP Array from MySQL results.

 

The two rows are 'players' and 'time'. The players row has plain numerical data and time, time postings (11:00 and so on).

 

The output's of the two arrays I'm after are in the formats below :

(example)

1,2,10,5,32,5,0

(example time)

"11:00", "11:15", "11:30",

The code I'm currently attemping with is :

while($row = mysql_fetch_array($mysql_result))
{

     $dataplayers[ $row['players'] ] = array();

     $datatime[ $row['time'] ] = array();
}

As it stands, I seem to be getting no output at all.

 

Thanks for your help in advanced!

Link to comment
https://forums.phpfreaks.com/topic/284033-simple-php-array-from-mysql-row/
Share on other sites

Not sure if I understand, but maybe

$dataplayers = $datatime = array();
while($row = mysql_fetch_array($mysql_result))
{
	$dataplayers[] = $row['players'];
	$datatime[] = $row['time'];
}
Also, stop using the mysql extension and its mysql_* functions and move to PDO or mysqli. Why? Because it's old, insecure, slow, and no longer supported with PHP.

Not sure if I understand, but maybe

$dataplayers = $datatime = array();
while($row = mysql_fetch_array($mysql_result))
{
	$dataplayers[] = $row['players'];
	$datatime[] = $row['time'];
}
Also, stop using the mysql extension and its mysql_* functions and move to PDO or mysqli. Why? Because it's old, insecure, slow, and no longer supported with PHP.

 

 

Thanks for the reply. The code works fine, but when I print the array to test the output I'm getting the below :

Array
(
    [0] => 17
    [1] => 14
    [2] => 11
    [3] => 11
    [4] => 7
    [5] => 5
)

Array
(
    [0] => 21:15
    [1] => 21:00
    [2] => 20:45
    [3] => 20:30
    [4] => 20:15
    [5] => 20:00
)


I need to try get it to array in the format below (if it's possible). I know it's possible manually.

Array(17,14,11,11,7,5)

Array("21:15", "21:00", "20:45", "20:30", "20:15", "20:00")


I'll try to explain briefly what it's for. The data is being pulled from the database to be used with PChart to display a line graph. The below example is how PChart requests the data to be displayed :

 

$myData->addPoints(array(-39,17,32,41,-48,47,29,45),"Series1");

 

This is what I was planning to do if I can get the array working...

 

$myData->addPoints($datatime,"Series1");

 

Again thanks for your time. Realise I'm maybe doing things a little backwards here, Apologies for my scripting.

so use

$myData->addPoints($datatimes,"Series1");
echo '<pre>';
print_r( Array(17,14,11,11,7,5) );
echo '<pre>';

produces

Array
(
    [0] => 17
    [1] => 14
    [2] => 11
    [3] => 11
    [4] => 7
    [5] => 5
)

exactly the same as Requinix's 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.