Storing database records in an array

I would like to create an array that will store the records retrieved from the database using the query statement SELECT.

The records to be retrieved have several fields, such as name, name, mi, and another 20 fields. What would be the best approach when coding this function?

ok, I watched what the prisoner gave below .. the next question: how do I scan this array using queries? for example, I want to find the username ..

+8
arrays php mysql
source share
4 answers
<?php // run query $query = mysql_query("SELECT * FROM table"); // set array $array = array(); // look through query while($row = mysql_fetch_assoc($query)){ // add each row returned into an array $array[] = $row; // OR just echo the data: echo $row['username']; // etc } // debug: print_r($array); // show all array data echo $array[0]['username']; // print the first rows username 
+18
source share

You should not look for this array, but use the database features to do this.
Suppose you pass a username through a GET form:

 if (isset($_GET['search'])) { $search = mysql_real_escape_string($_GET['search']); $sql = "SELECT * FROM users WHERE username = '$search'"; $res = mysql_query($sql) or trigger_error(mysql_error().$sql); $row = mysql_fetch_assoc($res); if ($row){ print_r($row); //do whatever you want with found info } } 
+3
source share
 $mysearch="Your Search Name"; $query = mysql_query("SELECT * FROM table"); $c=0; // set array $array = array(); // look through query while($row = mysql_fetch_assoc($query)){ // add each row returned into an array $array[] = $row; $c++; } for($i=0;$i=$c;$i++) { if($array[i]['username']==$mysearch) { // name found } } 
+1
source share
 $memberId =$_SESSION['TWILLO']['Id']; $QueryServer=mysql_query("select * from smtp_server where memberId='".$memberId."'"); $data = array(); while($ser=mysql_fetch_assoc($QueryServer)) { $data[$ser['Id']] =array('ServerName','ServerPort','Server_limit','email','password','status'); } 
0
source share

All Articles