Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $host="localhost";
- $username="sample1";
- $password="1234";
- $db_name="sampledb";
- mysql_connect("$host", "$username", "$password")or die("cannot connect");
- mysql_select_db("$db_name")or die("cannot select DB");
- function example1(array1) {
- //is this allowed??
- $array1 = array();
- $ctr = 0;
- $ctr1=1;
- $sql="SELECT names FROM tblnamelist";
- $result=mysql_query($sql);
- $row=mysql_fetch_array($result);
- $count=mysql_num_rows($result);
- //I also want to populate the array1 with all the values that was retrieved in the query then return it as an array
- if($count!=0) {
- while($ctr1<=$count) {
- $array1[$ctr]=$row[$ctr];
- }
- }
- }
- function get_results()
- {
- $array1 = array();
- $sql="SELECT names FROM tblnamelist";
- $result=mysql_query($sql);
- while($row=mysql_fetch_array($result))
- {
- $array1[] = $row;
- }
- return $array1;
- }
- $array = get_results();
- function example1(&$array1) {
- //is this allowed?? -- yes, but you have to do it by reference see & in the definition
- $array1 = array();
- while($row=mysql_fetch_array($result){
- echo $row['field_name'];
- }
- $sql="SELECT names FROM tblnamelist";
- $result=mysql_query($sql);
- while($row=mysql_fetch_array($result)){
- echo $row['field_name'];
- }
- if ($count!=0)
- {
- while($row=mysql_fetch_array($result))
- {
- array_push($array1,$row['names']);
- }
- }
- print_r($array1);
- $result=mysql_query($sql);
- while($row=mysql_fetch_array($result)) {
- $array1[]=$row;
- }
- while($row=mysql_fetch_array($result)) {
- $array1[$row['myUniqueRowID']]=$row;
- }
- $array1 = array();
- while($row = mysql_fetch_array($result)) {
- $array1[] = $row['names']; // Insert the value of $row['names'] to the end of the array
- }
- // return your array, or use Jakub's method.
- return $array1;
- function getNamesArray() {
- $sql="SELECT names FROM tblnamelist";
- $result=mysql_query($sql);
- // this is the result array that this function will return
- $array1 = array();
- // loop while there are rows in the mysql result
- while($row = mysql_fetch_array($result)) {
- // Insert the value of $row['names'] to the end of the array
- $array1[] = $row['names'];
- }
- return $array1;
- }
- // test the function:
- $test = getNamesArray();
- var_dump($test);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement