Problema ciudata mysql

Întrebări legate de conectarea la baze de date MySQL prin intermediul PHP

Moderatori: Zamolxe, Moderatori

DeNeDe
Junior Member
Mesaje: 33
Membru din: Joi Apr 14, 2011 10:37 pm

Problema ciudata mysql

Mesajde DeNeDe » Vin Feb 21, 2014 1:42 am

Azi am incercat sa modific unele lucruri intr-un script, modificari care le aveam intr-un alt script mai vechi care stiu sigur ca functioneaza. Totul bine si frumos, am facut modificarile in cauza si bum. Cand incerc sa intru pe pagina respectiva imi spune ca

Cod: Selectaţi tot

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY torrents.sticky ASC, torrents.id DESC' at line 1


Inteleg ca poate codul ar fi fost vechi, dar chiar asa de vechi incat sa nu-mi mearga? Si totusi, eroarea imi zice ca e langa linia 1, dar eu nu am umblat niciodata prin partea aceea.

Cod: Selectaţi tot

$HTMLOUT = '';
   
    $cats = genrelist_albums();

        $searchstr = (isset($_GET['search']) ? unesc($_GET['search']) : '');
    $cleansearchstr = searchfield($searchstr);
    if (empty($cleansearchstr))
        unset($cleansearchstr);
    if (isset($_GET['sort']) && isset($_GET['type'])) {
        $column = '';
        $ascdesc = '';
     
        switch ($_GET['sort']) {
            case '1': $column = "name";
                break;
            case '2': $column = "numfiles";
                break;
            case '3': $column = "comments";
                break;
            case '4': $column = "added";
                break;
            case '5': $column = "size";
                break;
            case '6': $column = "times_completed";
                break;
            case '7': $column = "seeders";
                break;
            case '8': $column = "leechers";
                break;
            case '9': $column = "owner";
                break;
            default: $column = "id";
                break;
        }
     
        switch ($_GET['type']) {
            case 'asc': $ascdesc = "ASC";
                $linkascdesc = "asc";
                break;
            case 'desc': $ascdesc = "DESC";
                $linkascdesc = "desc";
                break;
            default: $ascdesc = "DESC";
                $linkascdesc = "desc";
                break;
        }
     
        $orderby = "ORDER BY torrents." . $column . " " . $ascdesc;
        $pagerlink = "sort=" . intval($_GET['sort']) . "&type=" . $linkascdesc . "&";
        } else {
        $orderby = "ORDER BY torrents.sticky ASC, torrents.id DESC";
        $pagerlink = "";
        }

    $addparam = "";
    $wherea = array();
    $wherecatina = array();

    if (isset($_GET["incldead"]) &&  $_GET["incldead"] == 1)
    {
      $addparam .= "incldead=1&";
      if (!isset($CURUSER) || get_user_class() < UC_ADMINISTRATOR)
        $wherea[] = "banned != 'yes'";
    }
    else
    {
      if (isset($_GET["incldead"]) && $_GET["incldead"] == 2)
      {
      $addparam .= "incldead=2&amp;";
        $wherea[] = "visible = 'no'";
      }
      else
        $wherea[] = "visible = 'yes'";
      if ($CURUSER["view_xxx"] != "yes")
        $wherea[] = "category != '84'";
    }
   
    $category = (isset($_GET["cat"])) ? (int)$_GET["cat"] : false;

    $all = isset($_GET["all"]) ? $_GET["all"] : false;

    if (!$all)
    {
      if (!$_GET && $CURUSER["notifs"])
      {
        $all = True;
        foreach ($cats as $cat)
        {
          $all &= $cat['id'];
          if (strpos($CURUSER["notifs"], "[cat" . $cat['id'] . "]") !== False)
          {
            $wherecatina[] = $cat['id'];
            $addparam .= "c{$cat['id']}=1&amp;";
          }
        }
      }
      elseif ($category)
      {
        if (!is_valid_id($category))
          stderr("{$lang['browse_error']}", "{$lang['browse_invalid_cat']}");
        $wherecatina[] = $category;
        $addparam .= "cat=$category&amp;";
      }
      else
      {
        $all = True;
        foreach ($cats as $cat)
        {
          $all &= isset($_GET["c{$cat['id']}"]);
          if (isset($_GET["c{$cat['id']}"]))
          {
            $wherecatina[] = $cat['id'];
            $addparam .= "c{$cat['id']}=1&amp;";
          }
        }
      }
    }
   
    if ($all)
    {
      $wherecatina = array();
      $addparam = "";
    }

    if (count($wherecatina) > 1)
      $wherecatin = implode(",",$wherecatina);
    elseif (count($wherecatina) == 1)
      $wherea[] = "category = $wherecatina[0]";

    $wherebase = $wherea;

    if (isset($cleansearchstr))
    {
      $wherea[] = "MATCH (search_text, ori_descr) AGAINST (" . sqlesc($searchstr) . ")";
      //$wherea[] = "0";
      $addparam .= "search=" . urlencode($searchstr) . "&amp;";
      $orderby = "";
     
    }
   
   $where = implode(" AND ", $wherea);
   
    if (isset($wherecatin))
      $where .= ($where ? " AND " : "") . "category IN(" . $wherecatin . ")";

    if ($where != "")
      $where = "WHERE $where";
    
   if($where == "")
   {
      $where = "WHERE ";
   }
   else
   {
      $where .=" AND ";
   }

    $res = mysql_query("SELECT COUNT(*) FROM torrents $where category NOT IN (85, 86, 87, 88, 91, 92, 93 ,94, 95, 96) ") or die(mysql_error());
    $row = mysql_fetch_array($res,MYSQL_NUM);
    $count = $row[0];

    if (!$count && isset($cleansearchstr))
    {
      $wherea = $wherebase;
      $orderby = "ORDER BY id DESC";
      $searcha = explode(" ", $cleansearchstr);
      $sc = 0;
      foreach ($searcha as $searchss)
      {
        if (strlen($searchss) <= 1)
          continue;
        $sc++;
        if ($sc > 5)
          break;
        $ssa = array();
        foreach (array("search_text", "ori_descr") as $sss)
          $ssa[] = "$sss LIKE '%" . sqlwildcardesc($searchss) . "%'";
        $wherea[] = "(" . implode(" OR ", $ssa) . ")";
      }
 
   if ($sc)
      {
        $where = implode(" AND ", $wherea);
        if ($where != "")
          $where = "WHERE $where AND";
      $q = "SELECT COUNT(*) FROM torrents $where category NOT IN (85, 86, 87, 88, 91, 92, 93 ,94, 95, 96)";
      $res = mysql_query($q);
        $row = mysql_fetch_array($res);
        $count = $row[0];
      }
    }


