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?
$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];
}
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.