Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,894 questions

51,825 answers

573 users

How to bind variables to a prepared statement with bind_result() using MySQLi in PHP

1 Answer

0 votes
$db_host        = 'localhost';
$db_user        = 'root';
$db_password    = '';
$db_name        = 'allonpage';
 
$con = new mysqli('localhost', $db_user, $db_password, $db_name);
 
if ($con->connect_error) 
    die('Connection Error: ' . $con->server_info);
 
$sql = "
  SELECT src, alt
  FROM images
  WHERE alt LIKE ?
  AND add_date BETWEEN ? AND ?
";    
  
// i = variable type integer
// d = variable type double
// s = variable type string
// b = variable type blob 
     
if ($stmt = $con->prepare($sql)) 
{
    $alt = 'w%';
    $date1 = '2016-07-01';
    $date2 = '2016-07-31';
    $stmt->bind_param('sss', $alt, $date1, $date2); // 'sss' = 3 variables type string
 
    $stmt->execute();
    if ($stmt->errno) 
       die("Error: " . $stmt->error);
  
    // mysqli_stmt::bind_result - mysqli_stmt_bind_result
    // binds columns in the result set to variables
    $stmt->bind_result($src, $alt);   
        
    while ($stmt->fetch()) 
    {
        echo $src . " - " . $alt . "<br />";
    }
 
    $stmt->close();
}
                                  
$con->close();
 
 
/*
run: 
 
http://www.ws.com/images/logo-wordpress.png - wordpress logo   
http://www.ws.com/images/why-choose.png - why choose us
 
*/

 



answered Jul 11, 2016 by avibootz
...