我有一个名为test_config
的数据库表,有2个列,名为&值。
我想遍历每一行并从名称和值创建$variables
,例如$Name = "Value";
,然后可以在php文件中使用。
第一行在Name colum中有abc,在value columb中有123,因此创建的vaiable将是$abc = 123;
可以这样做。
感谢。
答案 0 :(得分:1)
这就是方法,但没有命名变量
$arr = array();
while($row = $query->fetch_array()){
$arr['name'=>$row['name'], 'value'=>$row['value']];
}
答案 1 :(得分:0)
我读到你想为每个名字创建一个变量。如果我是你,我会把它放在一个阵列中,就像Jeroen说的那样。这可以通过执行以下操作来实现:
#include <iostream>
#include <string>
using namespace std;
struct Point {
private:
int xCord,yCord;
public:
void setX(int x);
void setY(int y);
int getX();
int getY();
int rotate(int x, int y, Point p1);
int moveHorizontally(int x, int a, int b);
int moveVertically(int y, int a, int b);
};
int main() {
Point p1;
p1.setX(1); //sets X
p1.setY(2); //sets Y
cout << p1.getX() << ", " << p1.getY() << endl; //prints current value of X & Y
p1.rotate(p1.getX(), p1.getY(), p1);
cout << p1.getX() << ", " << p1.getY() << endl;
return 0;
}
void Point::setX(int newX) {
xCord = newX;
}
void Point::setY(int newY) {
yCord = newY;
}
int Point::getY() { //This will just return the y Cord.
return yCord;
}
int Point::getX() { //This will just return the x Cord.
return xCord;
}
int Point::moveHorizontally(int x, int tempX, int tempY) {
//Move the point to the right if positive.
//Move the point to the left if negative.
int newX = tempX + (x);
return newX;
}
int Point::moveVertically(int y, int tempX, int tempY) {
//Move the point up if positive.
//Move the point down if negative.
int newY = tempY + (y);
return newY;
}
int Point::rotate(int tempX, int tempY, Point p1){
//(1,2) -->> (-2,1)
int tempX_DNC = tempX;
int tempY_DNC = tempY;
int quadrant;
if((tempX > 0) && (tempY > 0)) { //Quadrant 1: x(positive), y(positive) Then rotates to Quad 2
quadrant = 1;
tempX = -(tempY);
tempY = tempX_DNC;
} else if ((tempX < 0) && (tempY > 0)) { //Quadrant 2: x(negative), y(positive) Then rotates to Quad 3
quadrant = 2;
tempX = -(tempY_DNC);
tempY = tempX_DNC;
} else if ((tempX < 0) && (tempY < 0)) { //Quadrant 3: x(negative), y(negative) Then rotates to Quad 4
quadrant = 3;
tempX = -(tempY_DNC);
tempY = tempX_DNC;
} else if ((tempX > 0) && (tempY < 0)) { //Quadrant 4: x(positive), y(negative) Then rotates to Quad 1
quadrant = 4;
tempX = -(tempY_DNC);
tempY = tempX_DNC;
} else {
quadrant = 0;
}
//This will rotate the points 90* to the left.
//(1,2) will then become (-2,1)
//I could have if in quadrant1, all are positive, if in quadrant 2 the x would be negative and y would be positive
//If in quadrant 3 the x and y will both be negative, if in quadrant 4 the x would be positive and the y would be negative
cout << tempX << ", " << tempY << endl;
p1.setX(tempX);
p1.setY(tempY);
cout <<"x is: " <<p1.getX() <<endl;
cout <<"Y is: " <<p1.getY() <<endl;
}
答案 2 :(得分:0)
假设您有来自数据库的数组 -
$foo = array('first' => '1st',
'second' => '2nd',
'third' => '3rd');
您希望输出为 -
$first = '1st';
$second = '2nd';
$third = '3rd';
您可以使用extract()
功能
<?php extract($foo); ?>
答案 3 :(得分:0)
在迭代数据库行的循环中,您可以使用extract()将列设置为变量及其值。
while ($databaseROW = mysql_fetch_assoc($result))
{
extract($databaseROW);
//rest of code from here down can now use variables
//you have columns Name & Value
//so now $Name & $Value variables are available
//$Name = "value from column Name for this $databaseROW";
//$Value = "value from column Value for this $databaseROW";
}
答案 4 :(得分:0)
通过一些进一步的研究,我找到了解决方案。
try {
$conn = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME."", DB_USERNAME, DB_PASSWORD);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT Name, Value FROM test_config");
$stmt->setFetchMode(PDO::FETCH_KEY_PAIR);
$stmt->execute();
$array = $stmt->fetchAll();
//var_dump($array);
extract($array);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
然后我用
测试它echo "The first row variable is $abc</br>";