Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are not in too deep, I <em>highly</em> recommend you use <a href="http://php.net/manual/en/book.pdo.php" rel="nofollow">PDO</a> instead of MySQLi because the MySQLi API is <em>terrible</em>.</p> <p>What you want is actually very difficult. Here's a function which demonstrates the hoops you need to jump through:</p> <pre><code>function query(MySQLi $db, $sql, array $data) { $stmt = $db-&gt;prepare($sql); if (!$stmt) return null; $types = str_repeat('s', count($data)); $args = array(); // references to values in $data for call_user_func_array foreach ($data as $k =&gt; $v) { $args[$k] =&amp; $data[$k]; } array_unshift($args, $types); call_user_func_array(array($stmt, 'bind_param'), $args); $stmt-&gt;execute(); // mysqlind driver if (method_exists($stmt, 'get_result')) { $res = $stmt-&gt;get_result(); $rows = $res-&gt;fetch_all(MYSQLI_ASSOC); $res-&gt;free(); } else { // by hand, ugh! $rows = array(); $resultbinds = array(); // where results will end up $args = array(); // references to $resultbinds for call_user_func_array $meta = $stmt-&gt;result_metadata(); while ($fieldmeta = $meta-&gt;fetch_field()) { $resultbinds[$fieldmeta-&gt;name] = null; $args[] =&amp; $resultbinds[$fieldmeta-&gt;name]; } $meta-&gt;free(); call_user_func_array(array($stmt, 'bind_result'), $args); while ($stmt-&gt;fetch()) { $row = array(); // copy array to dereference foreach ($resultbinds as $k =&gt; $v) { $row[$k] = $v; } $rows[] = $row; } } $stmt-&gt;free_result(); $stmt-&gt;close(); return $rows; } </code></pre> <p>This is how you would do the same thing in PDO:</p> <pre><code>function query_PDO($db, $sql, $data) { $stmt = $db-&gt;prepare($sql); $stmt-&gt;execute($data); $rows = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC); $stmt-&gt;closeCursor(); return $rows; } </code></pre> <p>Here is how to connect to MySQL using PDO:</p> <pre><code>function connect_PDO() { $host = 'localhost'; $user = 'root'; $pass = 'root'; $dbname = 'scratch'; $port = 8889; $socket = '/Applications/MAMP/tmp/mysql/mysql.sock'; $options = array( PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_ASSOC, ); if (version_compare(PHP_VERSION, '5.3.6', '&lt;')) { $options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8'; } $dsn = "mysql:host={$host};port={$port};unix_socket={$socket};dbname={$dbname};charset=utf8"; return new PDO($dsn, $user, $pass, $options); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload