Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. $host="localhost";
  2. $username="sample1";
  3. $password="1234";
  4. $db_name="sampledb";
  5.  
  6. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  7. mysql_select_db("$db_name")or die("cannot select DB");
  8.  
  9. function example1(array1) {
  10. //is this allowed??
  11. $array1 = array();
  12. $ctr = 0;
  13. $ctr1=1;
  14. $sql="SELECT names FROM tblnamelist";
  15. $result=mysql_query($sql);
  16. $row=mysql_fetch_array($result);
  17. $count=mysql_num_rows($result);
  18. //I also want to populate the array1 with all the values that was retrieved in the query then return it as an array
  19. if($count!=0) {
  20. while($ctr1<=$count) {
  21. $array1[$ctr]=$row[$ctr];
  22. }
  23. }
  24. }
  25.  
  26. function get_results()
  27. {
  28. $array1 = array();
  29.  
  30. $sql="SELECT names FROM tblnamelist";
  31. $result=mysql_query($sql);
  32.  
  33. while($row=mysql_fetch_array($result))
  34. {
  35. $array1[] = $row;
  36. }
  37. return $array1;
  38. }
  39. $array = get_results();
  40.  
  41. function example1(&$array1) {
  42. //is this allowed?? -- yes, but you have to do it by reference see & in the definition
  43. $array1 = array();
  44.  
  45. while($row=mysql_fetch_array($result){
  46.  
  47. echo $row['field_name'];
  48. }
  49.  
  50. $sql="SELECT names FROM tblnamelist";
  51. $result=mysql_query($sql);
  52. while($row=mysql_fetch_array($result)){
  53. echo $row['field_name'];
  54.  
  55. }
  56.  
  57. if ($count!=0)
  58. {
  59. while($row=mysql_fetch_array($result))
  60. {
  61. array_push($array1,$row['names']);
  62. }
  63. }
  64. print_r($array1);
  65.  
  66. $result=mysql_query($sql);
  67. while($row=mysql_fetch_array($result)) {
  68. $array1[]=$row;
  69. }
  70.  
  71. while($row=mysql_fetch_array($result)) {
  72. $array1[$row['myUniqueRowID']]=$row;
  73. }
  74.  
  75. $array1 = array();
  76. while($row = mysql_fetch_array($result)) {
  77. $array1[] = $row['names']; // Insert the value of $row['names'] to the end of the array
  78. }
  79.  
  80. // return your array, or use Jakub's method.
  81. return $array1;
  82.  
  83. function getNamesArray() {
  84. $sql="SELECT names FROM tblnamelist";
  85. $result=mysql_query($sql);
  86.  
  87. // this is the result array that this function will return
  88. $array1 = array();
  89.  
  90. // loop while there are rows in the mysql result
  91. while($row = mysql_fetch_array($result)) {
  92. // Insert the value of $row['names'] to the end of the array
  93. $array1[] = $row['names'];
  94. }
  95. return $array1;
  96. }
  97.  
  98. // test the function:
  99. $test = getNamesArray();
  100. var_dump($test);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement