D
D
dima12s2017-02-09 22:42:42
PHP
dima12s, 2017-02-09 22:42:42

Why duplicate SQL queries?

$result= $db->query("SELECT id,xfields,title,alt_name FROM `post` WHERE `title` = '".$db->safesql($value['name'])."'");
if ($db->num_rows($result)) {
while ($row_actor = $db->get_row($result)) {
$id_actor = $row_actor['id'];
$xf_actor = $row_actor['xfields'];
$alt_name_actor = $row_actor['alt_name'];
}
}
The "Name" field is displayed - $row_actor['xfields']; and "Picture". $row_actor['alt_name'];
If there is news, then the id is displayed as needed - Title + picture, and if there is no news, the Title + picture from the previous news is duplicated
I use Dle engine. How can I make the title + image appear only when there is an entry in the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2017-02-09
@ichubinets

I didn’t use DLE, but here, it seems, there’s nothing difficult, just put a condition if there are more than 0 records

if ($db->num_rows($result) > 0) 
{
    while ($row_actor = $db->get_row($result)) 
    {
        $id_actor = $row_actor['id'];
        $xf_actor = $row_actor['xfields'];
        $alt_name_actor = $row_actor['alt_name'];
    }
}

The best option would be
if ($rows = $db->get_row($result)) 
{
    foreach($rows as $row_actor) 
    {
        $id_actor = $row_actor['id'];
        $xf_actor = $row_actor['xfields'];
        $alt_name_actor = $row_actor['alt_name'];
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question