Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

mysql instead of $string

this is a followup to question
https://www.experts-exchange.com/questions/27838075/php-difference-in-strings.html

where I want to gather data from title field in my mysql table
instead of $string[]

<?php // RAY_temp_rgb192.html
error_reporting(E_ALL);
echo "<pre>";

// COPIED FROM THE POST AT EE
$string[1]='hello, how are you';
$string[2]='very different';
$string[3]='hi, how are you';
$string[4]='another string';

$string[] = 'Yousef Islam';
$string[] = 'Yusuf Islam';
$string[] = 'Yusef Islaam';
$string[] = 'Yousif Isla\'am';
$string[] = 'YUSUF Islam';

// COMPARISONS USING SOUNDEX+LEVENSHTEIN
foreach ($string as $x)
{
    // COMPUTE THE SOUNDEX KEY
    $sx = soundex($x);
    echo PHP_EOL . "TESTING <b>$x</b> WITH SOUNDEX() $sx";

    // COMPARE TO THE OTHER STRINGS
    foreach ($string as $y)
    {
        $sy = soundex($y);
        $sl = levenshtein($sx, $sy);
        echo PHP_EOL
        . "SOUNDEX() $sx"
        . " IS $sl DISTANCE FROM $sy";
    }
}
echo PHP_EOL;

Open in new window



changed to

<?php // RAY_temp_rgb192.html
include '5/db.php';
error_reporting(E_ALL);
echo "<pre>";
$result=mysql_query("select * from table order by title limit 0,4");
//$results=mysql_fetch_array($query);

//echo '<br>results content: {'.$result['title'].'}';
//foreach ($results as $row){
/*while ($row=mysql_fetch_array($result,MYSQL_ASSOC)){
  echo '<br>'.$row['title'];
}*/

// COPIED FROM THE POST AT EE
$string[1]='hello, how are you';
$string[2]='very different';
$string[3]='hi, how are you';
$string[4]='another string';

$string[] = 'Yousef Islam';
$string[] = 'Yusuf Islam';
$string[] = 'Yusef Islaam';
$string[] = 'Yousif Isla\'am';
$string[] = 'YUSUF Islam';

// COMPARISONS USING SOUNDEX+LEVENSHTEIN
//foreach ($string as $x)
//foreach (mysql_fetch_array($result,MYSQL_ASSOC) as $x)
while ($x=mysql_fetch_array($result,MYSQL_ASSOC))
{
  
  echo 'hello'.$x['title'];
    // COMPUTE THE SOUNDEX KEY
    $sx = soundex($x);
    echo PHP_EOL . "TESTING <b>$x</b> WITH SOUNDEX() $sx";

    // COMPARE TO THE OTHER STRINGS
    //foreach ($string as $y)
    while ($y=mysql_fetch_array($result,MYSQL_ASSOC))    
    {
        $sy = soundex($y);
        $sl = levenshtein($sx, $sy);
        echo PHP_EOL
        . "SOUNDEX() $sx"
        . " IS $sl DISTANCE FROM $sy";
    }
}
echo PHP_EOL;

Open in new window

SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What was your question?
@julian - mysql_fetch_array allows associative indexes by default. He also specified the MYSQL_ASSOC parameter later, too.

@rgb192 - Change:
$sy = soundex($y);
to
$sy = soundex($y['title']);
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As gr8gonzon demonstrated I have only had a superficial look at the code but it occurred to me that you might be able to do this with a SQL query - I am under the cosh a bit so can't look at this in depth so maybe Ray_Paseur or Gr8Gonzo can pick this up (I don't want the points)

I was thinking along the lines of
SELECT DISTINCT title FROM table WHERE SOUNDEX(title) 
IN (SELECT SOUNDEX(title) FROM table ORDER BY title) 

Open in new window

Not quite the same because subqueries don't support LIMIT and original was only taking the top 4 results - but might be something to exploit through MySQL on this.
Avatar of rgb192

ASKER

thanks for code samples
Thanks for the points but as gr8gonzo pointed out my recommendation would not have made much of a difference.
Avatar of rgb192

ASKER

I have a similar question

https://www.experts-exchange.com/questions/27877009/want-to-echo-many-columns-from-same-row.html

where I want to echo many columns from same row