Jump to content

Having problems with MySQLi SELECT


Go to solution Solved by Barand,

Recommended Posts

I'm not really sure what's going on, but I have been replacing all of my mysql queries with mysqli, and so far it is working fine, until I got to this one in particular...

 

I am getting this error... "Warning: mysqli::prepare(): Couldn't fetch mysqli"  and this error... "Warning: mysqli::close(): Couldn't fetch mysqli"

 

I researched that problem, but none of what others had to say about fixing it, applied nor worked for me.

 

What's funny about it is that just above that query, is another query in which has no problems...

 

Here's the top query that works fine:

$sql = "SELECT * FROM Agencies ORDER BY AgencyName ASC";
if ($result = $mysqli -> query($sql))
{
    while ($row = $result -> fetch_assoc())
    {
        echo '<option value="'.$row['AgencyID'].'">'.$row['AgencyName'].'</option>';
    }
    $result -> free();
}
$mysqli -> close();

Now here's the query that is throwing the errors:

$sql = "SELECT AgenciesAgents.*, Agents.AgentFirstName, Agents.AgentLastName
    FROM AgenciesAgents 
    LEFT JOIN Agents ON AgenciesAgents.AgentID = Agents.AgentID
    WHERE AgenciesAgents.AgencyID = ?
    ";
if ($stmt = $mysqli -> prepare($sql))
{
    $stmt->bind_param("i", $AgencyID);
    $stmt->execute();
    while ($row = $stmt -> fetch_assoc())
    {
        echo '<p><span style="vertical-align: top; color: #515151;">'.$row['AgentLastName'].', '.$row['AgentFirstName'].'</span><a href="agency-agent-correlations.php?function=deletecorrelation&agencyid='.$AgencyID.'&agentid='.$row['AgentID'].'" onclick="return confirm(\'Are you certain you wish do delete this agent?\');"> <img src="images/delete_icon.png" width="20" height="20" /></a></p>';
    }
    $stmt -> free();
}
$mysqli -> close();
Link to comment
https://forums.phpfreaks.com/topic/283449-having-problems-with-mysqli-select/
Share on other sites

So he does - missed that.

 

Only open the connection once at top of script and pass the connection to any functions that need it.

 

It will be closed automatically at end of script. The only time you need to open mid-script is if you change to another server.

and since you are closing the mysqli connection after the first block of code, it is not available later in the program.

 

You know, I'm glad you brought that up, because I was thinking the same thing, so how do you go ahead and open it back up, because in the connection file is where it is originally instantiated...

So he does - missed that.

 

Only open the connection once at top of script and pass the connection to any functions that need it.

 

It will be closed automatically at end of script. The only time you need to open mid-script is if you change to another server.

 

So what you're saying is, don't close the connection until the end of the page, after all of the queries on that page are completed?

Stmt objects use the fetch method. The result objects (as in your first query) use fetch_assoc.

 

For the life of me, I can't seem to figure out the correct syntax, to make this code work...

$sql = "SELECT AgenciesAgents.AgentID, Agents.AgentFirstName, Agents.AgentLastName
    FROM AgenciesAgents 
    LEFT JOIN Agents ON AgenciesAgents.AgentID = Agents.AgentID
    WHERE AgenciesAgents.AgencyID = ?
    ";
if ($stmt = $mysqli -> prepare($sql))
{
    $stmt->bind_param("i", $AgencyID);
    $stmt->execute();
    while ($row = $stmt -> fetch_assoc())
    {
        echo '<p><span style="vertical-align: top; color: #515151;">'.$row['AgentLastName'].', '.$row['AgentFirstName'].'</span><a href="agency-agent-correlations.php?function=deletecorrelation&agencyid='.$AgencyID.'&agentid='.$row['AgentID'].'" onclick="return confirm(\'Are you certain you wish do delete this agent?\');"> <img src="images/delete_icon.png" width="20" height="20" /></a></p>';
    }
    $stmt -> close();
}

Thanks, I thought about using fetch in that manner, but I couldn't comprehend how I could have multiple row result sets, the way it looks, but sure enough, as it loops, the variable changes each time...

 

Thanks for all of your help, it is much appreciated.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.