tags:

views:

70

answers:

7

I have the following code and want to manually select an array:

<?php

 $articleQuery = mysql_query("SELECT * FROM articles WHERE topic = 'IT' ");


 while($article= mysql_fetch_array($articleQuery )){
  $aid = $article['id'];
  $media = $article['media'];
  $link = $article['link'];
 }


 echo $aid[0];

?>

The problem is that its not really selecting/displaying the correct information. What I want is to be able to select the value of the first array.

Thanks in advance.

+1  A: 
$firstrow = null;
while($article= mysql_fetch_array($articleQuery)) {
    if ($firstrow === null)
        $firstrow = $article;

    $aid = $article['id'];
    $media = $article['media'];
    $link = $article['link'];
    //manipulate $aid, $media and $link.
}

//manipulate $firstrow

If you only need the first row, limit the query to one result and execute mysql_fetch_array at most once, instead of in a loop.

Artefacto
I don't want to be limited to selecting the first array. I want to be able to select any position of the array.Doing this: $aid[0] should work but for some reason it only display a single character of the information in that array.I don't want to reset the array counter because I want to be able to use any of them.
Farax
@Farax That's a new requirement. Then go with @Sarfraz's answer. Beware you'll be keeping the whole result in memory, which you ought to avoid if you can. Additionally, you should be using the mysqli extension.
Artefacto
+1  A: 

Or you can do like this:

 $array = array();
 while($article = mysql_fetch_object($articleQuery )){
  $array[] = $article;
 }

echo $array[0]->id; // get first id
echo $array[0]->media; // get first media
echo $array[0]->link; // get first link

echo $array[1]->id; // get second id
echo $array[1]->media; // get second media
echo $array[1]->link; // get second link
// and so on.......
Sarfraz
A: 

if you want $aid to be an array, you should do something like that:

$aid = array();
while($article= mysql_fetch_array($articleQuery )){
    $aid[] = $article['id'];
}
kgb
This is what I was looking for. Thank you very much!
Farax
A: 

The problem is that its not really selecting/displaying the correct information. What I want is to be able to select the value of the first array.

What you want is propably this:

$articleQuery = mysql_query("SELECT * FROM articles WHERE topic = 'IT' ");
$article= mysql_fetch_array($articleQuery);
echo $article[0];

You have unnecessary loop. You can also add "limit 1" to sql query. Although I'm not sure I understand your goal correctly.

zifot
A: 

Using mysql_fetch_assoc, will use the field names as the array indexer, so $article['id'] instead of $article[0]. That way if you change the definition of the table by adding new columns, your code won't break!

$articleQuery = mysql_query("SELECT * FROM articles WHERE topic = 'IT' LIMIT 1");
$article= mysql_fetch_assoc($articleQuery);
var_dump($article);
orvado
A: 

If you really only want the first result:

$articleQuery = mysql_query("SELECT * FROM articles WHERE topic = 'IT' LIMIT 1"); // note LIMIT clause
if( false !== ($article = mysql_fetch_array($articleQuery )))
{
    $aid   = $article['id'];
    $media = $article['media'];
    $link  = $article['link'];
}
echo $aid;

If you want them all, but indexable:

$articleQuery = mysql_query("SELECT * FROM articles WHERE topic = 'IT' ");
while($article= mysql_fetch_array($articleQuery ))
{
    $aid[]   = $article['id'];
    $media[] = $article['media'];
    $link[]  = $article['link'];
}
echo $aid[0];
Kris
A: 

Why not edit your SQL statement to select only one item?

mysql_query("SELECT * FROM articles WHERE topic = 'IT' LIMIT 1");

But the error in your code, is that you're looping over all your selected records, overwriting your variables on each pass. If you want to store all the rows as an array, you should modify your syntax like this:

while($article= mysql_fetch_array($articleQuery )){
  $aid[] = $article['id'];
  $media[] = $article['media'];
  $link[] = $article['link'];
}

...after which you could access the first row with aid[0].

But instead I'd suggest a different structure:

while($article= mysql_fetch_array($articleQuery )){
  $articles[]['aid'] = $article['id'];
  $articles[]['media'] = $article['media'];
  $articles[]['link'] = $article['link'];
}

What that does, is collect all the data into a single data structure, where each record holds all the data related to the article. You would access it like this:

echo $articles[0]['aid']; 
echo $articles[0]['media'];
echo $articles[0]['link'];

If this looks like hebrew to you, take a look at the PHP manual section for arrays.

nikc