?
Solved

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

Posted on 2008-10-22
5
Medium Priority
?
752 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
[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
  • 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

770 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