PDA

View Full Version : strange error



Demonicman
10-05-2007, 07:31 PM
<table>
<tr><td width=200><b><u>Your Current Stocks:</u></b></td></tr><?php
$stockmarket="mysql_query(select * from stocks where $userstats3[ID]='Owner')";
$stockup="mysql_fetch_array($stockmarket)";
?>
<tr><td>Name: Law And Order</td><td width=100></td><td>Shares: <?php if($stockup[Name]="Law And Order"){ echo "$stockup[Shares]"; } ?></td></tr>
<tr><td>Name: P.A.F.A.</td><td width=100></td><td>Shares: <?php if($stockup[Name]="P.A.F.A."){ echo "$stockup[Shares]"; } ?></td></tr>
<tr><td>Name: Crime Investigators Inc.</td><td width=100></td><td>Shares: <?php if($stockup[Name]="Crime Investigators Inc."){ echo "$stockup[Shares]"; } ?></td></tr>
<tr><td>Name: Whore House Co.</td><td width=100></td><td>Shares: <?php if($stockup[Name]="Whore House Co."){ echo "$stockup[Shares]"; } ?></td></tr>
<tr><td>Name: Ghetto Mart Inc.</td><td width=100></td><td>Shares: <?php if($stockup[Name]="Ghetto Mart Inc."){ echo "$stockup[Shares]"; } ?></td></tr>
<tr><td>Name: Weapons Co.</td><td width=100></td><td>Shares: <?php if($stockup[Name]="Weapons Co."){ echo "$stockup[Shares]"; } ?></td></tr>
<tr><td>Name: Pimp Inc.</td><td width=100></td><td>Shares: <?php if($stockup[Name]="Pimp Inc."){ echo "$stockup[Shares]"; } ?></td></tr>
<tr><td>Name: Playboy Inc.</td><td width=100></td><td>Shares: <?php if($stockup[Name]="Playboy Inc."){ echo "$stockup[Shares]"; } ?></td></tr>
<tr><td>Name: Homeless Rights Inc.</td><td width=100></td><td>Shares: <?php if($stockup[Name]="Homeless Rights Inc."){ echo "$stockup[Shares]"; } ?></td></tr>
<tr><td>Name: Black Market Co.</td><td width=100></td><td>Shares: <?php if($stockup[Name]="Black Market Co."){ echo "$stockup[Shares]"; } ?></td></tr>
</tr></table>

why is this code printing the following:


Name: Law And Order Shares: L
Name: P.A.F.A. Shares: P
Name: Crime Investigators Inc. Shares: C
Name: Whore House Co. Shares: W
Name: Ghetto Mart Inc. Shares: G
Name: Weapons Co. Shares: W
Name: Pimp Inc. Shares: P
Name: Playboy Inc. Shares: P
Name: Homeless Rights Inc. Shares: H
Name: Black Market Co. Shares: B

Twey
10-05-2007, 07:44 PM
What's strange about it? You're assigning strings there, never actually performing a query. Also, where are the constants ID and Shares defined?

Demonicman
10-05-2007, 07:57 PM
do you mind telling me how to fix it? lol i know theres stuff wrong with it

Twey
10-05-2007, 09:25 PM
Your code is wrong in very basic ways such that I'm not entirely certain what you're trying to do. The PHP tutorial (http://www.php.net/tutorial) might help you.

djr33
10-05-2007, 09:44 PM
1. Quotes do not go around functions.
A random number, for example:
$a = rand(0,1);
Not "rand(0,1)"

2. You will need quotes on any of the keys for the arrays--
$array['key']

You could define a constant, but you haven't done so and there is no need.
(define('name',$value), if you want...)

3. = is NOT the same as ==.
$a=2 will always return true. That sets the value. Or, rather, it will return true if there is no error setting the value.
2=3 will get an error, because you can't redefine a constant.
2==2 will return TRUE*, and 2==3 will return FALSE*.
*However, these are general results. If you use == (or != [not]), then you will get a result equivalent to 0, '' [empty string], FALSE or null on failure and TRUE, 1, [a string], etc. on success.
Using === or !== will give you a precise answer, where it will be only one of the return types, like 0 or [xor] FALSE.
Now, don't worry much about this for now. == will usually do the job.

4. Using "$array['key']" is generally invalid and you'll need to escape the parsing. Use it like "string".$array['key']."string";

And, yes, take a look at a tutorial to get started.

Twey
10-05-2007, 09:50 PM
4. Using "$array['key']" is generally invalid and you'll need to escape the parsing. Use it like "string".$array['key']."string";"{$array['key']}" is perfectly acceptable.

djr33
10-05-2007, 09:52 PM
Not as it was in the above text, though.
I prefer ending the quotes and doing it outside rather than messing with brackets. More confusing to me if I'm using brackets.
Personal preference, though, yes.