Searching for data from an ID in DB

Asked

Viewed 64 times

0

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

What I am trying to do is create a "related" but without it displays the same post or video in the 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; ?>
  • 1

    It was not clear to me the description of the problem. Important to understand that the mysql_* functions are obsolete.

  • @Marcosxavier, for example, I’m accessing a post, and the footer has a part where I display the related posts. In this field, I wanted to know how to search without the same post (which is open on the page). In a matter of mysql_* what method is being used now? , I’m learning php from some old video lessons.

  • 1

    Use Pdo instead of mysql_*. About the query, you could store the id of the post displayed in a variable and do something like "select * table Where idPostagem != idPostagemAberta"

1 answer

1


For your code to work we will make some changes. But first let’s theory.

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

I created a separate database connection file. So centralize the connection to the database in only one place of your project and avoid rework.

include "conexao.php";

As you have not posted anything of how the connection is being made, I will put as it is done 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 bank check if there were no 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 so:

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

To go through the query you can do using a repeat loop, exactly 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 $user['name'] array it is simple quotes ('), rather than crases (``).

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

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

Any doubt just comment.

Browser other questions tagged

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