Author Topic: Query works in phpMyAdmin but doesn't when using php code  (Read 1796 times)

five

  • New Member
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Query works in phpMyAdmin but doesn't when using php code
« on: July 19, 2011, 08:22:29 AM »
Hi all,

I've been trying to get this sql query to work from php, but i cannot seem to get it to. I am hoping that someone else has experienced this and knows a solution or can point out my mistakes.

I have a table in my sql db titled Parent_Child, and as you may imagine is just two IDs associating the two together. The two fields are Parent and Child.

I want to run a query like this:
PHP Code: [Select]
SELECT FROM Parent_Child WHERE Child ='8C8FDBCB-0984-49D8-BE14-1942E26600C4'

It works when I perform it in phpMyAdmin, but does not when I attempt to run the query using mysql_query.

PHP Code: [Select]

//$g is the ID of the current child.
$sql"SELECT * FROM Parent_Child WHERE Child ='".$g."'";
$resultmysql_query($sql);
while (
$row mysql_fetch_array($result))
 
	
	
{
 
	
	
	
echo 
$row['Parent'];
 
	
	
}


It's weird because when i echo $sql, it outputs the query, and when I paste that directly into phpMyAdmin query, it works.. If I passed $g as 8C8FDBCB-0984-49D8-BE14-1942E26600C4, it would give me my desired output in the variable $sql as: SELECT * FROM Parent_Child WHERE Child ='8C8FDBCB-0984-49D8-BE14-1942E26600C4'

note: I have the mysql_connect stuff also but that isn't the focus of the problem. I know it connects to the DB it just doesn't execute the query properly.


Any thoughts?

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 528
  • Karma: +5/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #1 on: July 19, 2011, 12:44:34 PM »
Hi there,

Is it completing the query at all? Above the script put:
PHP Code: [Select]
error_reporting(E_ALL);

And change this bit:
PHP Code: [Select]
$resultmysql_query($sql);
while (
$row mysql_fetch_array($result))


to:
PHP Code: [Select]
$resultmysql_query($sql);
if(
$result === false) echo mysql_error();
while (
$row mysql_fetch_array($result))

five

  • New Member
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #2 on: July 19, 2011, 01:01:18 PM »
Hiya,

Just tried that and it doesn't return anything. It just outputs a blank line. What would be stopping it from completing the query? Here's the entire function in case its somewhere else that I'm going wrong with what you asked me to do.

PHP Code: [Select]

function getParent($g)
 
	
{
 
 
	
	
error_reporting(E_ALL);
 
	
	

 
	
 
	
$con=mysql_connect("localhost""someuser""somepass");
 
	
	
if(!
$con)
 
	
	
{
 
	
	
	
die(
'Error: ' mysql_error());
 
	
	
}

 
	
	
@
mysql_select_db("someproject"$con) or die("Unable to connect to database");
 
	
	

 
	
	
$sql"SELECT * FROM Parent_Child WHERE Child ='".$g."'";
 
	
	

 
	
	
$result=mysql_query($sql);
 
	
	
if(
$result === false) echo mysql_error();
 
	
	
while (
$row mysql_fetch_array($result))
 
	
	
{
 
	
	
	
echo 
$row['Parent'];
 
	
	
}
}


The output is:
PHP Code: [Select]
8C8FDBCB-0984-49D8-BE14-1942E26600C4
(and a blank line here)

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 528
  • Karma: +5/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #3 on: July 19, 2011, 01:13:40 PM »
If that's the output of the code you have posted, then surely the "8C8FDBCB-0984-49D8-BE14-1942E26600C4" is being done by "echo $row['Parent'];"

To make sure that the echo is being called try changing that line to say:
PHP Code: [Select]
echo '['.$row['Parent'].']';

Oh and I'm assuming of course that the die() functions didn't get called and that no errors were displayed after adding error_reporting?

five

  • New Member
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #4 on: July 19, 2011, 01:35:38 PM »
Oh oops, i accidentally deleted a line from the previous post.. so that output is from the echo which is just $g. I printed it previously to help myself debug and see if it was even getting the right variable.

PHP Code: [Select]
echo $g

five

  • New Member
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #5 on: July 19, 2011, 01:38:57 PM »

