using variables with odbc database

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
FE
Forum Commoner
Posts: 35
Joined: Fri Jul 14, 2006 5:21 am

using variables with odbc database

Post by FE »

Hello, I am having great trouble adapting to code with an ODBC database!

Can someone help me by showing me how to make a query from an ODBC database and then assigning the results into variables.

E.G
$sql=Select ID, NAME, LAST_NAME FROM DATABASE WHERE id=$id;

Then put the values (ID, NAME and LAST_NAME) obtained from the database into variables $ID, $NAME, $LAST_NAME


Another question:

I’ve got 3 variables ($ID1, $ID2, $ID3) containing (152,168,335, respectively)
What would the php command be to make a variable = 152-168-335 and not 655 (=152+168+355)

Cheers guys!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: using variables with odbc database

Post by RobertGonzalez »

FE wrote:Can someone help me by showing me how to make a query from an ODBC database and then assigning the results into variables.

E.G
$sql=Select ID, NAME, LAST_NAME FROM DATABASE WHERE id=$id;

Then put the values (ID, NAME and LAST_NAME) obtained from the database into variables $ID, $NAME, $LAST_NAME
I snagged this code from your drop-down menu post and modified it slightly...

Code: Select all

<?php
if (!$connectionstring = odbc_connect("dbname","username","********"))
{
    die('Could not connect to my database...');
}

$id = 1; // This would actually be set programmatically I presume?
$query = "SELECT ID, NAME, LAST_NAME FROM DATABASE WHERE ID=$id" ;
if(!$result = odbc_do($connectionstring, $query))
{
    die('There was a database error in the query: ' . odbc_errormsg());
}

while ($row = odbc_fetch_array($result))
{
    $ID = $row['ID'];
    $NAME = $row['NAME'];
    $LAST_NAME = $row['LAST_NAME'];
} 
?>
FE wrote:Another question:

I’ve got 3 variables ($ID1, $ID2, $ID3) containing (152,168,335, respectively)
What would the php command be to make a variable = 152-168-335 and not 655 (=152+168+355)

Code: Select all

<?php
$var = $ID1 . '-' . $ID2 . '-' . $ID3;
?>
FE
Forum Commoner
Posts: 35
Joined: Fri Jul 14, 2006 5:21 am

Post by FE »

ah cheers! :D

loving this forum, i'm gonna definitly give back to the community
Post Reply