Jump to content

getting a column into an array


freelance84

Recommended Posts

Hi,

I'm pretty new to php & mysql, so this may seem very elementary:

 

$query_mq1 = "SELECT com FROM mq1 WHERE number='$mq1_a'";

 

The above query selects everything from the column named "com" from table "mq1" where it matches variable "$mq1_a" from column "number".

 

The following two lines get the query into a php variable or throw up an error if a problem arrises:

 

$result_mq1 = mysql_query($query_mq1);

if (!$result_mq1) die ("Database access failed: " . mysql_error());

 

 

My question is, is it possible to get these results into one array in one move? I can only seem to find "$row = mysql_fetch_row($result_mq1);" which only gets one result at a time.

 

Any help here would be brilliant as I am pretty stuck.

 

Thanks,

 

John.

Link to comment
Share on other sites

The function you're looking would be mysql_fetch_array.

I suggest using following code first to get the idea how it works:

$array = mysql_fetch_array($query_mq1);
echo '<pre>';
print_r($array);
echo '</pre>';

 

Or if you want to process the data rigth away you could do this:

while($row = mysql_fetch_row($resut_mq1)){
//Process data here
//Use this like:
//$row['field name'] = field value
//for example:
echo $row['username'];
//would print value of every 'username' field.
echo $row['username'] . ' - ' . $row['email'];
//you get the idea?
}

Link to comment
Share on other sites

$mq1 = get_post('mq1');

$query_mq1 = "SELECT com FROM mq1 WHERE number='$mq1'";

$result_mq1 = mysql_query($query_mq1);

if (!$result_mq1) die ("Database access failed: " . mysql_error());

 

$row = mysql_fetch_array($query_mq1);

print_r($row);

 

This returns "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL"

 

If I apply the fetch_array to the $result_mq1:

$mq1 = get_post('mq1');

$query_mq1 = "SELECT comment FROM mq1 WHERE number='$mq1'";

$result_mq1 = mysql_query($query_mq1);

if (!$result_mq1) die ("Database access failed: " . mysql_error());

 

$row = mysql_fetch_array($result_mq1);

print_r($row);

...it just returns one result and not all.

 

Do I need to use the array_push?

Link to comment
Share on other sites

Ok I've got the following code to get all the results of the query into one array, but it doe seem a little OTT for what it does. Is there no simpler way?

 

$row = mysql_fetch_row($result_mq1);

$rows = mysql_num_rows($result_mq1);

for ($j = 0 ; $j < $rows ; ++$j)

{

$content = mysql_fetch_row($result_mq1);

array_push($row,$content);

}

print_r($row);

Link to comment
Share on other sites

Using the more efficient extraction into an array:

 

$data = array();

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

  $data[] = $row;

}

print_r($data)

 

The print_r($data) results in the following:

Array ( [0] => Array ( [0] => test AA [comment] => test AA ) [1] => Array ( [0] => test BB [comment] => test BB ) )

 

Is this now an associative array?

echo (data[0]);    this just returns 'array'

 

How do I just return the 1st result from the array?

Link to comment
Share on other sites

Hi SaMike,

 

cheers, but this still just returns one result but now in an associative array

 

Sorry, I should have used....

 

$data = array();
while ($row = mysql_fetch_assoc($result_mq1)) {
  $data[] = $row;
}

 

$data will now contain all your records. With the first being within $data[0], the second $data[1] etc etc.

Link to comment
Share on other sites

It looks like you might be wanting something more like the following, which brings back the values from the com column.

 

$data = array();
while ($row = mysql_fetch_assoc($result_mq1)) {
    $data[] = $row['com'];
}

 

A more generic version of the above would be to use mysql_fetch_row and $row[0].

Link to comment
Share on other sites

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.