-1

I am attempting to search a SQL table from a PHP script. the SQL table is a word list and in the PHP I call a Python script to do the permutations of a given word. Everything is working until I actually go to execute the mysql_query. I need some formatting advice on how to pass so many values to a Select statement. From what I've seen it needs to be in the form ('a','b','c',...) and this is how it is formatted but I'm not getting a return on the actual execution.

<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
$con= mysql_connect("127.0.0.1","app","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("wordlookup");


//Retrieve the data.
$word = $_POST['data'];


$perm = exec("python comb.py $word");

$query="SELECT * FROM words WHERE IN (" .$perm. ")";
print $query;
$sql=mysql_query($query);
print $sql;



mysql_close($con);
?>

This is all of the PHP file, the output from the python call would be in the 'a','b','c'... format and the random prints are just debugging. The print $sql doesn't actually put out anything at the moment. Thanks, Kyle

2
  • You aren't doing any kind of error checking. Add error checking to your code and see where (and why) it dies. Commented Feb 8, 2012 at 6:40
  • Use $sql1 = mysql_fetch_array($sql) or $sql1 = mysql_fetch_object($sql). Use for loop according to the no of records. Commented Feb 8, 2012 at 6:40

2 Answers 2

2
SELECT * FROM words WHERE IN (...)

Your query is missing a condition. WHERE what IN ...? You need to fill in a column name there.

Further, if a query is not working, ask the database why it didn't work:

$result = mysql_query($query);
if (!$result) {
    echo mysql_error();
}
1
  • Lead me to way to fix it, had to add a few more things to get it to fully work. Commented Feb 8, 2012 at 7:21
0

Though solution is strange (use external script executed via exec to calculate permutations?), I'd answer your exact question.

$perm_arr = explode(' ', $perm);
function quote($str) { return "'$str'"; }
$perm_arr = array_map('quote', $perm_arr);
$sql = "select * from words where word in (" . join(', ', $perm_arr) . ")";

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.