Jump to content

Parse error


kpetsche20

Recommended Posts

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>";
	  }
	   ?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

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.