Page 1 of 1

Problem with mysql_fetch_assoc

Posted: Fri Jul 02, 2010 6:04 pm
by jotae
Please help me:

This code have problems:

Code: Select all

$con = mysql_connect("localhost", "root", "")or die('MySQL Connect Error: '.mysql_error("No hay conexión a la Base de Datos"));
       mysql_select_db("midis",$con);
         
       $result = 'select * from temas where CLASE like \'%'.$tipomus.'%\' order by ' . $sort;
       while($row=mysql_fetch_assoc($result)){ 
       //while ($row=mysql_fetch_array($result)){
        echo '<tr><td>'.$row["clase"].'</td>';
        echo '<td>'.$row["tema"].'</td>';
        echo '<td>'.$row["ritmo"].'</td>';
        echo '<td>'.$row["autor"].'</td>';
        echo '<td>'.$row["arreglo"].'</td>';
        echo '<td>'.substr($row["fecha"],-12,4).'</td>';
        if ($row["nuevo"]=='Y'){
         echo '<td>'."Nuevo".'</td></tr>';
        }else{
         echo '<td>'."".'</td></tr>';
        }
       }
$tipomus is a string variable.
The error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\SML\to.php on line 117 and don't read the records. Is possible a sintax error?

Whith this simple line, work ok: $result=mysql_query("select * from temas order by tema");

Thx for your help. Is urgent for me.

Re: Problem with mysql_fetch_assoc

Posted: Fri Jul 02, 2010 6:16 pm
by requinix
The big difference between those two bits of code is that the working one includes a call to mysql_query...

Re: Problem with mysql_fetch_assoc

Posted: Fri Jul 02, 2010 6:23 pm
by Jonah Bron
Just to clarify what tasairis said, you code needs to be changed to this:

Code: Select all

$con = mysql_connect("localhost", "root", "")or die('MySQL Connect Error: '.mysql_error("No hay conexión a la Base de Datos"));
       mysql_select_db("midis",$con);
         
       $result = mysql_query('select * from temas where CLASE like \'%'.$tipomus.'%\' order by ' . $sort, $con); // notice the change in this line
       while($row=mysql_fetch_assoc($result)){ 
       //while ($row=mysql_fetch_array($result)){
        echo '<tr><td>'.$row["clase"].'</td>';
        echo '<td>'.$row["tema"].'</td>';
        echo '<td>'.$row["ritmo"].'</td>';
        echo '<td>'.$row["autor"].'</td>';
        echo '<td>'.$row["arreglo"].'</td>';
        echo '<td>'.substr($row["fecha"],-12,4).'</td>';
        if ($row["nuevo"]=='Y'){
         echo '<td>'."Nuevo".'</td></tr>';
        }else{
         echo '<td>'."".'</td></tr>';
        }
       }

Re: Problem with mysql_fetch_assoc

Posted: Fri Jul 02, 2010 6:53 pm
by jotae
Wow!!! Thanks very much jonah bron. Working super nice!!! Regards and thx again...