What's bad w/this code ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

What's bad w/this code ?

Post by rax369 »

This code is not showing the firs element of my query:

Code: Select all

<?php

		  <select name="processors" id="processors">
            <?
			while ($row_processors = mysql_fetch_array($rs_processors))
			{?>
            <option> 
            <?
				echo $row_processorsї'brand']; echo " - ";
				echo $row_processorsї'name']; echo " - $"; echo $row_processorsї'price'];
				
				?>
            </option>
            <? } ?>
            <option selected><?php echo "Choose / Remove Product";?></option>
          </select>
?>
The MySQL query is this one:

Code: Select all

SELECT brand, name, price
FROM products
WHERE category LIKE 'processors'
ORDER BY brand, name
And should show this elements, in this specific order: (forget the colum Record that appears there, I just took a snapshot from dreamweaver mx showing the result of my query to show u this :lol: )

Image

... so following how the code is ordering the info, the 1st. element in the drop down menu should be:
AMD - ATHLON 1.3 GHZ - $85.40

But what I'm getting is this ... :x

Image

As you can see, the 1st. element (AMD - ATHLON 1.3 GHZ - $85.40) is not appearing, why :?: :cry:

thx for ur help
Last edited by rax369 on Wed Nov 13, 2002 1:04 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what's inbetween?

Code: Select all

<!-- somewhere must be a mysql_query -->
... what's here? ...
while ($row_processors = mysql_fetch_array($rs_processors))
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

volka wrote:what's inbetween?

Code: Select all

<!-- somewhere must be a mysql_query -->
... what's here? ...
while ($row_processors = mysql_fetch_array($rs_processors))
ok, volka, here is the rest of the code:

Code: Select all

<?php

$query_rs_processors = "SELECT brand, name, price FROM products WHERE categoria LIKE 'processors' ORDER BY brand, name";
$rs_processors = mysql_query($query_rs_processors, $ST_Prods_Connection) or die(mysql_error());
?>
What do u think I'm not doing correct :?: :cry:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

then I'm clueless or blind - probably both ;)
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

I dont know what to do, I have ever done this in that way, and know, PHP comes w/that problem :cry:

HELP PLEASE :!:
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

your code

Post by phpScott »

I took your code and modified it to do almost what I do in simillar situations.
Hope fully this will work for you. I am hoping the syntax comes out alright as I don't have the same db set up to test it. This is just plain substitution so it should work, maybe, hopefully, finger crossed :)

Code: Select all

<?php
<select name="processors" id="processors">
    <option selected>Choose / Remove Product</option>
    <?php
       while ($row_processors = mysql_fetch_array($rs_processors))
       {
          print"<option>".$row_processorsї'brand']." - ".$row_processorsї'name']." - $". $row_processorsї'price']."</option>";
       }
    ?>
    </select>
?>
phpScott
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

when I reach the state "damn, it must work. I know it works. somebody is playing tricks on me" I try to isolate the problem, creating a new program that does nothing else but this code fragment. In your case maybe something like

Code: Select all

<html><body><?php
include 'database_connection.php';

$query_rs_processors = "SELECT brand, name, price FROM products WHERE categoria LIKE 'processors' ORDER BY brand, name";
$rs_processors = mysql_query($query_rs_processors, $ST_Prods_Connection) or die(mysql_error()); 
echo '#records :', mysql_num_rows($ST_Prods_Connection);

echo '<table border="1"><tr>';

for ($i=0; $i!=mysql_num_fields($rs_processors); $i++)
	echo '<th>', mysql_field_name($rs_processors, $i);

while ($row_processors = mysql_fetch_row($rs_processors))
{
	
	echo '<tr>';
	foreach($row_processors as $value)
		echo '<td>', $value, '</td>';	
	echo '</tr>';
}

echo '</table>';

?></body></html>
(completely untested, not even by compiler ;) )
If this still does not process as expected I go on simplifying (skipping the ORDER BY then maybe the WHERE clause and so on).
But always keep in mind that you have to reintegrate your changes, so remeber what you did and keep the fragment small ;)
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

I did exactly what u told me phpScott, I even pasted ur code into my page, but what I got was the same result... the first element of the query is not appearing :evil:

this is so stupid, that is not working, I dont know what's bad.

I did as well what volka recommeded me, I and tried to exclude this especific problem, reencoding it at a new page, but cannt get it still.

aaargh :!: :!: :cry: , I feel sometimes that PHP functions doesnt works the way they are suppoustly to work. The function I'm dealing with is pretty simple... mysql_fetch_array... but it doesnt wanna work at all :evil:

If for some reason comes to ur mind guys, a possible solution to this stupid problem, please show it here.

I'm probably blind as well volka, I cannt locate the problem.

thx :oops:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

When you used volka's code how many results did it say the query returned?

Mac
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

Forget what I said, I found the origin of my problem... DREAMWEAVER MX :!:

this program sometimes I dont know why, but it generates lines of PHP code that u dont need, lines that u even havent typed, I what I have realized is that once u use the function 'mysql_fetch_array' over one variable storing some query, u cannt use it again right :?:

may be u have u do another query to ur db, store it in some variable, and use again some function like this one (e.g. mysql_fetch_array, mysql_fetch_row, mysql_fetch_assoc, ... etc.)

let's give an example to leave my point clear:

Code: Select all

<?
$query_rs_processors = "SELECT brand, name, price FROM products WHERE categoria LIKE 'processors' ORDER BY brand, name";
$rs_processors = mysql_query($query_rs_processors, $ST_Prods_Connection) or die(mysql_error());
?>
<select name="processors" id="processors">
	<option selected><?php echo "Choose / Remove Product";?></option>
		<?
		while($row_processors = mysql_fetch_array($rs_processors))
		{
			print"<option>".$row_processorsї'brand']." - ".$row_processorsї'name']." - $".$row_processorsї'price']."</option>";
		}
		?>
</select>
So because I already used the function 'mysql_fetch_array' to 'extract' the info from '$rs_processors' I cannt use again 'mysql_fetch_array' over that variable. Even if a copy the content of that variable to other before use this function (e.g $rs_processors2 = $rs_processors) even so I wouldnt able to use this function to extract the info, now from $rs_processors2, I dont know why.

could u explain me guys, this what I have found about these functions :?:

thx again guys for all ur help

cheers !:D
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

try this:

Code: Select all

<?php 
include 'database_connection.php'; 

$query_rs_processors = "SELECT brand, name, price FROM products WHERE categoria LIKE 'processors' ORDER BY brand, name"; 
$rs_processors = mysql_query($query_rs_processors, $ST_Prods_Connection) or die(mysql_error());  

while($data = mysql_fetch_assoc($rs_processors))&#123;
 echo "<pre>";
 print_r($data);
 echo "</pre>";
&#125;
?>
what does that return?
Post Reply