Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-29
4
Medium Priority
?
382 Views
Last Modified: 2012-03-30
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
0
Comment
Question by:dtleahy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 37784560
You can use mysql_fetch_array() http://us3.php.net/manual/en/function.mysql-fetch-array.php to get the results by field names.

MySQL connections are closed automatically at the end of the script.  You do not normally have to explicitly close them.

You can use the PHP 'header' http://us3.php.net/manual/en/function.header.php function to redirect to another page.  Note that you can't output any text or HTML before doing this, not even an extra space before the <?php tag, because that causes the headers to be already sent before it gets to your code.  You can't send any headers after the content has been sent.
0
 

Author Closing Comment

by:dtleahy
ID: 37785093
Dave,

Quick, accurate, awesome. Thank you very much!

Dennis
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37785124
You're welcome.!
0
 

Author Comment

by:dtleahy
ID: 37786808
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

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27655574.html

-Dennis
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question