Jump to content


Photo

Occasional Php Warning When Looping Multiple Mysqli Queries

PHP MySQLi Query Multiple Warning

  • Please log in to reply
1 reply to this topic

#1 Johnny2

Johnny2

    Member

  • Member
  • PipPipPip
  • 53 posts
  • LocationIdaho

Posted 07 September 2015 - 06:28 PM

My PHP script for mysqli queries works most of the time, but while running a loop that queries database many times, occasionally I'll get Warning:

PHP Warning:  mysqli_stmt::execute(): (HY000/2013): Lost connection to MySQL server during query in... blah blah blah

Can someone tell me if I need to modify the structure of my queries to make it work better?  Here's my example:

 

 

<?php

 

$host = "localhost";
$username = "blah_blah";
$password = "blah_blah";
$database = "blah_blah";

$mysqli = new mysqli($host, $username, $password, $database,0,'/var/lib/mysql/mysql.sock'); 
$mysqli->set_charset('utf8');

 

if ($stmt = $mysqli->prepare("SELECT column_A FROM table_X WHERE column_B=? AND column_C=? LIMIT 1")){
 $stmt->bind_param("si", $value_1, $value_2);
 $stmt->execute();
 $stmt->bind_result($column_A_value);
 $stmt->fetch();
 $stmt->close();
}

 

?>


  • 0

#2 Stefan

Stefan

    Stefan Mischook

  • Administrators
  • 4,337 posts
  • LocationMontreal Canada

Posted 06 October 2015 - 10:24 AM

Hi,

 

I would wrap the query attempt in a try/catch so you can at least display a friendly message if it fails to connect to the database. Check out:

 

http://php.net/manua...codes.catch.php

Stef


  • 0
StudioWeb makes teaching web design and programming easy: StudioWeb





Also tagged with one or more of these keywords: PHP, MySQLi, Query, Multiple, Warning

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

★★★★★ 5 Star Rated Web Developer Course - check it out now!