MySQLi
An example below shows basic MySQLi operation: connecting and querying.
Source C# code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
var mysqli = new MySQLi("localhost1", "root", null, DATABASE_NAME); if (mysqli.WasConnectionError) { echo("Connection error (" + mysqli.ConnectErrno + ") " + mysqli.ConnectError); return; } if (mysqli.SetCharset("utf8")) echo("Charset OK<br />"); else { echo("Error " + mysqli.Error + "<br />"); mysqli.Close(); return; } mysqli.AutoCommit(true); var result = mysqli.QueryResult("select * from persons", MySQLiResultMode.UseResult); if (result.IsNotFalse) { echo("Rows = " + result.Value.NumRows + "<br />"); Person person; while (result.Value.Fetch<Person>(out person)) echo("firstName= " + htmlspecialchars(person.FirstName) + ", lastName=" + htmlspecialchars(person.LastName) + "<br />" + PHP_EOL); result.Value.Free(); } else echo("Error " + mysqli.Error + "<br />"); mysqli.Close(); |
Translated code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$mysqli = new \mysqli('localhost1', 'root', null, self::DATABASE_NAME); if (!empty($mysqli->connect_error)) { echo 'Connection error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error; return; } if ($mysqli->set_charset('utf8')) echo 'Charset OK<br />'; else { echo 'Error ' . $mysqli->error . '<br />'; $mysqli->close(); return; } $mysqli->autocommit(true); $result = $mysqli->query('select * from persons', MYSQLI_USE_RESULT); if ($result !== false) { echo 'Rows = ' . $result->num_rows . '<br />'; while($person = $result->fetch_assoc()) echo 'firstName= ' . htmlspecialchars($person['first_name']) . ', lastName=' . htmlspecialchars($person['last_name']) . '<br />' . PHP_EOL; $result->free(); } else echo 'Error ' . $mysqli->error . '<br />'; $mysqli->close(); |