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

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
stevoo
Forum Newbie
Posts: 9
Joined: Tue Aug 26, 2008 3:58 pm

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

Post 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
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

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

Post 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?
stevoo
Forum Newbie
Posts: 9
Joined: Tue Aug 26, 2008 3:58 pm

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

Post by stevoo »

Checking stevoo with stevoo string(6) "stevoo" string(25) "stevoo "


hmmm
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

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

Post 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.
Last edited by Ziq on Tue Aug 26, 2008 4:27 pm, edited 1 time in total.
stevoo
Forum Newbie
Posts: 9
Joined: Tue Aug 26, 2008 3:58 pm

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

Post 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 ...
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

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

Post 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
stevoo
Forum Newbie
Posts: 9
Joined: Tue Aug 26, 2008 3:58 pm

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

Post 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
Post Reply