如何手动从PHP中获取Mysql记录? [关闭]

问题描述:

I am having table with two columns Symbol and weight. I am using this code

$table=mysql_fetch_array($result);
echo count($table);
echo "<pre>";
print_r($table);
echo "</pre>";

Out of the Above Code is

4
Array
(
    [0] => x1
    [symbol] => x1
    [1] => 0.50
    [weight] => 0.50
)

My table contains 4 entries. But it is showing only one. i want to see all the entries, can you tell how to access them manually. I know i can access them using.

while($table=mysql_fetch_array($result)
{
echo "<br/>".$table['symbol']."\t".$table['weight'];
}

But I want to do this manually. Because later i want to perform some manual operation on this array.

Content of my Table is

Symbol | Weight
    X1 | .50
    X2 | .25
    X3 | .20
    X4 | .10

By manually i mean, i will fetch the last two record without traversing the whole array(i.e with the help of while) then add the weight of these last two records.

For Example : Here my table have 4 entries and last two records are X3 and X4 with weight .20 and .10. I will replace these two records with the new single records that is X33 with weight .30. and my new table will look like

Symbol | Weight
    X1 | .50
    X2 | .25
    X33| .30

我的桌子上有两列符号和重量。 我正在使用此代码 p>

  $ table = mysql_fetch_array($ result); 
echo count($ table); 
echo“&lt; pre&gt;”; 
print_r($ table  ); 
echo“&lt; / pre&gt;”; 
  code>  pre> 
 
 

以上代码之外是 p>

  4  
Array 
(
 [0] =&gt; x1 
 [symbol] =&gt; x1 
 [1] =&gt; 0.50 
 [权重] =&gt; 0.50 
)
  code>   pre> 
 
 

我的表包含4个条目。 但它只显示一个。 我想查看所有条目,你能告诉我们如何手动访问它们。 我知道我可以使用它来访问它们。 p>

  while($ table = mysql_fetch_array($ result)
 {
echo“&lt; br /&gt;”。$ table ['symbol']。“\ t”  。$ table ['weight']; 
} 
  code>  pre> 
 
 

但我想手动执行此操作。因为稍后我想对此阵列执行一些手动操作。 p>

我的表的内容是 p>

 符号|权重
 X1 | .50 
 X2 | .25 
 X3 |。  20 
 X4 | .10 
  code>  pre> 
 
 

通过手动我的意思是,我将获取最后两个记录而不遍历整个数组(即借助于while) 然后添加最后两个记录的权重。 p>

例如:这里我的表有4个条目,最后两个记录是X3和X4,权重为.20和.10。我将替换这些 新的单个记录的两条记录是X33,权重为.30。我的新表格看起来像 p>

  Symbol | Weight 
 X1 | .50 
 X2 |。  25 
 X33 | .30 
  code>  pre> 
  div>

You have two options, throw in a WHERE clause or do a loop until you get all the data and store it in your own array.

while($table=mysql_fetch_array($result)
{
    $data[]=$table;
}

With your array, you can then access it manually however you would like.

The best option is to use mysqli or PDO since mysql is deprecated and will be removed in the future. I know mysqli result object has the fetch_all function to get an array of objects.