Difference between revisions of "Connecting to mysql"
From MyWiki
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | http://www.w3schools.com/php/php_mysql_insert.asp<br> | ||
+ | |||
<source lang="php"> | <source lang="php"> | ||
Line 13: | Line 15: | ||
if (!$conn) { | if (!$conn) { | ||
die("Connection failed: " . mysqli_connect_error()); | die("Connection failed: " . mysqli_connect_error()); | ||
− | } | + | } |
− | echo | + | echo "Connected successfully"; |
+ | |||
Line 22: | Line 25: | ||
+ | // 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> |
Latest revision as of 17:57, 10 November 2014
http://www.w3schools.com/php/php_mysql_insert.asp
////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; } ?>