How to call two mysql tables in the same php page?

Asked

Viewed 1,253 times

0

I’m new to programming for the web, and I’m having a hard time with this. I’m developing an advertising site and I need to call more than one table on the page.

I made 3 Div’s advertising: Main, Free and General.

I can only show the advertisements of the main, the free and general I can not.

As I’m calling the principal:

<?php
while ($dados=mysql_fetch_array($res))
{
?>

<?php echo $dados['site']?> <br>
<?php echo $dados['descricao']?> <br>
<img src="Painel/fotos/<?php echo $dados['foto']?> /><br>

<?php }?>

I’m trying to do the same thing for the other two divs and I can’t help myself?

  • 2

    Displays the example with two and with the SQL query.

  • Explain what "I can’t" is. Is it wrong? Which one? An important detail, especially for you who are a beginner, is that mysql_* functions have been obsolete since PHP 5.5. Use Mysqli or PDO. See more here: http://www.ultimatephp.com.br/php-por-que-nao-utilizar-funcoes-mysql

  • To 'combine' tables in sql you need a join or union.

  • I’m taking the data from the group like this: <?php include ("connected.php"); $sql="select * from main"; $res= mysql_query ($sql); ? > .

  • Try it with $sql1, $sql2 and $sql3 and $res1 $res2 and $res3.

  • Blz, I’ll try, thank you

Show 1 more comment

1 answer

1

Just copy this code to the other div, with a mysql_query and a mysql_fetch_array for each one. With mysql_query you got the data for this right ? then.. get the data for each table in the other Ivs

Edit:

that is to say:

<?php 
    include ("conexao.php"); 

    $sql="select * from principal"; $res= mysql_query ($sql); 
    while ($dados=mysql_fetch_array($res))
    {
        echo $dados['site'] . "<br>";
        echo $dados['descricao'] . "<br>";
?>
<img src="Painel/fotos/<?php echo $dados['foto']?> /><br>

<?php } ?>
    <br><br>
<?php 
    $sql="select * from gratuitas"; $res= mysql_query ($sql);
    while ($dados=mysql_fetch_array($res))
    {
        echo $dados['site'] . "<br>";
        echo $dados['descricao'] . "<br>";
        ?>
    <img src="Painel/fotos/<?php echo $dados['foto']?> /><br>

<?php } ?>
    <br><br>
<?php 

    $sql="select * from geral"; $res= mysql_query ($sql); 
    while ($dados=mysql_fetch_array($res))
    {
        echo $dados['site'] . "<br>";
        echo $dados['descricao'] . "<br>";
    ?>
    <img src="Painel/fotos/<?php echo $dados['foto']?> /><br>

<?php }
?>

I have not tested this code, there may be some syntax error. Correct if there is. The code above is to give an idea of what I mean.

  • You can be more specific?

  • has... show the code where you do the mysql_query and I add

  • <?php include ("connection.php"); $sql="select * from main"; $res= mysql_query ($sql); ?>

  • Okay I’ll test, and I’ll see

  • 1

    It worked, really vlw.

  • So if you can accept the answer, help :)

  • I made it simple like this, because you said yourself that you were beginners in web programming. This code can be written in MUCH better ways. Try to improve it after you have more experience.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.