Jump to content

need some help with php and mysql


dalcacio2007

Recommended Posts

hello everyone i usually try to google for the answears and learn on my own but right now i'm really stuck...

 

 

What i need is to query a database and echo the value of every field in the row and then move to the next row.

now each row has way too many field.  i thought i could avoid specifying $row['field'] by doing a foreach loop since $row is an array... but apparently something went wrong... the values are coming out repeated the output for this is

--(row1)--

value1

value1

value2

value2

... and so on

--(row2)--

value1

value1

value2

value2

... and so on

 

any idea what could be going wrong?

here's my code

 

Thanks for ur help :D

<?php
$con = mysql_connect("(ipaddress)","(user)","(pass)");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("(database)", $con);
$msqlq = "SELECT * FROM (table)";
$result = mysql_query($msqlq) or die(mysql_error());  
while($row = mysql_fetch_array( $result ))
{
	foreach($row as $field)
	{
	  echo $field.'<br>';//the <br> is just for debuggin purposes 
	}
}
mysql_close($con);


?>

Link to comment
Share on other sites

From the manual:

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

 

The default is both. So all elements are available by their column name and a numerical index. Either add the appropriate parameter to mysql_fetch_array() to get only one set of data or use mysql_fetch_assoc() or mysql_fetch_row().

 

EDIT: You posted your "any ideas?" comment while I was preparing this reply. If I had seen it before I wrote the above I would not have even bothered with a response. This is a forum where people voluntarily provide help. Waiting 11 minutes before asking "any ideas" is presumptuous to say the least.

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.