Page 1 of 1

New at php ... cannot compare strings ...

Posted: Tue Aug 26, 2008 4:02 pm
by stevoo
I am new just started php.
I am working on understanding AJAx , but my problem is not there

I have my psql database connection and whenever i try to do this

Code: Select all

$correct = 1;
    while($row = pg_fetch_array($result))
    { 
    echo "Checking ".$gamename." with ".$row['gamename'];
        if ($gamename == $row['gamename']){
            $correct = 0;
            echo "FOUND";
        }
    }
    if (correct  == 0)
        echo "<h4>Checking  duplicated Game Name... $error</h4>".$correct;
    else 
        echo "<h4>Checking duplicated Game Name... $ok</h4>".$correct;
        
 
although when runned
i can see this
Checking stevoo with stevoo

which is identical i cannot compare my gamename with the one that comes from my database.

What am i doing wrong ?

sTevoo

Re: New at php ... cannot compare strings ...

Posted: Tue Aug 26, 2008 4:18 pm
by Ziq
Try edit your code

Code: Select all

 
$correct = 1;
    while($row = pg_fetch_array($result))
    {
    echo "Checking ".$gamename." with ".$row['gamename'];
    var_dump($gamename);
    var_dump($row['gamename']);
        if ($gamename == $row['gamename']){
            $correct = 0;
            echo "FOUND";
        }
    }
    if (correct  == 0)
        echo "<h4>Checking  duplicated Game Name... $error</h4>".$correct;
    else
        echo "<h4>Checking duplicated Game Name... $ok</h4>".$correct;
 

What are you see in output?

Re: New at php ... cannot compare strings ...

Posted: Tue Aug 26, 2008 4:21 pm
by stevoo
Checking stevoo with stevoo string(6) "stevoo" string(25) "stevoo "


hmmm

Re: New at php ... cannot compare strings ...

Posted: Tue Aug 26, 2008 4:24 pm
by Ziq
try use trim() function

Code: Select all

 
if (trim($gamename) == trim($row['gamename'])){
 
Then you insert data in database you make mistake. Check this.

Re: New at php ... cannot compare strings ...

Posted: Tue Aug 26, 2008 4:26 pm
by stevoo
Thanx i was almost about to find it ...

but isnt there an easier way ?

When a user created a value, will i always do that ?
Is there any way to remove the whites before inserting ?
or it something i have to live with ...

Re: New at php ... cannot compare strings ...

Posted: Tue Aug 26, 2008 4:39 pm
by Ziq

Code: Select all

 
When a user created a value, will i always do that ? 
Is there any way to remove the whites before inserting ? 
 
Yeh. Just use trim() function before insert data in database. And necessary check input information for protect your script. Not only trim() function but and addslashes() etc. If you interested, first read information about SQL Injection

Re: New at php ... cannot compare strings ...

Posted: Tue Aug 26, 2008 4:48 pm
by stevoo
hm ... i kind of new that sql injections could be bad ... but i had no idea of the length ....

A quick read open my eyes :)

Thanx for referencing it