Solved

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

Posted on 2008-10-22
5
736 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 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

23 Experts available now in Live!

Get 1:1 Help Now