Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

string comparison in PHP.

Options
  • 28-06-2007 3:26pm
    #1
    Closed Accounts Posts: 289 ✭✭


    Hi
    I have code
    if ($title == $row["Title"])
    	$True_False_Flag = 0;
    

    Its not working , is it better to use the string compare function for string
    comparison in PHP ?

    i need to check if the title above is the same for example..


Comments

  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    You need to post the code in its context of use, and also find out if

    $title is being set properly (try to echo it in code)
    $row["Title"] is being returned properly (try to echo it in code)
    $True_False_Flag is being used correctly (try adding an else to your if statement to see if your conditional statement is actually getting to the if statement in the first place.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    I suggest you output both of those values to the buffer to see if they're even getting to that point, i.e.[PHP]echo "[".$title."][".$row["Title"]."]";[/PHP]
    My guess is that one of them is null (i.e. will echo as '[]') even though you're expecting something there.


  • Closed Accounts Posts: 289 ✭✭berengar


    yeah this is outputting exactly the same string .. so it should work ..
    is there another way like

    if $title.cmp()- $row["Title"]cmp() = 0 ???


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    berengar wrote:
    yeah this is outputting exactly the same string
    Perhaps not. Paste the output here.


  • Closed Accounts Posts: 289 ✭✭berengar


    ok ..

    here is the code ..
    <?php
    //init ..
    
    $True_False_Flag = 1;  //flag for checking if film already exists...
    
    
    $title = $_POST[title];
    $director = $_POST[director];
    $year_ = $_POST[year];
    $actor1 = $_POST[actor1];
    $actor2 = $_POST[actor2];
    $actor3 = $_POST[actor3];
    $comments = $_POST[comments];
    $medium = $_POST[medium];
    
    
    
    $con = mysql_connect("localhost","root");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("Test", $con);
    
    //check if film already exists .. 
     $check_title = "SELECT Title from Films";
    
    $res_check = mysql_query($check_title,$con);
    
    $sql="INSERT INTO Films (Title, Director, year,actor1,actor2,actor3,comments,medium) VALUES('$title','$director','$year_','$actor1','$actor2','$actor3','$comments','$medium')";
    
    while ($row = mysql_fetch_array($res_check))
    {
    //echo $row["Title"]."<br>";
    //echo $title."<br>";                    //check output..
    
    if ($title == $row["Title"])
    	$True_False_Flag = 0;
    
    
    }
    
    if ($True_False_Flag = 1)
    {
    	if (!mysql_query($sql,$con))
    	{
    	 die('Error: ' . mysql_error());
    	}
    	?> <B> <?php echo "...Film added";
    
    
    }
    
    else if ($True_False_Flag = 0)
    {
    print "Film already exists ... ";
    
    
    }
    
    
    mysql_close($con)
    ?> </B>
    <br><br><br>
    <HR>
    <a href="http://localhost/intro.html">Main Menu..</a><br><br>
    
    <BODY>
    
    </BODY>
    </HTML>
    
    


    Basically its a personal little project (DVD db), and here i am making sure the film i am adding is not there already, if it is it wont add it , but it does anyway
    and when i print out the $title and the row["title"] it is the same when a film is already there so it should flag it ..
    but it doesn't and it adds the film anyway..


  • Advertisement
  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Show us the output from the line of code I posted.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    I can't necessarily see any problem, but that's cos I'm not running it.

    There may be a more efficient way of solving your problem though.

    Instead of the lines
    //check if film already exists .. 
     $check_title = "SELECT Title from Films";
    
    $res_check = mysql_query($check_title,$con);
    
    Replace it with these lines
    //check if film already exists .. 
    $check_title = "SELECT Title from Films WHERE Title = '".mysql_escape_string($title)."'";
    mysql_query($check_title,$con);
    
    //mysql_affected_rows tells you how many rows were returned.
    //If mysql_affected_rows is zero, then the flag is set to true. 
    //Otherwise the flag is set to false to signify that there were matches.
    $True_False_flag = !(mysql_affected_rows());
    


  • Closed Accounts Posts: 289 ✭✭berengar


    ok thanks and the output is

    [Heat][Heat]


    to the line
    echo "[".$title."][".$row["Title"]."]";
    


    so they are the same ..


  • Registered Users Posts: 568 ✭✭✭phil


    Use this to test:
    echo "[" . urlencode($title) . "][" . urlencode($row["Title"]) . "]";
    

    and paste the output please.

    This should show up hidden or escaped characters, spaces etc. just in case.

    Phil.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    phil wrote:
    This should show up hidden or escaped characters, spaces etc. just in case.
    Or character set differences, which could be what the problem is.

    Another thing you could try is to see if you can do an 'identical' (===) comparison rather than an 'equal' (==) one, although I'd be surprised if it'll make any difference.


  • Advertisement
  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    while ($row = mysql_fetch_array($res_check))
    {
    //echo $row["Title"]."<br>";
    //echo $title."<br>";                    //check output..
    
    if ($title == $row["Title"])
    	$True_False_Flag = 0;
    
    
    }
    

    I think it could be more of a logic problem. Your SELECT statement is searching for all films, and then checking for title against row[Title], however at the end of the while loop if title!=Title then your flag will never make it to 0. The flag may be set to 0 during the loop, but it will be overwritten as soon as the next record returned fails the IF condition.

    You would be better of to do something like:
    $check_title = " SELECT Title from Films WHERE Title='$title' ";
    ...
    if ($res_check) //res_check returned at least 1 result
    	$True_False_Flag = 0;
    else //no results returned from query
    	$True_False_Flag = 1;
    


Advertisement