I have this portion of code and it gives me the error in the title.
I have a count $k
from 1 to 5 for each table.
$myarray
consists of at least 3 to 4 names.
The error occurs in the line with $qu .=
...
What i tried so far: changing the variablename $i
to $v{$i}
in the $qu .=
line.
So is there any possibility to iterate the query? So that it has as many AND
s in the WHERE
clause as the count of the array is?
while ($k<=5) {
$queryname = "SELECT Name FROM description";
$qname = mysql_query($queryname,$link);
while ($reihe = mysql_fetch_object($qname)) {
{
$unse = unserialize($reihe->Name);
{
foreach ($unse as $j=>$h)
foreach ($h as $m) {
$myarray = preg_split('/ |\s| /',$m);
{
echo "<br>";
$array_empty = $myarray;
$empty_elements = array("");
$myarray = array_diff($array_empty,$empty_elements);
var_dump($myarray);
for ($i=1; $i<=count($myarray); $i++) {
$v{$i} = $myarray[$i];
echo $v{$i};
$esc{$i} = strtolower(mysql_escape_string($v{$i}));
echo "<br>" . $esc{$i} . "<br>";
$qu = "SELECT * FROM `table ID=$k` WHERE";
$qu{$i} .= "AND `table ID=$k`.`name` LIKE '%$esc{$i}%'";
}
}
}
{
$test_a = mysql_query($qu,$link) or die (mysql_error());
echo "<br>";
var_dump($test_a);
for ($x=0; $x<mysql_num_rows($test_a); $x++) {
$row = mysql_result($test_a,$x,'data1');
$namee = mysql_result($test_a,$x,'data2');
echo 'data1' . $row . '<br>';
echo 'data2' . $namee . '<br>';
}
}
}
}
}
$k++;
}
You appear to have misunderstood some basic PHP syntax.
As mentioned in the manual:
Therefore your use of curly braces throughout your code (except for those defining the scope of the
for
control structure) are wholly erroneous and do not accomplish what you intend.Secondly, from your code it appears that you are storing relational data in serialised PHP objects; this defeats many of the benefits of using an RDBMS like MySQL. Unless you have compelling reasons for doing otherwise, you should probably store your PHP objects in a normalised form. For example, rather than each
description
record having aName
field that contains a serialised PHP object whose properties are arrays that hold imploded strings of names, just store each such name in a newdescriptionNames
table that references the related record in thedescription
table:It also appears that you have five (schematically) identical tables named
Table ID=1
,Table ID=2
, etc.? If so, you should probably combine your five tables into one, with a column (if so desired) to indicate from which table the record originated; I would also suggest changing your table names so that they avoid using special characters like whitespace and=
as they will likely only cause trouble and confusion further down the road if you forget to properly quote them. For example:In any event, you shouldn't use the ancient
mysql_*
functions. They are no longer maintained and the community has begun the deprecation process. Instead you should learn about prepared statements and use either the PDO abstraction layer or else the improved MySQLi extension.If you need to maintain your existing data structure, with PDO you could do something like:
However, using my proposed new data structure, you would only need do: