Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从表的单个列中获取数据,并将这些数据保存在每个变量中_Php_Mysql - Fatal编程技术网

Php 从表的单个列中获取数据,并将这些数据保存在每个变量中

Php 从表的单个列中获取数据,并将这些数据保存在每个变量中,php,mysql,Php,Mysql,我知道获取行数据的方法是使用 $row = mysql_fetch_row($RS); $variable1 = $row[0]; $variable2 = $row[1]; $variable3 = $row[2]; 但是如何从列中获取值呢?我知道要获得特定行的列数据,必须使用while。但是通过而,我们如何像处理行数据那样获取并存储在变量中呢 while ($row = mysql_fetch_array($RS)) { echo "$row[servicename] = $row

我知道获取行数据的方法是使用

$row = mysql_fetch_row($RS);
$variable1 = $row[0];
$variable2 = $row[1];
$variable3 = $row[2];
但是如何从列中获取值呢?我知道要获得特定行的列数据,必须使用while。但是通过
,我们如何像处理行数据那样获取并存储在变量中呢

while ($row = mysql_fetch_array($RS)) {
    echo "$row[servicename] = $row[servicestatus]"; //grab data from a single column of a table
    echo "<br>";
}
while($row=mysql\u fetch\u数组($RS)){
echo“$row[servicename]=$row[servicestatus]”;//从表的单个列中获取数据
回声“
”; }
我会尽可能多地解释,希望我的解决方案能对您有所帮助

因此,您希望打印特定列的值。首先,需要一个新数组来存储值,需要一个计数器来设置存储值的位置

$newArray=array();
$i=0;
while ($row = mysql_fetch_array($RS)) {
    echo"<pre>";
    $newArray[$i]=$row[servicename] ; //the single column name you want is servicename
    $i++;
}
print_r($newArray);
$newArray=array();
$i=0;
while($row=mysql\u fetch\u数组($RS)){
回声“;
$newArray[$i]=$row[servicename];//需要的单个列名是servicename
$i++;
}
打印(新数组);
之后,所需的列值将位于数组
$newArray
中,您可以通过循环以正常方式访问它,并将每个字段的值设置为所需的变量


另外,我强烈建议您使用pdo或mysqli。

可能会有所帮助,您可以尝试foreach和mysql_fetch_assoc,而不是while。请停止使用mysql_*函数。它们被抛弃、不安全、不受欢迎。它们在较新的php版本中已被完全删除,因此您的代码在较新的版本中无法工作。使用或代替。