Query works in phpMyAdmin but doesn't when using php code

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 ‘


’;
}
}[/php]

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 ‘


’;
echo ‘[’.$row[‘Child’].’]’;
echo ‘
’;
echo ‘[’.$g.’]’;
echo ‘
’;
echo ‘/’.str_replace(’-’,’-?’,$g).’/’;
echo ‘
’;
echo preg_match(’/’.str_replace(’-’,’-?’,$g).’/’,$row[‘Child’]);
echo ‘
’;[/php]

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!

Aww, I’m assuming the mysqli method didn’t work then. But good to hear you’ve got it working nonetheless :D.

First of all sorry about my english.

Try to obtain all data
[php]$sql = “SELECT * FROM Parent_Child”;
$res = mysql_query($sql);
echo" BEGIN
";
while($row= mysql_fetch_array($res)){
print_r($row);
}
echo “END
”;[/php]

if don’t show data, check user and password try in phpmyadmin to use this user an password and execute SELECT, must be posible this user don’t has privileges to show this table.

Sponsor our Newsletter | Privacy Policy | Terms of Service