Jump to content

Query works in phpmyadmin but not in php code


fivers

Recommended Posts

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?

 

Link to comment
Share on other sites

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'];
		      }
                }

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'];
		 }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.