Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
I've got an interview this morning and I want to sound intelligent... 4 69
Wordpress French and English Site 6 77
Wordpress Pagination 1 28
issue with DB import 1 17
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

861 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