We help IT Professionals succeed at work.

cant print mysql table info(broken)

Johnny
Johnny used Ask the Experts™
on
im trying to print out info from a mysql table both field names and record data.

for some reason it only prints the first 0 records data but stops there any ideas why and how to fix this please?

if i search for a field name it only returns the first one
ie
$search_for="first_name, account_owner";
first name is returned nothing else

$search_for="account_owner,first_name,phone";
account owner is returned nothing else

$search_for="*";
ID is first field only returns ID

it does print out the field names all 22 of them just no data after first record data


please see attached code

thank you in advance for any help or code you may provide

$id = $_REQUEST['id'];
// any table name and where is will work
$result = mysql_query("SELECT * FROM contacts where ID='".$id."'" );
echo "Displaying Record Number: ".$id."<br>\n";
$numfields=mysql_num_fields($result); // count the number of results
//echo mysql_num_fields($result);
for($i=0; $i<$numfields; $i++) // print out the results and column names 1 at a time
{
$fieldName = mysql_field_name($result,$i);
$myrow=mysql_fetch_array($result,MYSQL_NUM); // set the results into array row
//$row = mysql_fetch_assoc($result);
echo"$i: ";
echo"{$fieldName}";
echo" - ";
echo "{$myrow[$i]}";
//echo "{$row[$fieldName]}";
echo"<br>\n";
} // of of for loop

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Greg AlexanderLead Developer

Commented:
Do you have to do it that way?

Author

Commented:
as long as it prints something like this

Displaying Record Number: 7
0: ID - 7
1: FIRST NAME -
2: LAST NAME -
3: MAILING STREET -
4: MAILING CITY -
5: MAILING STATE -
6: MAILING ZIPCODE -

i still dont get why its not working tho
see revised code

$id = $_REQUEST['id'];

//$search_for="first_name, account_owner";
//$search_for="account_owner,first_name,phone";
$search_for="*";

$result = mysql_query("SELECT ".$search_for." FROM contacts where ID='".$id."'" );
$numfields=mysql_num_fields($result); // count the number of results
echo "Displaying Record Number: ".$id."<br>\n";
//echo mysql_num_fields($result);
for($i=0;$i<$numfields;$i++) // print out the results and column names 1 at a time
{
$fieldName = mysql_field_name($result,$i);
$fieldName = str_replace("_"," ",$fieldName);
$fieldName = strtoupper($fieldName);
$myrow=mysql_fetch_array($result,MYSQL_NUM); // set the results into array row
//$row = mysql_fetch_assoc($result);
echo"$i: ";
echo"{$fieldName}";
echo" - ";
echo "{$myrow[$i]}";
//echo "{$row[$fieldName]}";
echo"<br>\n";
} // end of for loop

Open in new window

Author

Commented:
the attached code works but i dont want it in a table i want it on top of each other

$id = $_REQUEST['id'];

//$search_for="first_name, account_owner";
//$search_for="account_owner,first_name,phone";
$search_for="*";

$result = mysql_query("SELECT ".$search_for." FROM contacts where ID='".$id."'" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
//print "There are $num_rows records.<P>";
//print "<table width=1000 height=400 border=1>\n";
while ($get_info = mysql_fetch_row($result)){
//print "<tr>\n";
foreach ($get_info as $field)
print "$field<br>\n";
//print "\t<td><font face=arial size=2/>$field</font></td>\n";
//print "</tr>\n";
}
//print "</table>\n";

Open in new window

Greg AlexanderLead Developer

Commented:
You want it to look like this but not in a table?

0: ID - 7
1: FIRST NAME -
2: LAST NAME -
3: MAILING STREET -
4: MAILING CITY -
5: MAILING STATE -
6: MAILING ZIPCODE -

Author

Commented:
yes please
Lead Developer
Commented:
See what that does (I have not tested it)
<?
$id = $_REQUEST['id'];

//$search_for="first_name, account_owner";
//$search_for="account_owner,first_name,phone";
$search_for="*";

$result = mysql_query("SELECT ".$search_for." FROM contacts where ID='".$id."'" ) or die("SELECT Error: ".mysql_error());
$number_of_fields = mysql_num_fields($result);
$i = 1;
while ($get_info = mysql_fetch_row($result)){
	foreach ($get_info as $field){
		print "$field\n";
		if($i % $number_of_fields == 0){
			echo "<br>";
		}	
	$i++;
	}
}
?>

Open in new window

Author

Commented:
tweaked it to fit my needs but this is what i wanted thank yoiu

$id = $_REQUEST['id'];
echo "Displaying Record Number: ".$id."<br>\n";

//$search_for="first_name, account_owner";
//$search_for="account_owner,first_name,phone";
$search_for="*";

$result = mysql_query("SELECT ".$search_for." FROM contacts where ID='".$id."'" ) or die("SELECT Error: ".mysql_error());
$number_of_fields = mysql_num_fields($result);
$i = 0;
while ($get_info = mysql_fetch_row($result)){
        foreach ($get_info as $field){
		$fieldName = mysql_field_name($result,$i);
		$fieldName = str_replace("_"," ",$fieldName);
		$fieldName = strtoupper($fieldName);
		echo"{$fieldName}";
		echo" - ";
		print "$field <br> \n";
		$i++;
	}
}

Open in new window