Problem with mysql_fetch_assoc

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Problem with mysql_fetch_assoc

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with mysql_fetch_assoc

Post by requinix »

The big difference between those two bits of code is that the working one includes a call to mysql_query...
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Problem with mysql_fetch_assoc

Post 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>';
        }
       }
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Problem with mysql_fetch_assoc

Post by jotae »

Wow!!! Thanks very much jonah bron. Working super nice!!! Regards and thx again...
Post Reply