Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 9th, 2004, 01:29 PM
Registered User
 
Join Date: Nov 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default mysql_fetch_array and if .. else

I'm attempting to use an if statment during a while mysql_fetch_array, however, the if statement only uses the first Record in the array. How do I make the decision process for each record?

// Printing results in HTML
while ($rows = mysql_fetch_array($result)) {
    extract ($rows);
    echo $JeffandMary;
    if ($jeffandmary = "Yes")
          echo "<p>Jeff and Mary</p>";
      else
           echo "<p>No</p>" ;
    }
 
Old November 9th, 2004, 01:39 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Look more closely at that, you're using the assignment operator, not a comparison operator (e.g. '==').

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
 
Old November 12th, 2004, 05:52 AM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

also note that $rows is now an array (due to mysql_fetch_array), so to get specific records, use $rows[$i], where $i is an identifier to a specific record.

I think theres another function to allow you to reference the array as an associate array, for instance $row['id'], would return the record named 'id'. quite handy.

check the php website, http://www.php.net/array and http://www.php.net/mysql

Many shoes,

James/SiliconFuRy
 
Old November 21st, 2005, 09:08 AM
Registered User
 
Join Date: Nov 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i would like to know whether the code below really working?

while ($rows = mysql_fetch_array($result)) {
    extract ($rows);
    echo $JeffandMary;
    if ($jeffandmary = "Yes")
          echo "<p>Jeff and Mary</p>";
      else
           echo "<p>No</p>" ;
    }

I ask is because I have a similar code that want to compare the value in a row to a variables.Example I have var $emNo='000080';

then have a code :
$query = "select * from ACCOUNT order by ACC_ID";
$r = mysql_query($query);
while ($row = mysql_fetch_array($r, MYSQL_NUM)) {
if ($row[1] == $emNo){
return true;
}
else return false;


}//end while

My first record in Account table is EMP_NUM=00001. When I put the value emNo as ''00001', it can compare and return true. But when I put value emNo as '00080', it return false, where as the account table has the EMP_NUM=00080.
Looks like it always compare only the first row. What is wrong with my code?







Similar Threads
Thread Thread Starter Forum Replies Last Post
chapter 15 cart.php Warning: mysql_fetch_array(): pink BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 July 19th, 2005 07:49 PM
Problem with mysql_fetch_array insomne PHP Databases 0 September 27th, 2004 02:03 PM
mysql_fetch_array with a variable string aeres PHP How-To 7 August 18th, 2003 01:01 PM
mysql_fetch_array problem... aeres PHP Databases 2 July 4th, 2003 12:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.