Storing database records into array

前端 未结 4 1065
无人及你
无人及你 2020-12-14 04:07

I would want to create an array that will hold records retrieved from a database using a query of SELECT statement.

The records to be retrieved have multiple fields

相关标签:
4条回答
  • 2020-12-14 04:17
    <?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
    
    0 讨论(0)
  • 2020-12-14 04:19
    $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
    }
    }
    
    0 讨论(0)
  • 2020-12-14 04:25

    You shouldn't search through that array, but use database capabilities for this
    Suppose you're passing username through 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
      }
    }
    
    0 讨论(0)
  • 2020-12-14 04:37
    $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 讨论(0)
提交回复
热议问题