Jump to content

Simple code for a page to list 1 column in a table


eqnastun

Recommended Posts

Hello,

 

I am trying to create a small piece of code that will load as a php page that can display 2 columns from a table. The columns are in the middle of the table but I am looking to output name and e-mail into a list.

Here is what I have so far but I am not getting any results.

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php 

echo "<b><center>Database Output</center></b><br><br>";

$username="root";
$password="xxxx";
$database="test2";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM fr_users";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
$i=0;
while ($i < $num) {

$field1-name=mysql_result($result,$i,"login");
$field2-name=mysql_result($result,$i,"alias");

echo "<b>$field1-name - $field2-name2</b><hr><br>";

$i++;
}
?> 
</body>
</html> 


Appreciate any help. Thank You. I am sure this is very easy to do.

remove mysql_close();

 

are you getting any errors?

 

 

EDIT: change it all to this...

 

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php 

echo "<b><center>Database Output</center></b><br><br>";

$username="root";
$password="xxxx";
$database="test2";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM fr_users";
$result=mysql_query($query);
$result = mysql_fetch_array($result);

echo "<b>".$result['login']." - ".$result['alias']."</b><hr><br>";
?>
</body>
</html> 

I edit some of your code , This one below list two columns in a table with format

 

Data1:data2

 

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php 

echo "<b><center>Database Output</center></b><br><br>";

$username="root";
$password="xxxx";
$database="test2";

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM fr_users"; 
$result=mysql_query($query);

//$num=mysql_numrows($result);

mysql_close();
//$i=0;
while ($row = mysql_fetch_array($result)) {

//$field1-name=mysql_result($result,$i,"login");
//$field2-name=mysql_result($result,$i,"alias");

echo $row['login'] . ":" . $row['alias']."<br />"; // desired columns names

}
?>
</body>
</html>


Appreciate any help. Thank You. I am sure this is very easy to do.

 

yeah, but you don't need a while() loop. $row = mysql_fetch_array($result); will do the exact same thing, and you need to get rid of mysql_close(); all mysql after that is being denied.

 

i test the code and find this error :

 

Notice: Use of undefined constant localhost - , so you need to add single quote  'localhost'

 

and without while loop it just show the first record with desired columns in the selected TABLE and neglect the rest !

 

-php new bie

and without while loop it just show the first record with desired columns in the selected TABLE and neglect the rest !

 

no it won't. It would only do the same result if the mysql query had LIMIT 1 in it. But it doesn't you don't need a loop. setting $row to fetch array will place all results into the $row array without the need for a loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.