0
<?php
echo "Lets run the program";
$id = 1;
$conn = new mysqli('localhost', 'root', 'root');
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = $conn->stmt_init();

if($stmt->prepare("Select Genre_Name from Practice.genre_list where Genre_ID = ?")){
    $stmt->bind_param(i,$id );
    $stmt->execute();
    $stmt->bind_result(s, $result);
    $stmt->fetch();
    echo $result;
    echo "in if function";
}
else{
    echo "some issue";
}
?>

output:

Lets run the program Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /Applications/MAMP/htdocs/practice/preparedStatement.php on line 5 Connect failed: No such file or directory

After searching on web, I checked the configuration in php.ini file and mysql.sock is set properly, no issues with that. Still not able to figure out why prepared statement is not working, I am able to connect using traditional approach of mysqli_connect(), but not with the OOA approach.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Gaurav Gupta
  • 55
  • 11

1 Answers1

0

I'm assuming you're running on Linux. The setting in your PHP.ini file for mysql.default_socket is not pointing to the mysql socket.

Open a command line and type:

locate mysql.sock

After that, edit your PHP.ini and set:

mysql.default_socket = /path/to/mysql.sock

Substitute the location of mysql.sock in place of /path/to/mysql.sock

It's fairly likely that the file is in /var/lib/mysql/mysql.sock, but it may vary depending on your server.

Karl Buys
  • 439
  • 5
  • 12
  • ; Default socket name for local MySQL connects. If empty, uses the built-in ; MySQL defaults. mysql.default_socket = /Applications/MAMP/tmp/mysql/mysql.sock This is already set in php.ini file – Gaurav Gupta Nov 30 '16 at 21:07