Search notes:

PHP: Accessing a MySQL database

Create a user and a database

<html>
<head><title>mysql: Create user</title></head>
<body>

<?php

  $db_conn = new mysqli('localhost', 'root', 'root');

  if ($db_conn -> connect_errno) {
    echo "Failed to connect to MySQL: " . $db_conn -> connect_error;
    return;
  }

  print "Connected as <code>root</code><br>";

  if (! $db_conn -> query("create user rene@localhost identified by 'renes_secret';") === TRUE) {
    print ("Could not create user <code>rene@localhost</code>: " . $db_conn -> error);
//  return;
  }
  else {
    print "User <code>rene@localhost created</code><br>";
  }

  if (! $db_conn -> query("create database renes_db") === TRUE) {
    print ("Could not create database <code>renes_db</code>: " . $db_conn -> error);
//  return;
  }
  else {
    print "Database <code>renes_db</code> created<br>";
  }

  
  if (! $db_conn -> query("grant all on renes_db.* to rene;") === TRUE) {
    print ("Could not grant all on renes_db: " . $db_conn -> error);
//  return;
  }
  else {
    print "All privileges on <code>renes_db</code> granted<br>";
  }

  $db_conn -> close();

?>

<p>Now, <a href='02_create_table.html'>create a table</a>.

</body>
</html>
Github repository about-php, path: /db/mysql/01_create_user_and_database.html

Create a table

<html>
<head><title>mysql: Create a table</title></head>
<body>

<?php

  $db_conn = new mysqli('localhost', 'rene', 'renes_secret');

  if ($db_conn -> connect_errno) {
    echo "Failed to connect to MySQL: " . $db_conn -> connect_error;
    return;
  }

  print "Connected as <code>rene</code><br>";

  $db_conn -> select_db("renes_db");

  if (! $db_conn -> query("create table tq84_table(id integer, txt varchar(20))" ) === TRUE) {
    print ("Could not create table <code>tq84_table</code>: " . $db_conn -> error);
    return;
  }

  print "Table <code>tq84_table</code> created<br>";

  $db_conn -> close();

?>

<p>Now, <a href='03_insert_into_table.html'>Fill the table</a>.

</body>
</html>
Github repository about-php, path: /db/mysql/02_create_table.html

Insert into table

<html>
<head><title>mysql: Fill a table</title></head>
<body>

<?php

  $db_conn = new mysqli('localhost', 'rene', 'renes_secret');

  if ($db_conn -> connect_errno) {
    echo "Failed to connect to MySQL: " . $db_conn -> connect_error;
    return;
  }

  print "Connected as <code>rene</code><br>";

  $db_conn -> select_db("renes_db");

  if (! $stmt = $db_conn -> prepare ("insert into tq84_table (id, txt) values (?, ?)") ) {
    print "Could not prepare statment: " . $db_conn->error;
    return;
  }

  $stmt -> bind_param('is', $i, $t); # i: integer, s: string

  $i = 1; $t = 'one'  ; $stmt -> execute() or trigger_error($db_conn->error); printf ("inserted: %d rows<br>", $stmt->affected_rows);
  $i = 2; $t = 'two'  ; $stmt -> execute() or trigger_error($db_conn->error); printf ("inserted: %d rows<br>", $stmt->affected_rows);
  $i = 3; $t = 'three'; $stmt -> execute() or trigger_error($db_conn->error); printf ("inserted: %d rows<br>", $stmt->affected_rows);
  $i = 4; $t =  null  ; $stmt -> execute() or trigger_error($db_conn->error); printf ("inserted: %d rows<br>", $stmt->affected_rows);
  $i = 5; $t = 'five' ; $stmt -> execute() or trigger_error($db_conn->error); printf ("inserted: %d rows<br>", $stmt->affected_rows);

  $stmt    -> close();
  $db_conn -> close();

?>

<p>Now, <a href='04_select_from_table.html'>select from the table</a>.

</body>
</html>
Github repository about-php, path: /db/mysql/03_insert_into_table.html

Select from table

<html>
<head><title>mysql: Select from a table</title></head>
<body>

<h1>Without bind variables</h1>

<?php

  $db_conn = new mysqli('localhost', 'rene', 'renes_secret');

  if ($db_conn -> connect_errno) {
    echo "Failed to connect to MySQL: " . $db_conn -> connect_error;
    return;
  }

  $db_conn -> select_db("renes_db");

  $stmt = $db_conn->query("select id, txt from tq84_table");

  print "<table>\n";
  while ($row = $stmt->fetch_array()) {

    printf (" <tr><td>%d</td><td>%s</td></tr>\n", $row['id'], $row['txt']);

  }
  print "</table>";


  $stmt    -> close();
  $db_conn -> close();

?>

<h1>With bind variables</h1>


<?php

  $db_conn = new mysqli('localhost', 'rene', 'renes_secret');

  if ($db_conn -> connect_errno) {
    echo "Failed to connect to MySQL: " . $db_conn -> connect_error;
    return;
  }

  $db_conn -> select_db("renes_db");

  $stmt = $db_conn->prepare("select id, txt from tq84_table where id > ?");
  $two = 2;
  $stmt -> bind_param('i', $two);
  $stmt -> execute();

  $result = $stmt -> get_result();

  print "<table>\n";
  while ($row = $result->fetch_array()) {

    printf (" <tr><td>%d</td><td>%s</td></tr>\n", $row['id'], $row['txt']);

  }
  print "</table>";


  $stmt    -> close();
  $db_conn -> close();

?>


<p>Now, <a href='05_cleanup.html'>clean the thing up</a>

</body>
</html>
Github repository about-php, path: /db/mysql/04_select_from_table.html

Cleaning up

<html>
<head><title>mysql: Drop database and user</title></head>
<body>

<?php


  $db_conn = new mysqli('localhost', 'root', 'root');

  if ($db_conn -> connect_errno) {
    echo "Failed to connect to MySQL: " . $db_conn -> connect_error;
    return;
  }

//print "Connected as <code>root</code><br>";

  if (! $db_conn -> query("drop database renes_db") === TRUE) {
    print ("Could not drop database <code>renes_db</code>: " . $db_conn -> error);
    return;
  }

  print "Database <code>renes_db</code> dropped<br>";

  if (! $db_conn -> query("drop user rene@localhost") === TRUE) {
    print ("could not drop user <code>rene</code>" . $db_conn -> error);
    return;
  }

  print "User <code>rene</code> dropped<br>";

  $db_conn -> close();

?>

<p><a href='01_create_user_and_database.html'>start over</a>.

</body>
</html>
Github repository about-php, path: /db/mysql/05_cleanup.html

See also

Accessing an sqlite database with PHP

Index