To make sure that the echo is being called try changing that line to say:
PHP Code: [Select]
echo '['.$row['Parent'].']';

Oh and I'm assuming of course that the die() functions didn't get called and that no errors were displayed after adding error_reporting?

Changed the line. still no luck. And yeah the die() functions were not called and no errors are displayed.  :(

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 528
  • Karma: +5/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #6 on: July 19, 2011, 01:47:57 PM »
By still no luck I will assume that you mean that there is no output.

Firstly, try changing the sql to just "SELECT * FROM `Parent_Child`" (and make sure that this table name is EXACTLY the same as it appears in phpmyadmin - same capitals etc)

Secondly, have you ever managed to get an SQL query running on the server you are using?

five

  • New Member
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #7 on: July 19, 2011, 02:19:07 PM »
Right, still no output.

I did change the SQL statement to "Select * FROM `Parent_Child`", and it did return all of the Parent/Child ID's from the database. I'm sure that I checked on the case sensitivity of them.

And yeah, I'm able to run other SQL queries which makes me believe that something is wrong with my syntax in $sql.

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 528
  • Karma: +5/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #8 on: July 19, 2011, 02:23:32 PM »
In which case it might have been the use of ` quotes or maybe the fact that the WHERE clause was left out. As a test try using the same code you started with but use the ` quotes:
PHP Code: [Select]
$sql "SELECT * FROM `Parent_Child` WHERE `Child` = '".$g."'";

five

  • New Member
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #9 on: July 19, 2011, 02:28:12 PM »
In which case it might have been the use of ` quotes or maybe the fact that the WHERE clause was left out. As a test try using the same code you started with but use the ` quotes:
PHP Code: [Select]
$sql "SELECT * FROM `Parent_Child` WHERE `Child` = '".$g."'";

Still no output  :o

I also tried to remove the usage of ` around Child and from Parent_Child and one with `` and the other without. Unfortunately, still no output.

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 528
  • Karma: +5/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #10 on: July 19, 2011, 02:50:00 PM »
And there is DEFINITELY a row in the table `Parent_Child` where the field `Child` is EXACTLY equal to '8C8FDBCB-0984-49D8-BE14-1942E26600C4' ?

This is very strange that this is not working...

Once you have checked that the row I mentioned does exist, and it does, try changing the query to say:
PHP Code: [Select]
$sql "SELECT * FROM `Parent_Child` WHERE `Child` LIKE '%8C8FDBCB%'";

And in the loop do:
PHP Code: [Select]
echo $row['Parent'];
echo 
'<br />';
echo 
$row['Child'];

five

  • New Member
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #11 on: July 19, 2011, 02:54:12 PM »
Once you have checked that the row I mentioned does exist, and it does, try changing the query to say:
PHP Code: [Select]
$sql "SELECT * FROM `Parent_Child` WHERE `Child` LIKE '%8C8FDBCB%'";

And in the loop do:
PHP Code: [Select]
echo $row['Parent'];
echo 
'<br />';
echo 
$row['Child'];


Changed the query to what you suggested and it gave me the Parent and Child.
PHP Code: [Select]

33BD0276
-50E2-4F6D-A5DD-0B588D1DD33F
8C8FDBCB
-0984-49D8-BE14-1942E26600C4


I also tried taking what you did using LIKE and replacing the = with it, and it gave me no output.
PHP Code: [Select]

$sql 
"SELECT * FROM `Parent_Child` WHERE Child LIKE '".$g."'";

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 528
  • Karma: +5/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #12 on: July 19, 2011, 03:06:41 PM »
Okay, I think I know the solution then... fingers crossed!
PHP Code: [Select]
$sql "SELECT * FROM `Parent_Child` WHERE `Child` LIKE '%".$g."%'";

Try that and let me know!

five

  • New Member
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #13 on: July 19, 2011, 03:09:13 PM »
Nope :( didn't work.

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 528
  • Karma: +5/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #14 on: July 19, 2011, 03:14:23 PM »
Damn, okay...
PHP Code: [Select]
$sql "SELECT * FROM `Parent_Child` WHERE `Child` LIKE '%".trim(strtoupper($g))."%'";