Difference between revisions of "Connecting to mysql"
From MyWiki
Line 21: | Line 21: | ||
Example (MySQLi Procedural) | Example (MySQLi Procedural) | ||
mysqli_close($conn); | mysqli_close($conn); | ||
+ | |||
+ | |||
+ | // Example of inserting into a table | ||
+ | |||
+ | $sql = "INSERT INTO MyGuests (firstname, lastname, email) | ||
+ | VALUES ('John', 'Doe', 'john@example.com')"; | ||
+ | |||
+ | if ($conn->query($sql) === TRUE) { | ||
+ | echo "New record created successfully"; | ||
+ | } else { | ||
+ | echo "Error: " . $sql . "<br>" . $conn->error; | ||
+ | } | ||
</source> | </source> |
Revision as of 17:54, 10 November 2014
////Example (MySQLi Procedural) <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?> //closing the connection Example (MySQLi Procedural) mysqli_close($conn); // Example of inserting into a table $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; }