Ceea ce am adaugat este

Cod: Selectaţi tot

if($where == "")
   {
      $where = "WHERE ";
   }
   else
   {
      $where .=" AND ";
   }

    $res = mysql_query("SELECT COUNT(*) FROM torrents $where category NOT IN (85, 86, 87, 88, 91, 92, 93 ,94, 95, 96) ") or die(mysql_error());
si

Cod: Selectaţi tot

$where = "WHERE $where AND";
      $q = "SELECT COUNT(*) FROM torrents $where category NOT IN (85, 86, 87, 88, 91, 92, 93 ,94, 95, 96)";
      $res = mysql_query($q);
        $row = mysql_fetch_array($res);

Ideea e ca inainte sa adaug bucatile de cod, totul mergea bine. Avand in vedere ca e vorba de mysql cred ca problema ar veni de la mysql count ?



Avatar utilizator
vectorialpx
Senior Member
Mesaje: 4832
Membru din: Mar Mar 01, 2005 9:48 am
Localitate: Bucuresti
Contact:

Re: Problema ciudata mysql

Mesajde vectorialpx » Vin Feb 21, 2014 10:35 am

E foarte simplu sa-ti faci debug.
Iei SQL-ul si-l muti intr-o variabila si apoi faci un echo.

Cod: Selectaţi tot

// ....
$SQL = "SELECT COUNT(*) FROM torrents $where category NOT IN (85, 86, 87, 88, 91, 92, 93 ,94, 95, 96) ";
die(
 $SQL );
$res = mysql_query($SQL) or die(mysql_error());
 

Revii pe forum cu acel SQL sau te prinzi tu care este eroarea.
E normal ca eroarea sa fie pe lina 1, pentru ca tu ai tot SQL-ul pe o linie. Formateaza si tu, ca sa nu mai ai aceste dubii.

Cod: Selectaţi tot

SELECT COUNT(*)
FROM torrents
$where
category NOT IN (85, 86, 87, 88, 91, 92, 93 ,94, 95, 96)

DeNeDe
Junior Member
Mesaje: 33
Membru din: Joi Apr 14, 2011 10:37 pm

Re: Problema ciudata mysql

Mesajde DeNeDe » Vin Feb 21, 2014 12:22 pm

M-ai cam bagat in ceata... :D

Cod: Selectaţi tot

SELECT COUNT(*) FROM torrents WHERE category NOT IN (85, 86, 87, 88, 91, 92, 93 ,94, 95, 96)


Asta imi da.. si tot ce vad diferit ar fi WHERE in loc de $where.. dar $where ar fi definit ca WHERE.

Cu "WHERE" lucrurile par sa functioneze cat de cat, dar nu cum ar trebui. Probabil din cauza ca as avea nevoie de "$where"
Codul de baza de la care am pornit era

Cod: Selectaţi tot

$res = mysql_query("SELECT COUNT(*) FROM torrents $where") or die(mysql_error());

Avatar utilizator
vectorialpx
Senior Member
Mesaje: 4832
Membru din: Mar Mar 01, 2005 9:48 am
Localitate: Bucuresti
Contact:

Re: Problema ciudata mysql

Mesajde vectorialpx » Vin Feb 21, 2014 5:44 pm

L-ai frecat pe acel $where ($wherea, whatever) incat nu mai stii ce trebuie sa primesti :)
Fa-i un echo si vezi ce iti vine acolo. Din pacate nu pot sa refac tot scenariul tau.

In principiu, acel where ar trebui sa functioneze cam asa:

Cod: Selectaţi tot

<?php
// ...
$where = array();
$order = array();

if(
 stuff ) {
    $where[] = 'IdProduct IN ( 12, 12 )';
    $order[] = 'IdClient';
}
if(
 another_stuff ) {
    $where[] = 'IdCategory = 3';
}

$SQL = '
SELECT *
FROM `tab`
'
 . ( !empty($where) ? ' WHERE '.implode(' AND ', $where) : '' )
 . ( !empty($order) ? ' ORDER BY '.implode(', ', $order) : '' ) ;

 

moof2010
Average Member
Mesaje: 55
Membru din: Sâm Aug 17, 2013 10:54 pm

Re: Problema ciudata mysql

Mesajde moof2010 » Sâm Feb 22, 2014 12:08 pm

in sql e gresit $where... pune si tu ".$where." ca el in interogare executa $where care nu are sens in loc de continutul lui $where

DeNeDe
Junior Member
Mesaje: 33
Membru din: Joi Apr 14, 2011 10:37 pm

Re: Problema ciudata mysql

Mesajde DeNeDe » Sâm Feb 22, 2014 1:29 pm

Am pus asa, si degeaba.


Înapoi la “MySQL”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 23 vizitatori