Jump to content

mysql_fetch_array


jwk811

Recommended Posts

this is my code
[code]
$sql = "SELECT price FROM selling";
$result = mysql_query($sql);
$price = mysql_fetch_array($result);
[/code]
the price column has more than one entry but $price[1] and so on arent coming up.. only $price[0] has a value.. how come the rest in that column arent apart of the array?
thanks for the help!
Link to comment
Share on other sites

Becasue your only selecting one field ( price ) in your query.

[code]
"SELECT price FROM selling";
[/code]

If your looking at displaying more than one record (in which case the wording of your question is a bit obscured) from this field you need a loop. eg;

[code=php:0]
while ($row = mysql_fetch_array($result)) {
  echo $row[0];
}
[/code]
Link to comment
Share on other sites

okay you are overcomplicating it.

[code]
$sql = "SELECT price FROM selling";
$result = mysql_query($sql);
while ($price = mysql_fetch_array($result)) {
  $priceList[] = $price;
}
[/code]
okay now you have an array called $priceList. Each position in the array is a "row" returned from your query. so:
[code]
$sql = "SELECT price FROM selling";
$result = mysql_query($sql);
while ($price = mysql_fetch_array($result)) {
  $priceList[] = $price;
}

echo $priceList[1]; // echos the 2nd one
// echos all of them
foreach ($priceList as $val) {
  echo "$val <br/>";
}

[/code]
Link to comment
Share on other sites

[quote]ok the whole row it coming up with $row[0].. that how its supposed to be? because $row[1] has errors.. how can i break it down?[/quote]

There is no $row[1]. Your only selecting one field, so for each record (each time through the while loop), this fields data will show up in $row[0].

You really need to read some tutorials on this stuff, your whole approuch is bent out of shape.
Link to comment
Share on other sites

i'm a noob, as well, and this is how i do it...
[code]
<?php
$sql = "SELECT price FROM selling";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);

echo "Number of results: ". $num_rows ."<br />";

while($row = mysql_fetch_array($result)){
echo $row ."<br />";
}
?>
[/code]
you can also call the value of each column in a sql table in whatever order you choose like this.

[code]
<?php
$sql = "SELECT price FROM selling";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);

echo "Number of results: ". $num_rows ."<br />";

while($row = mysql_fetch_array($result)){
echo $row['column1'] ." | ". $row['column2'] ."<br />";
}
?>
[/code]
Link to comment
Share on other sites

[quote author=boo_lolly link=topic=116541.msg474892#msg474892 date=1164684788]
i'm a noob, as well, and this is how i do it...
[code]
<?php
$sql = "SELECT price FROM selling";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);

while($row = mysql_fetch_array($result)){
echo $row ."<br />";
}
?>
[/code]
you can also call the value of each column in a sql table in whatever order you choose like this.

[code]
<?php
$sql = "SELECT price FROM selling";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);

while($row = mysql_fetch_array($result)){
echo $row['column1'] ." | ". $row['column2'] ."<br />";
}
?>
[/code]
[/quote]

Sorry, but thats just going to confuse the issue. Your first piece of code has an error because $row is an array so needs to be referenced using $row[0] or $row['price'] not just $row. Your second calls two fields $row['column1'] and $row['column2'] which do not exist in the query.

I know your trying to help, but it does confuse things.
Link to comment
Share on other sites

I think it would help clarify this whole array thing if I were to explain it with a query that pulls data from more than 1 field. if you were to do (using this example data in your table):

selling:
price  item
12    blah
56    blah2
72    blah3

"select price, item from selling"

then you would have 2 elements in your fetch array. example:

[code]
$sql = "SELECT price, item FROM selling";
$result = mysql_query($sql);
$price = mysql_fetch_array($result);

echo $price[0]; // echos 12
echo $price['price']; // still echos 12, since you used fetch_array
echo $price[1]; // echos blah
echo $price['item']; // still echos blah, since you used fetch_array
[/code]
notice how $price[1] does not echo out 56, but the 2nd field.  The fetch array returns an array of all of the fields you selected from the query.  so if you select only price, you only have $price[0]. In my example, I selected price and item, so now you have 2 elements, 0 and 1.  If you want to get the next row of values, you need to do another fetch_array.  Each fetch_array function call returns the next row in the result source that you assigned to $result. 

that is why the while loop is introduced.  we retrieve the row from the result source and store that array  in another array, making an array of arrays (a multi-dimensional array). Using my example of pulling data from more than 1 field:
[code]
while ($price = mysql_fetch_array($result)) {
  $priceList[] = $price;
}

echo $priceList[0][0]; // echos 12
echo $priceList[0]['price']; // still echos 12, since you used fetch_array
echo $priceList[0][1]; // echos blah
echo $priceList[0]['item']; // still echos blah, since you used fetch_array
echo $priceList[1][0]; // echos 56
echo $priceList[1]['price']; // still echos 56, since you used fetch_array
echo $priceList[1][1]; // echos blah2
echo $priceList[1]['item']; // still echos blah2, since you used fetch_array

[/code]
Link to comment
Share on other sites

[code]
echo $priceList[0][0]; // echos 12
echo $priceList[0]['price']; // still echos 12, since you used fetch_array
echo $priceList[0][1]; // echos blah
echo $priceList[0]['item']; // still echos blah, since you used fetch_array
echo $priceList[1][0]; // echos 56
echo $priceList[1]['price']; // still echos 56, since you used fetch_array
echo $priceList[1][1]; // echos blah2
echo $priceList[1]['item']; // still echos blah2, since you used fetch_array
echo $priceList[2][0]; // echos 72
echo $priceList[2]['price']; // still echos 72, since you used fetch_array
echo $priceList[2][1]; // echos blah3
echo $priceList[2]['item']; // still echos blah3, since you used fetch_array
[/code]

notice the pattern, the [i]loop[/i] you could make, because of the pattern...
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.