Solved

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

Posted on 2008-10-22
5
730 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now