Jump to content

How do I Compare Strings?


doubledee

Recommended Posts

Not sure if there is a better way to do this, but I want to check whether the value in a Sticky Field is the same as what is in the Database.

 

If what is in the Sticky Field is *exactly* what is in the Database, then the User didn't update that field, and so I would NOT do an UPDATE.

 

Seem reasonable?

 

Here is where I grab the value out of the Form...

// ****************************
// Process Each Form Field.		*
// ****************************
foreach($_POST['answerArray'] as $questionID => $response){
	// Copy trimmed Form array to PHP array.
	$answerArray[$questionID] = trim($response);

 

 

And here is where I grab the existing value in the Database...

// ********************
// Check for Answer.	*
// ********************

// Build query.
$q1 = "SELECT response
		FROM bio_answer
		WHERE member_id=? AND question_id=?";

// Prepare statement.
$stmt1 = mysqli_prepare($dbc, $q1);

// Bind variable to query.
mysqli_stmt_bind_param($stmt1, 'ii', $memberID, $questionID);

// Execute query.
mysqli_stmt_execute($stmt1);

// Store results.
mysqli_stmt_store_result($stmt1);

// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt1)==1){
	// Answer Found.

	// Bind result-set to variable.
	mysqli_stmt_bind_result($stmt1, $currResponse);

	// Fetch record.
	mysqli_stmt_fetch($stmt1);

 

 

So I guess I want to compare $answerArray[$questionID] against $currResponse and see if there are any differences?!

 

What is the best way to do that?

 

Any better ideas of how to check to see if the User made any changes to a field so I only do an UPDATE if there was actually a change?

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

You can just do the update regardless.  Checking if it's changed or not just to avoid an update query is not worth while.  MySQL will already check during it's update process if the field value has actually changed, and if not skip updating that field.

 

If you set a column to the value it currently has, MySQL notices this and does not update it.

Link to comment
Share on other sites

Kicken is right there. If there is some other reason that won't work for your situation you can use strpos to find a very exact string. If its not found a false will be returned. However, if the search finds that exact string and there is more to the left or right it will still return the position of that match. I found this quickly and I'm sure there are functions that are purely Boolean and return a true or false instead of false or a position, search php.net.

 

Thanks,

 

AlgidDes

Link to comment
Share on other sites

. . . you can use strpos to find a very exact string. If its not found a false will be returned. However, if the search finds that exact string and there is more to the left or right it will still return the position of that match. I found this quickly and I'm sure there are functions that are purely Boolean and return a true or false instead of false or a position, search php.net.

 

You are correct that there are more appropriate functions. strcmp(), short for "string comparison" will do exactly as you suspected.

strcmp — Binary safe string comparison
Link to comment
Share on other sites

This is what I ended up doing...

// ********************************
// Check for Changes to Answer.		*
// ********************************
if (strcmp($answerArray[$questionID], $currResponse)==0){
	// Answer Unchanged.
	echo '<p>Field #' . $questionID . ' has NOT changed.</p>';

}else{
	// Answer Changed.
	echo '<p>Field #' . $questionID . ' has changed.</p>';


	// ******************
	// Update Answer.		*
	// ******************

	// Build query.
	$q2 = "UPDATE bio_answer
			SET response=?,
			updated_on=NOW()
			WHERE member_id=? AND question_id=?
			LIMIT 1";

 

But according to Kicken, that was unnecessary, I guess?!

 

 

Debbie

 

Link to comment
Share on other sites

You will need to do your comparison check if you want to prevent the updated_on field from being changed when there was no content changes.  MySQL would still update that even if the response value had not changed.

 

Yep, that was the whole point!!

 

Okay, so it looks like my extra effort paid off.

 

Thanks!!

 

 

Debbie

 

 

Link to comment
Share on other sites

You will need to do your comparison check if you want to prevent the updated_on field from being changed when there was no content changes.  MySQL would still update that even if the response value had not changed.

 

Which is why an "updated_on" field should be set up as a "TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" so the updated timestamp would be handled by the database. Then there is no need to include a timestamp in the UPDATE queries and if the UPDATE query would not result in any changes, the timestamp is not updated either. That is the simplest solution.

 

Okay, so it looks like my extra effort paid off.

 

Sorry, but no.

Link to comment
Share on other sites

You will need to do your comparison check if you want to prevent the updated_on field from being changed when there was no content changes.  MySQL would still update that even if the response value had not changed.

 

Which is why an "updated_on" field should be set up as a "TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" so the updated timestamp would be handled by the database. Then there is no need to include a timestamp in the UPDATE queries and if the UPDATE query would not result in any changes, the timestamp is not updated either. That is the simplest solution.

 

Hey, that's pretty cool!  I learned something new!  8)

 

 

Okay, so it looks like my extra effort paid off.

 

Sorry, but no.

 

Oh well.

 

 

Debbie

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.