kpetsche20 Posted June 28, 2008 Share Posted June 28, 2008 Here the error Parse error: syntax error, unexpected '[', expecting ',' or ';' in /home/webmaste/public_html/cuz/articles.php on line 29 Here the code <?php $sql = "SELECT * FROM articles"; $run = mysql_query($sql); while($data = mysql_fetch_array($run)) { echo "<a href=\"index.php?p=articles&name=".data['name']."\">".data['name']."</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/112335-parse-error/ Share on other sites More sharing options...
anthrt Posted June 28, 2008 Share Posted June 28, 2008 echo "<a href=\"index.php?p=articles&name=".data['name']."\">".data['name']"</a>"; You're missing $ before your data variables. Quote Link to comment https://forums.phpfreaks.com/topic/112335-parse-error/#findComment-576716 Share on other sites More sharing options...
paul2463 Posted June 28, 2008 Share Posted June 28, 2008 try this <?php $sql = "SELECT * FROM articles"; $run = mysql_query($sql) or die ("error in query " . mysql_error()); //just in case you query is duff while($data = mysql_fetch_assoc($run)) { echo "<a href=\"index.php?p=articles&name=".$data['name']."\">".$data['name']."</a>"; } ?> if you are using fetch_array you have to use the numbered key of the data you wish to get (i.e $data[1]), using fetch_assoc allows you to use the name of the column for the data(i.e $data['name']), but either way unless you use $data it wont understand you are calling for information from an array object hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/112335-parse-error/#findComment-576722 Share on other sites More sharing options...
DJTim666 Posted June 28, 2008 Share Posted June 28, 2008 if you are using fetch_array you have to use the numbered key of the data you wish to get (i.e $data[1]), using fetch_assoc allows you to use the name of the column for the data(i.e $data['name']) Not true.. I'm not 100% sure of the difference between fetch_array and fetch_assoc. However I do know that I use fetch_array all the time and I call with the field names; not numbers. Wouldn't wanna get the newbs confused as soon as they come here, now would we. P.S. kpetsche20, you're just missing the $ in front of your data array as stated in the first reply. -- DJ Quote Link to comment https://forums.phpfreaks.com/topic/112335-parse-error/#findComment-576739 Share on other sites More sharing options...
.josh Posted June 28, 2008 Share Posted June 28, 2008 fetch_array returns both a numerical and associative array, so for example in his code he has a column named 'name'. So if we were to do this: <?php $sql = "SELECT name, id FROM articles"; $run = mysql_query($sql); while($data = mysql_fetch_array($run)) { $list[] = $data; } foreach($list as $key => $val) { echo "key: $key val: $val <br />"; } The output would look like this: key: 0 val: John key: name val: John key: 1 val: James key: name val: James key: 2 val: Joe key: name val: Joe // etc... As you can see, using fetch_array will return a merged numerical and associative array. fetch_row returns just a numerical array (0,1,2...) fetch_assoc returns just an associative array ('name' ...) And now for a bit of overkill: fetch_array accepts an optional 2nd argument to specify that you want just a numerical or associative array returned, making it in essence, just like fetch_row or fetch_assoc Quote Link to comment https://forums.phpfreaks.com/topic/112335-parse-error/#findComment-576756 Share on other sites More sharing options...
.josh Posted June 28, 2008 Share Posted June 28, 2008 okay well that's not entirely accurate: the foreach wouldn't return it that way. <?php $sql = "SELECT name, id FROM articles"; $run = mysql_query($sql); while($data = mysql_fetch_array($run)) { $list[] = $data; } foreach($list as $key => $val) { echo "key: $key val: $val <br />"; } $list would be a 2d array. each element on the first level would coincide with the row returned from your query. On the 2nd level, you will have a 2 element array: first element will be the numerical index, 2nd element will be the associative index. Both of them will hold the same value. so if you did a nested foreach loop: foreach ($list as $l) { foreach ($l as $key => $val) { echo "key: $key val: $val <br/>"; } } output would be: key: 0 val: John key: name val: John key: 0 val: James key: name val: James key: 0 val: Joe key: name val: Joe Hope that clears it up. Quote Link to comment https://forums.phpfreaks.com/topic/112335-parse-error/#findComment-576763 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.