Jump to content

Recommended Posts

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 ;-)

 

Link to comment
https://forums.phpfreaks.com/topic/297525-mysql-query-inside-while/
Share on other sites

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.

  • Like 1

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.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.