irwan

Different MySQL with MySQLi

Aug 4th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. Table
  2.  
  3. CREATE TABLE IF NOT EXISTS `users` (
  4.   `id` int(11) NOT NULL AUTO_INCREMENT,
  5.   `nama` varchar(99) NOT NULL,
  6.   PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
  8.  
  9. INSERT INTO `users` (`id`, `nama`) VALUES
  10. (1, 'zlumberjay');
  11.  
  12.  
  13.  
  14. STEP 2, create file config.php:
  15.  
  16.  
  17. <?php
  18. define ('HOST', 'localhost');
  19. define ('USR', 'root');
  20. define ('PASS', '');
  21. define ('DB', 'test');
  22. ?>
  23.  
  24.  
  25.  
  26. STEP 2 create file TestMysql.php
  27.  
  28.  
  29. <?php
  30. include "config.php";
  31. $mysql = mysql_connect(HOST, USR, PASS)
  32.   or die ("tidak terkoneksi dengan HOST");
  33. mysql_select_db ("test", $mysql)
  34.   or die ("tidak terkoneksi dengan DB");
  35. $query = "SELECT * FROM users";
  36. $result=mysql_query($query);
  37. while($row=mysql_fetch_assoc($result)){
  38.        echo $row['nama'] ."<br/>";
  39. }
  40. ?>
  41.  
  42.  
  43.  
  44. STEP 2 create file TestMysqli.php
  45.  
  46. include "config.php";
  47. $mysqli = new mysqli(HOST, USR, PASS, DB);
  48. if(mysqli_connect_errno()) {
  49. echo "Tidak terkoneksi dengan HOST DB";
  50. }
  51. $query = "SELECT * FROM users";
  52. if ($result = $mysqli->query($query))
  53. {
  54.    while ($row = $result->fetch_assoc())
  55. {
  56.        echo $row['nama'] ."<br/>";
  57. }
  58.    $result->close();
  59. }
  60. $mysqli->close();
  61. ?>
Advertisement
Add Comment
Please, Sign In to add comment