How to bind one variable to a prepared statement as parameters using MySQLi in PHP

2 Answers

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);

if ($stmt = $con->prepare("SELECT src, alt FROM images WHERE alt LIKE ?")) 
{

    $alt = 'w%';
    $stmt->bind_param('s', $alt); // 's' = variable type string
    $stmt->execute();
    if ($stmt->errno) 
       die("Error: " . $stmt->error);
 
    
    $stmt->bind_result($src, $alt);   
       
    $stmt->fetch();

    echo $src . " - " . $alt;

    $stmt->close();
}


                                 
$con->close();


/*
run: 

http://www.ws.com/images/logo-wordpress.png - wordpress logo   

*/

 



answered Jul 10, 2016 by avibootz
edited Jul 10, 2016 by avibootz
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);

if ($stmt = $con->prepare("SELECT src, alt FROM images WHERE alt LIKE ?")) 
{
    $alt = 'w%';
    $stmt->bind_param('s', $alt); // 's' = variable type string
    $stmt->execute();
    if ($stmt->errno) 
       die("Error: " . $stmt->error);
 
    
    $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.wp.com/images/why-choose.png - why choose us

*/

 



answered Jul 10, 2016 by avibootz
...