Getting info

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Love_Daddy
Forum Commoner
Posts: 61
Joined: Wed Jul 10, 2002 6:55 am
Location: South Africa
Contact:

Getting info

Post by Love_Daddy »

Hi All,

I have a problem, with placing the results of my select with php.
Firstly:

1) Select name, surname from <table>
2) I need to display the name and surname to a form.

So I Know that I have to assign a name and surname variables so that I can move my
selected info to the form.
So how do I go about it? :x
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$host = ...
$user = ...
$password = ...
$db = ...
//connect to mysql-server
$conn = mysql_connect($host, $user, $password) or die(mysql_error());

// select the database
$conn = mysql_select_db($db, $conn) or die(mysql_error());

// build your query
$query = "SELECT name, surname FROM tblName";

// perform the query
$query = mysql_query($query) or die($query. ' -> '. mysql_error());

// fetch each result and display
while( $row = mysql_fetch_row($query, $conn) )
  print($row&#1111;0] .', '. $row&#1111;1].<br/>');

// free the mysql-resources (done automatically when script ends)
mysql_free_result($result);
mysql_close($conn);
php-mysql-manual:
http://www.php.net/manual/en/ref.mysql.php
User avatar
Love_Daddy
Forum Commoner
Posts: 61
Joined: Wed Jul 10, 2002 6:55 am
Location: South Africa
Contact:

Re:Getting Info

Post by Love_Daddy »

Well I also need to mention that I'm using PostgreSQL.
So After I do my select,
I need to do the following:

$name = $row['name'];
$surname=$row['surname'];

And then move the info to the form:

Name: <input type="text" value="<? echo $name; ?>">
Something like that..

I remember seeing something of the this sort but I don't remember where!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it's almost the same
array pg_fetch_row ( resource result, int row)
...
Note: From 4.1.0, row became optional. Calling pg_fetch_row() will increment internal row counter by 1.

Code: Select all

...
$result = pg_query($conn, $query) or die(pg_last_error());
while ($row = pg_fetch_row($result))
  print($row&#1111;0] .', '. $row&#1111;1].<br/>');
...


if you want to work with the names of the fields instead of the numbers as indices to the result-array, you have to use pg_fetch_array instead of pg_fetch_row


manual: http://www.php.net/manual/en/ref.pgsql.php
User avatar
Love_Daddy
Forum Commoner
Posts: 61
Joined: Wed Jul 10, 2002 6:55 am
Location: South Africa
Contact:

Post by Love_Daddy »

$sql="Select id,client from client";
$results=pg_exec($conn,$sql);
if ($results)
{
$numrows = pg_numrows($results);
echo "<b><u>Client Information</b></u><br>\n";
for ($row = 0; $row < $numrows; $row++)
{
$id = pg_result($results,$row,'ID'). " ";
$client = pg_result($results,$row,'Client')." ";
echo "Client ID : $id";
echo "Client Name : $client<br>\n";
}}
else
{echo ('No connection to the table!');exit;}
?>
<form>
<p>
Client ID:<input type="text" size="2" value="<? echo $id; ?>">&nbsp;&nbsp
Client Name:<input type="text" size="10" value="<? echo $client;?>">
</form>
</html>

What this code does is, it returns all the values/records from the database.
So what I need to do is, get specific results.
Like if I need to select one name or client ID only to display.
Say I have a login script for client ID:1 I want to display information on this specific client
nd not everything..

:roll:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you may limit the selected records with a WHERE-clause.
i.e.
SELECT * from client WHERE id = 2;
or "PHPable"
$query = "SELECT * from client WHERE id=".$_SESSION['clientId'];

manual:
PostgreSQL Interactive Documentation # SELECT
PostgreSQL Interactive Documentation # SELECT[WHERE-clause]
Post Reply