jwk811 Posted November 28, 2006 Share Posted November 28, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/ Share on other sites More sharing options...
trq Posted November 28, 2006 Share Posted November 28, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131330 Share on other sites More sharing options...
jwk811 Posted November 28, 2006 Author Share Posted November 28, 2006 i thought that all the data in that one field would come out in an array? no? how could i do this then? Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131332 Share on other sites More sharing options...
trq Posted November 28, 2006 Share Posted November 28, 2006 Your thinking completely the opisite of how records are actually stored. See above I edited my post already. Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131333 Share on other sites More sharing options...
jwk811 Posted November 28, 2006 Author Share Posted November 28, 2006 so all i have to do is add the while $row = .... and then it will all come up fine? Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131340 Share on other sites More sharing options...
jwk811 Posted November 28, 2006 Author Share Posted November 28, 2006 this isnt working Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131344 Share on other sites More sharing options...
trq Posted November 28, 2006 Share Posted November 28, 2006 Post your code. Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131347 Share on other sites More sharing options...
jwk811 Posted November 28, 2006 Author Share Posted November 28, 2006 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 Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131354 Share on other sites More sharing options...
.josh Posted November 28, 2006 Share Posted November 28, 2006 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 themforeach ($priceList as $val) { echo "$val <br/>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131355 Share on other sites More sharing options...
trq Posted November 28, 2006 Share Posted November 28, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131359 Share on other sites More sharing options...
boo_lolly Posted November 28, 2006 Share Posted November 28, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131363 Share on other sites More sharing options...
trq Posted November 28, 2006 Share Posted November 28, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131366 Share on other sites More sharing options...
.josh Posted November 28, 2006 Share Posted November 28, 2006 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 item12 blah56 blah272 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 12echo $price['price']; // still echos 12, since you used fetch_arrayecho $price[1]; // echos blahecho $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 12echo $priceList[0]['price']; // still echos 12, since you used fetch_arrayecho $priceList[0][1]; // echos blahecho $priceList[0]['item']; // still echos blah, since you used fetch_arrayecho $priceList[1][0]; // echos 56echo $priceList[1]['price']; // still echos 56, since you used fetch_arrayecho $priceList[1][1]; // echos blah2echo $priceList[1]['item']; // still echos blah2, since you used fetch_array[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131372 Share on other sites More sharing options...
jwk811 Posted November 28, 2006 Author Share Posted November 28, 2006 thanks guys! i got it to work! Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131499 Share on other sites More sharing options...
jwk811 Posted November 28, 2006 Author Share Posted November 28, 2006 how would i echo blah3 then? Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131769 Share on other sites More sharing options...
.josh Posted November 29, 2006 Share Posted November 29, 2006 [code]echo $priceList[0][0]; // echos 12echo $priceList[0]['price']; // still echos 12, since you used fetch_arrayecho $priceList[0][1]; // echos blahecho $priceList[0]['item']; // still echos blah, since you used fetch_arrayecho $priceList[1][0]; // echos 56echo $priceList[1]['price']; // still echos 56, since you used fetch_arrayecho $priceList[1][1]; // echos blah2echo $priceList[1]['item']; // still echos blah2, since you used fetch_arrayecho $priceList[2][0]; // echos 72echo $priceList[2]['price']; // still echos 72, since you used fetch_arrayecho $priceList[2][1]; // echos blah3echo $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... Quote Link to comment https://forums.phpfreaks.com/topic/28696-mysql_fetch_array/#findComment-131949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.