3

Here is my code:

$sql = "SELECT `description` FROM `auctions` WHERE `description` REGEXP '[0-9]{10}'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
  echo $row["description"];
}

This returns the whole field description, I need to just get the part that matches the REGEXP, how do I do this using php?

3

2 Answers 2

1
$sql = "SELECT `description` FROM `auctions` WHERE `description` REGEXP '[0-9]{10}'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    preg_match('/\d{10}/', $row["description"], $match);
    echo $match[0];
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$results = array();
$sql = "SELECT `description` FROM `auctions` WHERE `description` REGEXP '[0-9]{10}'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    // Checks for a match in the description and capture the match to $match
    preg_match("/[0-9]{10}/", $row['description'], $match);

    // Save the complete match (the ten digits) to the results array.
    $results[] = $match[0];
    // Or just echo them
    echo $match[0];
}

By the way, you also should take a look at prepared statements, inter alia to prevent SQL injections.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.