Okay, I think I know the solution then… fingers crossed!
[php]$sql = “SELECT * FROM Parent_Child
WHERE Child
LIKE '%”.$g."%’";[/php]
Try that and let me know!
Okay, I think I know the solution then… fingers crossed!
[php]$sql = “SELECT * FROM Parent_Child
WHERE Child
LIKE '%”.$g."%’";[/php]
Try that and let me know!
Nope didn’t work.
Damn, okay…
[php]$sql = “SELECT * FROM Parent_Child
WHERE Child
LIKE '%”.trim(strtoupper($g))."%’";[/php]
This didn’t work either . So I think we’ve narrowed down the issue to being the variable $g causing the issue right? Not sure if this helps at all but both Parent and Child are Varchars with max length 255.
Yea looks like $g is playing games with us. Hmm that shouldn’t make a difference, especially if you say this works if you put the value into phpmyadmin and it works… what if you do this? (we’ll get there eventually!)
[php]$childlookup = trim(strtoupper(str_replace(’-’,’%’,$g))); //So many functions, thought might make the $sql= line a bit too long!
$sql = “SELECT * FROM Parent_Child
WHERE Child
LIKE '%”.$childlookup."%’";[/php]
Dang still no luck with this one either…
Things just got stranger… I recreated your table structure locally and ran the same script that you are and all is working fine.
And just to reconfirm, if you literally only delete from “WHERE …” onwards and run the script the same it works and returns all the rows. And as soon as you put the WHERE clause back in it breaks?
If this is the case then you may need to check how you are passing the string to the function.
Yeah removing everything from WHERE onwards returns all the rows.
I’m calling this getParent method in another method as such:
[php]public function printParent()
{
$this->getParent($this->ID);
}[/php]
Where ID is a public variable declared in this class.
[php]public $ID = ‘’;[/php]
but in my code elsewhere the ID is set to 8C8FDBCB-0984-49D8-BE14-1942E26600C4
Haha, this isn’t ideal… but a workaround! What if PHP does the detection of the child?
[php] $sql = “SELECT * FROM Parent_Child
“;
$result=mysql_query($sql);
if($result === false) echo mysql_error();
while ($row = mysql_fetch_array($result))
{
if(preg_match(’/’.str_replace(’-’,’-?’,$g).’/’,$row[‘Child’]) > 0)
{
echo $row[‘Parent’].”
”;
echo $row[‘Child’];
echo ‘
Still no output. $row[‘Parent’] and $row[‘Child’] do have values though, as I attempted to echo those and they came out.
ok… do you know what php version you are running?
[php]echo ‘[ PHP Version: ‘.phpversion().’ ]’;[/php]
PHP Version: 5.2.13
and mySQL version: 5.0.83
phpmyadmin version: 3.2.0.1
It may also be worth adding the line:
[php]phpinfo();[/php]
Do a search on the page for “mysql support” - see if it’s enabled and what the client api version is.
oo, you’re way ahead of me…
and damn, i was hoping this was due to an outdated installation.
In the while loop, can you do this and show me the result?
[php]echo ‘
sorry for late response, had to go zz @_@.
Here’s what that outputs
[php][8C8FDBCB-0984-49D8-BE14-1942E26600C4]
[A274BFE8-846C-486D-9990-FFDCC96271A2]
/A274BFE8-?846C-?486D-?9990-?FFDCC96271A2/
0[/php]
as well as all the other ones
apparently I can’t delete posts. That post above is wrong actually. I have the $g as A274BFE8-846C-486D-9990-FFDCC96271A2 where it should be 8C8FDBCB-0984-49D8-BE14-1942E26600C4. I was testing it with other values of $g.
Assuming that $g is 8C8FDBCB-0984-49D8-BE14-1942E26600C4, the output is actually like this:
[php][E6DE79ED-BF8F-48E2-8360-2F9A760C2AD5]
[8C8FDBCB-0984-49D8-BE14-1942E26600C4]
/8C8FDBCB-?0984-?49D8-?BE14-?1942E26600C4/
0[9C797418-3CE9-48AE-A0CC-EA36E1C31E6C]
[8C8FDBCB-0984-49D8-BE14-1942E26600C4]
/8C8FDBCB-?0984-?49D8-?BE14-?1942E26600C4/
0[192CED35-2FE6-4E5F-9891-8290344347A8]
[8C8FDBCB-0984-49D8-BE14-1942E26600C4]
/8C8FDBCB-?0984-?49D8-?BE14-?1942E26600C4/
0[9BFEDC05-2A9F-4876-AB5B-C15B2AEE31AD]
[8C8FDBCB-0984-49D8-BE14-1942E26600C4]
/8C8FDBCB-?0984-?49D8-?BE14-?1942E26600C4/
0[1B2F3EF8-51A9-495E-A080-EAB091D9C91E]
[8C8FDBCB-0984-49D8-BE14-1942E26600C4]
/8C8FDBCB-?0984-?49D8-?BE14-?1942E26600C4/
0[86FD8C32-02FE-4DBB-B281-E3B984EAFA13]
[8C8FDBCB-0984-49D8-BE14-1942E26600C4]
/8C8FDBCB-?0984-?49D8-?BE14-?1942E26600C4/
0[F63C224F-147C-4564-A4E9-99E7573D741E]
[8C8FDBCB-0984-49D8-BE14-1942E26600C4]
/8C8FDBCB-?0984-?49D8-?BE14-?1942E26600C4/
0[ACAF942E-54C8-47E1-A003-D44D7B645DD7]
[8C8FDBCB-0984-49D8-BE14-1942E26600C4]
/8C8FDBCB-?0984-?49D8-?BE14-?1942E26600C4/
0[/php]
I’ve truncated it because there’s like 50 relationships in my table and I don’t think you want to see all that
Right, apologies for the late reply - not managed to get back to my PC since my last post. This issue (or very similar at least) cropped up at work today and after some playing it eventually worked by changing to use mysqli instead of mysql - so it would look something like this:
[php]function getParent($g)
{
$con= new mysqli(“host”, “user”, “password”, “database”);
if($con->connect_errno)
{
die(‘Connect Error: ’ . $con->connect_errno);
}
$sql = “SELECT * FROM Parent_Child
WHERE Child
= '”.$g."’";
$result= $con->query($sql);
if($result === false) echo $con->error();
while ($row = $result->fetch_array())
{
echo ‘"’.$row[‘Parent’].’"
is the parent of
“’.$row[‘Child’].’”
(’.$g.’)’;
}
$result->close();
$con->close();
}
[/php]
Try it and let’s hope it works!!
Hey Smokey,
Thanks for all your hard work, I’ve managed to get a working solution although it isn’t quite efficient once my database scales higher. The core principle is essentially filtering through all of the results called from $sql = "SELECT * FROM Parent_Child
", which ends up with something like O(n) run time. I got this idea from one of your earlier responses, as you were iterating through all the entries.
[php]function getParent($g)
{
error_reporting(E_ALL);
//global $up_array;
$temp_par=’’;
$con=mysql_connect(“", "”, “****”);
if(!$con)
{
die('Error: ’ . mysql_error());
}
@mysql_select_db("tedproject", $con) or die("Unable to connect to database");
$sql = "SELECT * FROM `Parent_Child`";
$result=mysql_query($sql);
if($result === false) echo mysql_error();
while ($row = mysql_fetch_array($result))
{
$temp_par= $row['Parent'];
$temp_child= $row['Child'];
if($temp_child === $g)
{
break;
}
}
if($temp_child != $g)
{
$temp_par=NULL;
}
return $temp_par;
}
[/php]
I’m still not sure why sql breaks because of that WHERE clause, but I’m relieved I have a temporary solution while my DB is still small haha!