문제

I am having a while loop in my function for getting the values 1 by 1 but i am getting the only 1 value

while($row=  mysqli_fetch_array($this->result))
    {
        $image=$this->getEventDetails($row['Album_top'],'Event_image');
        $alert.="<div class=facBox>
        <a href='gallery.php?id=$row[Id]' style=margin-top:0px;>
        <img src='admin/customer/eventgallery/$image' alt=''>
        </a>
        <div class=clear></div>
        <a href='gallery.php?id=$row[Id]' style=margin-top:0px;>$row[Album_title]</a>
                </div>";
    }

I am getting the image name from another function that is

    function getEventDetails($id,$fieldname)
{
    $get="Select * from sarnaevent where Id='$id'";
    $this->result=mysqli_query($this->con,$get);
    $row=mysqli_fetch_array($this->result);
    return $row[$fieldname];
}

Now i am getting the only value from the loop my $alert is having only one facbox. if i remove this line

$this->getEventDetails($row['Album_top'],'Event_image');

It works fine but i want this line to get the image name.

도움이 되었습니까?

해결책

Inside getEventDetails(), you assign $this->result, overwriting the previous contents of $this->result. Since that occurs inside the while loop where the previous result is being used, the loop exits because there are no further results to retrieve from the inner fetch.

User a different temporary result set inside teh getEventDetails() method.

function getEventDetails($id,$fieldname)
{
    $get="Select * from sarnaevent where Id='$id'";
    // Different variable name...
    $temporary_result = mysqli_query($this->con,$get);
    $row=mysqli_fetch_array($temporary_result);
    return $row[$fieldname];
}

In general, I would question the need to be storing a transient result resource into the object's $this->result for most purposes. In any case where you're using that inside a method, you are probably better off using a variable scoped only to the method, which lives only for the lifetime that result set is being used.

Please use caution when sending $id directly into the query. Although I suspect it is known to be an integer variable, and therefore it won't break the SQL, it's a good idea to get into the habit of using prepare()/execute() to prevent SQL injection.

One final point of caution: When placing the string variable into your HTML markup, be sure to use htmlspecialchars() to escape it against malforming the HTML. (If the Id is known to be an integer, it isn't necessary there)

"...<a href='gallery.php?id=$row[Id]' style=margin-top:0px;>" . htmlspecialchars($row['Album_title']) . "</a>..."

다른 팁

Instead of using mysqli_fetch_array() try using mysqli_fetch_assoc (see: http://php.net/manual/en/mysqli-result.fetch-assoc.php); the returned array will be associative (key-value pairs) where the keys are the column names.

You're blowing out your original query.

First, you do a query, and assign its result to $this->result. You then iterate across those results. For the first row, you immediately make a new query, overwrite $this->result with the new results, and then try to continue... but you've just replaced the rest of your results with the single result from the event details query, so mysqli_fetch_array doesn't find any more results.

Solution: use a separate variable for that result. A local one should be fine, since you don't use it outside that function. You may also need to use a separate connection; I'm not sure of that. Try it with just changing $this->result in getEventDetails() to use something like $eventResult instead.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top