Link to home
Start Free TrialLog in
Avatar of dtleahy
dtleahyFlag for United States of America

asked on

php mysql syntax and function questions (mysql_fetch_row: one record returned)

Be gentle with me. I have a total of about 3 days experience trying to very quickly learn just enough php to fulfill a task.

The sample code I see almost always returns a recordset, and the recordset is then dumped into an array, and a "while" loop is used to traverse the recordset.

But, if I deliberately return only one record, I do not need to traverse a recordset - I just want to read returned field data for that one record. So, I found a way to do this, using

mysql_fetch_row()

(As you see in the code below.)

The questions I have at this point:
1.) to test if the record was returned, should I use "empty":
if (empty($results))
or is there a better way?

2.) I see that I cannot address the $onerow array by field names, so is the only (or the best) way to use
$onerow[1]
or is there another way to reference the array?

3.) When do I close the connection? (right after the line starting with $results = )?

4.) Once my processing.php file has completed, possibly with a database error, but hopefully with success, how do I gracefully redirect the user to the page I want them to go to? (a custom error page, or a custom success page)


include_once "myconnection.php"

$querystringactivationcode = $_SERVER['QUERY_STRING'];
$query7 = "SELECT userid, activationcode FROM mytable WHERE ((xyzvalid=0) AND (activationcode = '" . $querystringactivationcode . "')) LIMIT 1"; 
$results = mysql_query($query7) or die(mysql_error());
$onerow = mysql_fetch_row($results);

## $onerow[0]; // the 0 element in the array is userid
## $onerow[1]; // the 0 element in the array is the activation code

## $onerow['userid']; // I see that the array cannot be accessed this way - elements of the array are referenced by integers (zero based array)


## so, I can do some processing:

if ($querystringactivationcode == $onerow[1])
	{
	}
else
	{
	}

Open in new window



Thank you from a php beginner

Dennis
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dtleahy

ASKER

Dave,

Quick, accurate, awesome. Thank you very much!

Dennis
You're welcome.!
Avatar of dtleahy

ASKER

the header redirect is a bit tricky, because I want to use querystring variables. This is complex enough that I created a new question for it: php redirect header conditional querystring

https://www.experts-exchange.com/questions/27655574/php-redirect-header-conditional-querystring.html

-Dennis