mysql_fetch_array question

Hi All, I’m new to PHP and stumped by the results (or lack thereof) that I’m getting from a mysql_fetch_array when attempting to pull data from a MySQL table into an html table. Here is the part that’s puzzling me:

while ($p = mysql_fetch_array($result)) {

<td><? if ($p[‘service’] == ‘manualqotd_no_rev’) {
echo ‘Manual Conversions’;
}
else if (($p[‘points’] == 0) AND ($p[‘service’] != ‘manualqotd_no_rev’)) {
echo $p[‘network’];
}
else
echo $p[‘service’];?></td>

I’m seeing expected results for the final 2 statements, but the entries where $p[‘service’] == ‘manualqotd_no_rev’ are not displayed in the table, they are getting skipped! Is there a certain syntax I should use in order to echo text (instead of listing db results) when a particular db field (‘service’ in this case) contains a certain value ( ‘manualqotd_no_rev’ in this case)?

I’m sure this is a blatant mistake on my part - any help much appreciated. Thanks!

try mysql_fetch_assoc - mysql_fetch_array returns an indexed array (indices are 0, 1, etc.). Assoc returns an associative array.

Thanks Steve, but no luck. I’m still getting the same results.

I am connecting to the db and pulling data from two tables that have overlapping data, like this:

$result = mysql_query(“SELECT nloppoints.points, nloppoints.service, nloppoints.posttime, redirects.network, redirects.name, redirects.value FROM nloppoints, redirects WHERE nloppoints.qotdid = redirects.qotdid AND nloppoints.posttime BETWEEN ‘$fromdate’ and ‘$todate’ ORDER BY nloppoints.posttime”);

The results are sent to a 4-column html table like this:

while ($p = mysql_fetch_assoc($result))

<td><? if ($p[‘service’] == ‘manualqotd_no_rev’) {
echo ‘Manual Conversions’;
}

else if (($p[‘points’] == 0) AND ($p[‘service’] != ‘manualqotd_no_rev’)) {
echo $p[‘network’];
}
else
echo $p[‘service’];?></td>
<td><? if ($p[‘service’] == ‘manualqotd_no_rev’)
echo ‘No Revenue Earned’;
else echo stripslashes($p[‘name’]);?></td>
<td><? if ($p[‘service’] == ‘otx’) {
echo ($p[‘points’] / 3333.33);
}
else if ($p[‘service’] == ‘manualqotd_no_rev’) {
echo ‘0’;
}
else if ($p[‘points’] == 0) {
echo ($p[‘value’] / 100);
}
else
echo ($p[‘points’] / 3000);?></td>
<td><? echo $p[‘posttime’];?></td>

Any other ideas why this is not including the entries where service = ‘manualqotd_no_rev’ ?

Thanks

At the end of your line containing the while() statement you do not have an opening brace {.

Also you then don’t have a closing php tag before the <td>.

You’re either showing selected snips of code (which isn’t useful because the error could be somewhere else) or you simply haven’t coded it properly.

If you’re going to show selected snips of code you at least ensure that you have all the tags and braces in there to stop any confusion otherwise you will have people poking at various shadows for days. You’ll then get frustrated, members start trying to outsmart each other and then no-one gets anywhere.

I may be slight incorrect here, but you are trying to search within an array without searching it. So, try to change:


if ($p['service'] == 'manualqotd_no_rev') {
echo 'Manual Conversions';
}

to

if(in_array("manualqotd_no_rev",$p)){
echo 'Manual Conversions';
}

But that only looks to see if ANY key holds the value. Not the one targetted key.

Yeah that suggestion didn’t do the trick, Ryan. Thanks for the tip though.

Tango, I may have left out some code as I was just pasting snippets. Here it is in it’s entirety:

<?
$result = mysql_query(“SELECT nloppoints.points, nloppoints.service, nloppoints.posttime, redirects.network, redirects.name, redirects.value FROM nloppoints, redirects WHERE nloppoints.qotdid = redirects.qotdid AND nloppoints.posttime BETWEEN ‘$fromdate’ and ‘$todate’ ORDER BY nloppoints.posttime”);

$num_results = mysql_num_rows($result);

echo ‘Total Number of Conversions:’ . " " . $num_results;
?>
<br>
<?

$colorclass = "";

			while ($p = mysql_fetch_assoc($result))
			{
				if($colorclass == "odd")
				{
					$colorclass = "";	
				}
				else
				{
					$colorclass = "odd";	
				}

        ?&gt;
		&lt;tr class="&lt;? echo $colorclass;?&gt;"&gt;
		  &lt;td&gt;&lt;? if (in_array('manualqotd_no_rev', $p)) {
			echo 'Manual Conversions';
			}
			else if (($p['points'] == 0) AND ($p['service'] != 'manualqotd_no_rev')) {
		    echo $p['network'];
			}
			else
			echo $p['service'];?&gt;&lt;/td&gt;
			&lt;td&gt;&lt;? if ($p['service'] == 'manualqotd_no_rev')
			echo 'No Revenue Earned';
			else echo stripslashes($p['name']);?&gt;&lt;/td&gt;
            &lt;td&gt;&lt;? if ($p['service'] == 'otx') {
				echo ($p['points'] / 3333.33);
				}
				else if ($p['service'] == 'manualqotd_no_rev')  {
			echo '0';
			}
			 else if ($p['points'] == 0)  {
			echo ($p['value'] / 100);
			}
			else 
			echo ($p['points'] / 3000);?&gt;&lt;/td&gt;
            &lt;td&gt;&lt;? echo $p['posttime'];?&gt;&lt;/td&gt;
		&lt;/tr&gt;
        &lt;?
			}
		?&gt;

I guess a mock up table of what he wants would help.

There are 2 tables in the database:

nloppoints contains the following fields:

Field Type Collation Attributes Null Default Extra Action
bpid int(11) No None AUTO_INCREMENT
uid int(11) No None
points int(11) No None
qotdid int(11) No None
posttime timestamp No CURRENT_TIMESTAMP

‘redirects’ contains the following fields:

Field Type Collation Attributes Null Default Extra Action
oid int(11) No None AUTO_INCREMENT
network varchar(255) utf8_unicode_ci No None
name varchar(255) utf8_unicode_ci No None
value int(11) No None
redirect varchar(255) utf8_unicode_ci No None
qotdid int(11) No None
created_date timestamp on update CURRENT_TIMESTAMP No CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Here is the query again:

$result = mysql_query(“SELECT nloppoints.points, nloppoints.service, nloppoints.posttime, redirects.network, redirects.name, redirects.value FROM nloppoints, redirects WHERE nloppoints.qotdid = redirects.qotdid AND nloppoints.posttime BETWEEN ‘$fromdate’ and ‘$todate’ ORDER BY nloppoints.posttime”);

In the query i’ve indicated that nloppoints.qotdid = redirects.qotdid. What the output should produce is a 4-column table listing:

  1. the redirects.network name (which in some cases can be replaced by the nloppoints.service name)
  2. The offer (redirects.name) name
  3. The offer value, which depending on the value of nloppoints.service could be pulled from either nloppoints.points or redirects.value
  4. The timestamp (nloppoints.posttime)

As I said before the results are all displayed as I expect, except in the instances when nloppoints.service=‘manualqotd_no_rev’. In these cases I want to override the associated db values for the corresponding qotdid with ‘Manual Conversions’ for (1.), ‘No Revenue Earned’ for (2.), ‘0’ for (3.), and keep the timestamp as is.

This must be possible, I just obviously don’t have a firm grasp on how to do it…