www.webdeveloper.com
Results 1 to 4 of 4

Thread: Good fucntion to search a string

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    69

    Good fucntion to search a string

    I can't seem to find the right function to search a string. I want to grab the email out of the error code and then display that to the user but the function I am using returns everything after the string is found also. What function can I use to only return what I am looking for?

    The output of the below code is user@email.com' for key 'PRIMARY' has already been registered. I only want user@email.com returned.

    PHP Code:
                    //Grabs the error message
                    
    $errorMessage mysqli_error($dbConnect);
                    
    //grabs the email from the error mesage
                    
    $usedEmail strstr($errorMessage$email);
                    
    // tells users email has already been used.
                    
    $regMessage $usedEmail " has already been registered"

  2. #2
    Join Date
    Nov 2013
    Posts
    69
    So I ended up using the
    PHP Code:
    rtrim() 
    function to trim what I didn't want off of the string but surely there is a function that will only grab the string that is passed to it and nothing before or after it.

    PHP Code:
                    //Grabs the error message
                    
    $errorMessage mysqli_error($dbConnect);
                    
    //grabs the email from the error mesage
                    
    $usedEmail strstr($errorMessage$email);
                    
    // tells users email has already been used.
                    
    $usedEmail rtrim($usedEmail,"' for key 'PRIMARY' has already been registered"); 
                    
    $regMessage $usedEmail " has already been registered"

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    You already have the email in $email, so just use that:
    PHP Code:
    if(strpos($errorMessage$email) !== false) {
      
    $regMessage $email " has already been registered";
    }
    else {
      
    // some other error?

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Sep 2013
    Posts
    221
    Below is the example code which i had used in one of my application and had worked fine for me:
    <?php

    $con = mysqli_connect("localhost", "", "", "");
    // Check connection
    if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
    }
    $existsQuery = "select count(*) as count from entry where emailaddress like '".$_POST[emailaddress]."'";
    $existsResult = mysqli_query($con, $existsQuery);

    if($existsResult->fetch_object()->count > 0)
    {
    echo "email already exist";
    }
    else
    {
    $sql = "INSERT INTO entry (firstname, lastname, emailaddress, favoritesong) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[emailaddress]','$_POST[favoritesong]')";

    if (!mysqli_query($con, $sql)) {
    die('Error: ' . mysqli_error($con));
    }
    echo "1 record added";
    }

    mysqli_close($con);
    ?>

    The error here is been displayed by database if the email address entered in form already exists.
    Hope this helps.
    strad solutionswww.stradsolutions.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles