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

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 #30 on: July 20, 2011, 05:07:57 PM »
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 Code: [Select]
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'].'" <br /> is the parent of <br /> "'.$row['Child'].'" <br /> ('.$g.')';
	
	
}
	
	
$result->close();
	
	
$con->close();
	
}


Try it and let's hope it works!!

five

  • New Member
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #31 on: July 21, 2011, 08:35:16 AM »
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 Code: [Select]
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;
 
	
}


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!




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 #32 on: July 21, 2011, 03:12:10 PM »
Aww, I'm assuming the mysqli method didn't work then. But good to hear you've got it working nonetheless  :D.

kikesv

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 122
  • Karma: +2/-0
  • Programmer and web designer
    • View Profile
Re: Query works in phpMyAdmin but doesn't when using php code
« Reply #33 on: July 22, 2011, 03:33:00 AM »
First of all sorry about my english.

Try to obtain all data
PHP Code: [Select]
$sql "SELECT * FROM Parent_Child";
$res mysql_query($sql);
echo
" BEGIN<br>";
while(
$rowmysql_fetch_array($res)){
     
print_r($row);
}
echo 
"END<br>";


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.