Re: setting condition problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, May 2, 2005 1:49 pm, Ahmed Abdel-Aliem said:
> i have a query that grabs records from the database, it works fine,
> but i have a problem with  acondition i want to set for it
> the variable $Record[Relate] has 1 of 2 values, "self" or "share"
> what i wanna do is that when the variable has the value "share" it do
> check the records grabbed and get the other records that has the value
> share and $Record[Title] the same and display them once,
> i don't know how to start doing this,
> can anyone guide me or tell me how to do it
> thanks in advance

I believe you want to do a LEFT OUTER JOIN between your projects table and
some other table.

select * from projects left outer join SOMETHING on projects.title =
SOMETHING.project_title_or_something_like_that

However, you may actually be needing a LEFT OUTER JOIN between your
projects table and itself:

select * from projects as main left outer join projects as second on
main.title = second.title

You will probably, in this second case, not want a record related to
itself, nor to see both of the two versions of the relationship.
EG, suppose your table has only two records, both with the same title:
ID Title
1  setting condition problem
2  setting condition problem

With the above query, you'll end up with:
main.id main.title                  second.id second.title
1       setting condition problem   2         setting condition problem
2       setting condition problem   1         setting condition problem

because the records match up both when you look at ID # 1, and when you
look at ID #2.

The standard way to avoid that is to add this:
where main.id < second.id
into your "ON" clause, so you only see the match-up one time.

> here is the query
>
> <?
> $Query = "SELECT ID FROM projects";
> $Result= mysql_query($Query);
> $Total_Numbers = mysql_num_rows($Result);
> $StartingID=$_GET['StartingID'];
> $StartingID = ($StartingID) ? $StartingID : 0; //if rec is passed in, use
> it,
> $Row = array();
> $Query = mysql_query("SELECT * FROM projects ORDER BY ID DESC LIMIT
> $StartingID,$Items_Numbers");
> $Num = mysql_num_rows($Query);
> while ($Record=mysql_fetch_array($Query)){
> 	$Record[ID] = stripslashes($Record[ID]);

This is probably just plain WRONG.

Your data should not need stripslashes() called on it when it comes out of
the database, unless you have specifically set up MagicQuotes on database
output, which is pretty uncommong.

More likely, you've got both MagicQuotes on for your GPC data, *AND* you
are calling addslashes before you insert the data, so you end up with
doing addslashes *TWICE* before an insert, which is wrong.

It's also possible you aren't doing any of that, and your data has never
had any need for addslashes, and you are just calling stripslashes because
you don't understand how/why addslashes works/exists.

When you feed data to MySQL, the MySQL engine needs certain special
characters to be "escaped" so that it interprets them as DATA and not as
part of the MySQL syntax.

addslashes and magic quotes and mysql_escape_string are all designed to do
this.  But you should only use *ONE* of them and only one time before
INSERT (or UPDATE etc) the data.

The very act of MySQL executing the query, interpreting the escaped
string, essentially reverses out the escaping -- It's how MySQL knows to
store the data as DATA and not to interpret it as commands.  But MySQL
doesn't *keep* the escape characters embedded in the DATA it stores, and
you shouldn't have anything to strip out while retrieving the data.

Hope that makes sense.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux