Searching for data from an ID in DB

0

I have researched here and google to see if it would solve my problem without having to ask a question, but I could not find any solution.

What I'm trying to do is create a "related" but without it displaying the same post or video in case. In case I am using the following code:

<?php
    if(!@mysql_connect("localhost","root",""))
    {
        die('oops connection problem ! --> '.mysql_error());
    }
    if(!mysql_select_db("player"))
    {
        die('oops database selection problem ! --> '.mysql_error());
    }
    $query = @mysql_query("SELECT * FROM videopv2 WHERE 'videopv2'.'tipo_relacao'= '$tipode' ORDER BY id DESC");
    while ($relacionados = @mysql_fetch_object($query)):
        ?>
    <article class="video-preview <?php echo $relacionados->categoria_video; ?>" data-viewkey="6b684234a3" itemscope="itemscope" itemtype="http://schema.org/ImageObject">
        <a title="<?php echo $relacionados->titulo_video; ?>" itemprop="url" target="_blank" href="/AP/embed/<?php echo $relacionados->id; ?>">
            <img class="media-preview-thumbnail js-resrc js-resrc-lazy" alt="<?php echo $relacionados->titulo_video ?>" itemprop="thumbnailUrl" src="/AP/assets/embed/imgs/<?php echo $relacionados->imagem_video; ?>" data-src="/AP/assets/embed/imgs/<?php echo $relacionados->imagem_video; ?>" />
            <h4 class="media-preview-title" itemprop="name"><?php echo $relacionados->titulo_video; ?></h4>
        </a>
    </article>
<?php endwhile; ?>
    
asked by anonymous 24.10.2016 / 13:19

1 answer

1

For your code to work let's make some changes. But first let's theory.

You are using the procedural form of mysqli_query and you are skipping a few steps. If you want to know more, there is the object-oriented form of mysqli :: query. I addressed the differences in how to use them here >.

I created a separate bank connection file. This centralizes the connection to the bank in one place of your project and avoids rework.

include "conexao.php";

As you have not posted anything about how the connection is being made, I'll put it as it is in procedural form: (Not that yours is wrong)

// Conecta ao banco de dados
mysql_connect('127.0.0.1', 'usuario', 'senha');
mysql_select_db('banco');

Tip: After connecting the database, check for errors:

/* valida conexão ao banco */
if (mysqli_connect_errno()) {
    printf("Falha na conexão ao banco: %s\n", mysqli_connect_error());
    exit();
}

To perform a query based on a query you can do this:

// Monta e executa uma consulta SQL
$sql = "SELECT *' FROM 'usuarios'";
$query = mysql_query($sql);

To walk through the query you can do using a loop repetition, just as you are doing:

// Para cada resultado encontrado...
while ($usuario = mysql_fetch_assoc($query)) {
  // Exibe um link com a notícia
  echo $usuario['nome'] . ' - ' . $usuario['email'];
} // fim while

Note that in the array $ user ['name'] is single quotation marks (''), instead of ciphers ('').

And a hint to display the total of lines coming in the query

// Total de notícias
echo 'Total de notícias: ' . mysql_num_rows($query);

Any questions just comment.

    
18.07.2017 / 14:01