fivers Posted July 19, 2011 Share Posted July 19, 2011 Hi all, I'm running on mySQL Server version: 5.0.83 , and I've been running into an issue where a query works in phpmyadmin but does not in my code. I have a table set up with 2 columns titled Parent and Child and the table name is Parent_Child. It is simply two IDs that associate one with the other. The SQL statement that I want to run is: SELECT * FROM Parent_Child WHERE Child ='8C8FDBCB-0984-49D8-BE14-1942E26600C4' What this query should return is two columns, Parent and Child with the corresponding ID's where the child is 8C8FDBCB-0984-49D8-BE14-1942E26600C4 . In my php code, I have the standard mysql_connect statements, but i'll leave those out to cut down on this post size. I know it can connect because other queries work, just this one is having issues. In php, I have a variable $sql that holds the sql query. $g is the ID of whatever i'm looking up. In this case let's just say $g is 8C8FDBCB-0984-49D8-BE14-1942E26600C4 . $sql= "SELECT * FROM Parent_Child WHERE Child ='".$g."'"; I've also tried to change the $sql string as such: $sql = "SELECT * FROM Parent_Child WHERE Child = '$g'"; and $sql = "SELECT * FROM Parent_Child WHERE Child = '" .'$g' . "'"; After this, I run $result= mysql_query($sql); echo $sql; while ($row = mysql_fetch_array($result)) { echo $row['Parent']; } When I copy/pate the echos output of $sql directly into phpmyadmin, it runs the query perfectly.. it comes out as SELECT * FROM Parent_Child WHERE Child ='8C8FDBCB-0984-49D8-BE14-1942E26600C4' But it doesn't echo anything out for $row['Parent']. I've checked the size of the output and it's always seems to be null.. mysql_error() doesn't return anything whenever I place it where it needs to be.. Any pointers or errors in the sql query code that you see? Quote Link to comment https://forums.phpfreaks.com/topic/242327-query-works-in-phpmyadmin-but-not-in-php-code/ Share on other sites More sharing options...
AyKay47 Posted July 19, 2011 Share Posted July 19, 2011 Have you tried seeing if your mysql query is actually grabbing any rows? $result= mysql_query($sql); print mysql_num_rows($result); if(mysql_num_rows($result) > 0){ while ($row = mysql_fetch_array($result)) { echo $row['Parent']; } } Quote Link to comment https://forums.phpfreaks.com/topic/242327-query-works-in-phpmyadmin-but-not-in-php-code/#findComment-1244593 Share on other sites More sharing options...
fivers Posted July 19, 2011 Author Share Posted July 19, 2011 Prints 0 so I guess not? What could be the cause of this then? It outputs $sql exactly as the query I want but it's just not fetching any rows I guess Quote Link to comment https://forums.phpfreaks.com/topic/242327-query-works-in-phpmyadmin-but-not-in-php-code/#findComment-1244594 Share on other sites More sharing options...
AyKay47 Posted July 19, 2011 Share Posted July 19, 2011 the query itself is working, but the id that you have is not matching any id's that are in your database field "Child", the next step would be to double check these values Quote Link to comment https://forums.phpfreaks.com/topic/242327-query-works-in-phpmyadmin-but-not-in-php-code/#findComment-1244595 Share on other sites More sharing options...
fivers Posted July 19, 2011 Author Share Posted July 19, 2011 I did check, there is a row with the Parent as 33BD0276-50E2-4F6D-A5DD-0B588D1DD33F and the child as 8C8FDBCB-0984-49D8-BE14-1942E26600C4 . It returns my answer when I run it in phpmyadmin so I think there's a matching ID for Child yeah? Quote Link to comment https://forums.phpfreaks.com/topic/242327-query-works-in-phpmyadmin-but-not-in-php-code/#findComment-1244599 Share on other sites More sharing options...
AyKay47 Posted July 19, 2011 Share Posted July 19, 2011 hmm, my guess is that they don't match, but if you say that they do...what field types are these two? Can you post the code that surrounds this query please...perhaps something there is causing this to fail Quote Link to comment https://forums.phpfreaks.com/topic/242327-query-works-in-phpmyadmin-but-not-in-php-code/#findComment-1244610 Share on other sites More sharing options...
fivers Posted July 19, 2011 Author Share Posted July 19, 2011 Sure $con=mysql_connect("localhost", "someuser", "somepass"); if(!$con) { die('Error: ' . mysql_error()); } @mysql_select_db("project", $con) or die("Unable to connect to database"); $sql= "SELECT * FROM Parent_Child WHERE Child ='".$g."'"; $result= mysql_query($sql); echo $sql; while ($row = mysql_fetch_array($result)) { echo $row['Parent']; } Quote Link to comment https://forums.phpfreaks.com/topic/242327-query-works-in-phpmyadmin-but-not-in-php-code/#findComment-1244620 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.