Jump to content

[SOLVED] Pulling single row from database and inserting specific field into a variable


cbr_600rr

Recommended Posts

Ok, I feel pretty stupid right now, I cannot get this code to work.  I have a database with say 3 fields called id, field2, field3

 

values have already been stored in the fields

 

The specific table only contains 1 row at anytime.

 

How do I fetch the single row and pull out a value from say field2 and insert it into a variable?

 

After making a successful connection to the DB, I have something like this,...

 

$result = mysql_query('SELECT * FROM table1 WHERE id='1');

 

if($result){

echo "Successful";

}else {

echo "ERROR";}

 

$row = mysql_fetch_row($result);

echo $row[0]; // should be equal to '1' (the first id number)

echo $row[1]; // should be equal to value in field02

 

What am I doing wrong here?

would it be easier to do something like this...

 

$result = mysql_query('SELECT field2 FROM table1 WHERE id='1');

 

 

I appreciate the help guys

 

 

your SQL syntax is wrong.  you have one too many single-quotes.

 

change your query to:

 

$result = mysql_query('SELECT * FROM table1 WHERE id=1');

 

the rest should work as you have it.

your SQL syntax is wrong.  you have one too many single-quotes.

 

change your query to:

 

$result = mysql_query('SELECT * FROM table1 WHERE id=1');

 

the rest should work as you have it.

 

 

Thanks for the help, it is working now!

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.