Solved

What's the PHP equivalent to ASP's "EOF"?

Posted on 2008-10-22
5
737 Views
Last Modified: 2013-12-13
I have my code attached...

What's happening is that in this instance there is no data to retrieve. With ASP I would write something like "if RSContest.EOF then..." In other words, I would have a message or a redirect in place to handle those instances where there were no values to retrieve.

Right now, with the way I have things set up, I get an error message: "Warning: extract() [function.extract]: First argument should be an array in C:\wamp\www\Showdown\ContestLocationsDisplay.php on line 52"

Line 52 is "extract ($rowcontest). What am I doing wrong and what can I put in place in the way of an if statement so when these scenarios surface, I can direct my user's experience accordingly?

Thanks!

$querycontest = "select * from dbo_state_contests where state_id='$state'";
$resultcontest = mysqli_query($cxn, $querycontest)
or die ("Couldn't execute query.");
$rowcontest = mysqli_fetch_assoc($resultcontest);
extract($rowcontest);
$contestdate = $show_date;
$contestvenue = $venue;
$contestcity = $city;
$contesturl = $url

Open in new window

0
Comment
Question by:brucegust
  • 3
5 Comments
 
LVL 4

Expert Comment

by:ScannyR
ID: 22778347
If I understand correctly the problem arises when there are no records to pull from the database? If that is the case, you can simply verify that the number of rows returned by the query is greater than zero before proceeding. See the code snippet below.

Rich
$querycontest = "select * from dbo_state_contests where state_id='$state'";
$resultcontest = mysqli_query($cxn, $querycontest)
or die ("Couldn't execute query.");
if(mysql_num_rows($resultcontest)>0)
{
//proceed
}
else
{
//take action required with no records
}

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22778557
when there are no records,  mysqli_fetch_assoc will return false:
Try:

$querycontest = "select * from dbo_state_contests where state_id='$state'";
$resultcontest = mysqli_query($cxn, $querycontest)
or die ("Couldn't execute query.");
if( $rowcontest = mysqli_fetch_assoc($resultcontest) )
{
extract($rowcontest);
$contestdate = $show_date;
$contestvenue = $venue;
$contestcity = $city;
$contesturl = $url;
}
else{
 echo "No records";
}
0
 
LVL 82

Expert Comment

by:hielo
ID: 22778571
NOTE: if there is more than one record, then use a while:
$querycontest = "select * from dbo_state_contests where state_id='$state'";
$resultcontest = mysqli_query($cxn, $querycontest)
or die ("Couldn't execute query.");
while( $rowcontest = mysqli_fetch_assoc($resultcontest) )
{
extract($rowcontest);
$contestdate = $show_date;
$contestvenue = $venue;
$contestcity = $city;
$contesturl = $url;
echo $contesturl;
}

Open in new window

0
 

Author Comment

by:brucegust
ID: 22780207
In this instance, there will never be more than one record. Right now, my code looks like what I have attached and I'm getting this error:

Warning: extract() [function.extract]: First argument should be an array in C:\wamp\www\Showdown\ContestLocationsDisplay.php on line 144

Do I only use "extract" when I'm expecting more than one row?
$querycontest = "select * from dbo_state_contests where state_id='$state'";
$resultcontest = mysqli_query($cxn, $querycontest)
or die ("Couldn't execute query.");
 
if( $rowcontest = mysqli_fetch_assoc($resultcontest) )
 
{
 
$rowcontest = mysqli_fetch_assoc($resultcontest);
extract($rowcontest);
$contestdate = $show_date;
$contestvenue = $venue;
$contestcity = $city;
$contesturl = $url;

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22780453
>>if( $rowcontest = mysqli_fetch_assoc($resultcontest) )
>>{
>> $rowcontest = mysqli_fetch_assoc($resultcontest);

if you expect only one record, then dod NOT call mysqli_fetch_assoc more than once. You already called it in the if clause, so $rowcontest is already an array that contains the data you seek. After you call mysqli_fetch_assoc once, the pointer is advanced to the next record, but since there is no other record, the second call will put FALSE in $rowcontest, so your extract() will fail:

$querycontest = "select * from dbo_state_contests where state_id='$state'";
$resultcontest = mysqli_query($cxn, $querycontest)
or die ("Couldn't execute query.");
 
if( $rowcontest = mysqli_fetch_assoc($resultcontest) )
 
{
//get rid of the following line 
//$rowcontest = mysqli_fetch_assoc($resultcontest);
 
extract($rowcontest);
$contestdate = $show_date;
$contestvenue = $venue;
$contestcity = $city;
$contesturl = $url;

Open in new window

0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Pagination Function Not working ? 7 32
.htaccess 5 39
mysql date time 14 31
error while installing php56 in redhat enterprise linux 20 25
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

821 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