alespais Posted July 28, 2015 Share Posted July 28, 2015 Hello everyone, I hope you can help me with this problem. Here's the deal: I have this code: <?php $artic = $_GET['art']; include("config.php"); $result=$db->query("SELECT * FROM equiva WHERE ((codigo1 LIKE $artic) OR (codigo2 LIKE $artic))"); while ($row=$result->fetch_array(MYSQLI_ASSOC)){ if($artic == $row['codigo1']){ $codigoart = $row['codigo2'];} else {$codigoart = $row['codigo1'];} $resultados=$db->query("SELECT id_art,denom2,exist,marca FROM articulos WHERE id_art LIKE $codigoart"); $row2 = $resultados->fetch_array(MYSQLI_ASSOC); $mark = $row2['marca']; $cadena = $row2['id_art']; $res_marca = $db->query("SELECT id_marca,marca FROM marcas WHERE id_marca LIKE $mark"); $row1 = $res_marca->fetch_array(MYSQLI_ASSOC); ?> <tbody> <tr> <td valign="middle"><?php echo $codigoart;?> </td> <td valign="middle"><?php echo $row1['marca'];?> </td> <td valign="middle"><?php echo $row2['denom2'];?> </td> <td> <a target="_blank" href="verequiv.php?art=<?php echo $cadena;?>" ><img src="/images/articulos/<?php echo $nombre;?>.jpg"></a></td> </tr> </tbody> <?php I try to get from the equiv table (that has only 2 columns, codigo1 and codigo2) those records same to $artic and dump on an array. So far so good. Walking through this array, and compare values of the same with a query, apparently it only resolved the first pass, then no more. If I check if($resultados=$db->query("SELECT id_art,denom2,exist,marca FROM articulos WHERE id_art LIKE $codigoart")), the first time it is true but then is false. Please, any ideas is welcome because I am stuck here. thank you! PD: Obviously sorry for my English ;-) Quote Link to comment https://forums.phpfreaks.com/topic/297525-mysql-query-inside-while/ Share on other sites More sharing options...
iarp Posted July 29, 2015 Share Posted July 29, 2015 My suggestion would be to try running the queries manually within MySQL directly and seeing if the output is what you are expecting. None of those LIKE statements look correct to me at all. If anything I would try wrapping the variables you're using within the queries in single quotes, kind of like: $res_marca = $db->query("SELECT id_marca,marca FROM marcas WHERE id_marca LIKE '$mark'"); This also begs the question, are you sure you want LIKE conditions and not regular equals to? LIKE conditions typically also contain percent signs that allow you to search for a specific value that maybe within several other values in a single cell. 1 Quote Link to comment https://forums.phpfreaks.com/topic/297525-mysql-query-inside-while/#findComment-1517666 Share on other sites More sharing options...
maxxd Posted July 29, 2015 Share Posted July 29, 2015 Try joining the tables instead of running queries within a loop (which is typically a bad idea. The following is untested and hastily put together, but it looks like it could work for your purposes: SELECT a.id_art ,a.denom2 ,a.exist ,a.marca AS articulosMarca ,m.id_marca ,m.marca AS marcasMarca FROM equiva e LEFT JOIN articulous a ON (a.id_art = e.codigo1 OR a.id_art = e.codigo2) LEFT JOIN marcas m ON m.id_marca = a.marca WHERE ((e.codigo1 LIKE %$artic%) OR (e.codigo2 LIKE %$artic%)) Again, it's untested, but I think I covered your criteria. 1 Quote Link to comment https://forums.phpfreaks.com/topic/297525-mysql-query-inside-while/#findComment-1517672 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.