mysql functions

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

mysql functions

Post by nincha »

is there a function that gives the row number where a certain item is located at??
spammich
Forum Newbie
Posts: 11
Joined: Sun Mar 23, 2003 12:40 am

Post by spammich »

Row number? The whole point ot a relational database is that you don't need to worry about rows and stuff, only primary keys.

Code: Select all

$result = mysql_query("SELECT id FROM mytable WHERE mydata=$blahblahblah");
$obj = mysql_fetch_object( $result );
mysql_free_result( $result );

$rownumber = $obj->id;
Or better yet, use PEAR::DB_DatabaseObjects:
http://pear.php.net/manual/en/packages. ... object.php

Hope this helps.
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

can u give the syntax to getting an item using primary keys? i was tinking like

Code: Select all

$result = mysql_query("SELECT $field * FROM $table WHERE id=1");
but thats the wrong syntax for it, can some one tell me the right way of using it.Thnx
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

More like
$sqlstring = "SELECT field1, field2, field3 FROM sometable WHERE id=1";
$sql_result = mysql_query($sqlstring,$conn);


Phil J.
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

i tried that and what i got was Resource id #2 for everything, can some one tell me what this means
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post by haagen »

The $result is just a "pointer" the real data. To fetch it you have to do a

Code: Select all

$row = mysql_fetch_row($result);
Then the $row is an array. You can reach the data by

Code: Select all

echo $rowї"fieldname"];

or

echo $rowї0];  // field no. 0
Hope this helps. And please, read a database tutorial, before asking.
Post Reply