Abo Hani Abo Hani - 1 year ago 58
PHP Question

Commands out of sync in A correct code

I've the following query executed via PHP, it used to work like yesterday, and stopped working today, here's it :

SELECT * , (SELECT IF (pro.uid= '4342','True','False') FROM pro) AS 'uid' FROM Episodes,pro ORDER BY Episodes.id DESC LIMIT 9

the '4342' is changed with GET param, i'm having the following error :

Commands out of sync; you can't run this command now

My PHP API is as the following ( snippets ) :

include_once "db_config.php";

DEFINE ('DB_HOST', $host);
DEFINE ('DB_USER', $user);
DEFINE ('DB_NAME', $database);

$mysqli = @($GLOBALS["___mysqli_ston"] = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD)) OR die ('Could not connect to MySQL');
@((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . constant('DB_NAME'))) OR die ('Could not select the database');



if(isset($_GET['meaE'], $_GET['uid']))
$query="SELECT * , (SELECT IF (pro.uid= '".$_GET['uid']."','True','False') FROM pro ) AS 'uid' FROM Episodes,pro ORDER BY Episodes.id DESC LIMIT 9
$resouter = mysqli_query($GLOBALS["___mysqli_ston"], $query);


$set = array();

$total_records = mysqli_num_rows($resouter);
if($total_records >= 1){

while ($link = mysqli_fetch_array($resouter, MYSQLI_ASSOC)){

$set['APITest'][] = $link;

echo $val= str_replace('\\/', '/', json_encode($set));


Answer Source

Your query seems wrong, I don't know how it worked yesterday, but right now your are cross joining with out a condition (could be correct but I doubt it) - that means each record in episodes will be joined to each record in pro . In addition, your subquery with the IF will return more then 1 record unless this table contains only 1 record (which again - I doubt it) .

Try this:

SELECT t.*,s.* , IF (s.uid= '4342','True','False')  AS 'uid'
FROM Episodes t
 ON(t.CommonColumn = s.CommonColumn) --Like an ID column
LIMIT ?????