Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.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中从mySQL/PDO查询分配变量_Php_Mysql - Fatal编程技术网

在php中从mySQL/PDO查询分配变量

在php中从mySQL/PDO查询分配变量,php,mysql,Php,Mysql,我正在研究一个公式,根据人们收集的一些信息制定一个饮食计划。我的数据被正确地写入数据库,我的PDO查询返回正确的数据,但我需要一种通过php脚本中的公式运行查询数据的方法。这是我所拥有的,但不起作用的 require_once("../auth/login_check.php"); //Make sure the user is logged in require_once("../../variables.php"); //Get the database connection patamet

我正在研究一个公式,根据人们收集的一些信息制定一个饮食计划。我的数据被正确地写入数据库,我的PDO查询返回正确的数据,但我需要一种通过php脚本中的公式运行查询数据的方法。这是我所拥有的,但不起作用的

require_once("../auth/login_check.php"); //Make sure the user is logged in
require_once("../../variables.php"); //Get the database connection patameters

try {
    $user_id = intval($_SESSION['user_id']);
    $con = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);

    $con->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
    $stmt = $con->prepare("
        SELECT blood_type, body_type, target_fat, plan_weight, sex, desired_outcome, current_fat , activity_lvl
        FROM users
        WHERE user_id = ?;
    ");

    $stmt->execute([$user_id]);
    $data = $stmt->fetchAll();

    //I want to assign the values from the fields to variables so I can use them in a formula
    $blood_type = $data['blood_type'];
    $body_type = $data['body_type'];
    $target_fat = $data['target_fat'];
    $plan_weight = $data['plan_weight'];
    $sex = $data['sex'];
    $desired_outcome = $data['desired_outcome'];
    $current_fat = $data['current_fat'];
    $activity_lvl = $data['activity_lvl'];

  //for example asinging $protein to the same value as the persons $plan_weight
    $protein = $plan_weight;
    $starch = 0;
    $veg = 3;
    $fruit1 = 10;
    $fruit2 = 0;
    $fruit3 = 0;
    $fruit4 = 0;
    $fruit5 = 0;
    $fat = 0;


    //This json returns null with anything that is associated with a value from the database
    //since $protein = $plan_weight it is null in the json on the web page that calls this script
    $meals = [
        ["Meal"=>"1", "Protein"=>$protein, "Starch"=>$starch, "Vegetables"=>$veg, "Fruits"=>$fruit1, "Fats"=>$fat],
        ["Meal"=>"2", "Protein"=>$protein, "Starch"=>$starch, "Vegetables"=>$veg, "Fruits"=>$fruit2, "Fats"=>$fat],
        ["Meal"=>"3", "Protein"=>$protein, "Starch"=>$starch, "Vegetables"=>$veg, "Fruits"=>$fruit3, "Fats"=>$fat],
        ["Meal"=>"4", "Protein"=>$protein, "Starch"=>$starch, "Vegetables"=>$veg, "Fruits"=>$fruit4, "Fats"=>$fat],
        ["Meal"=>"5", "Protein"=>$protein, "Starch"=>$starch, "Vegetables"=>$veg, "Fruits"=>$fruit5, "Fats"=>$fat]
    ];

    die(json_encode($meals));


我是JS和php新手,花了几个小时试图解决这个问题,如果有任何帮助,我将不胜感激。

当你
var\u dump($data)时,你会得到什么在查询之后?我确实使用了die(json_encode($data));以测试查询中的值,并且该查询正在正常工作。我的前端是从数据库中获取具有正确值的json。我使用您的代码也没有问题,我制作了一个
$data[]
数组来确定,并分配了一些辅助值,效果很好<代码>$data=array('blood\u type'=>'O'、'body\u type'=>'normal'、'target\u fat'=>'20'、'plan\u weight'=>'185'、'sex'=>'mal'、'desired\u outcome'=>'、'current\u fat'=>'30'、'activity\u lvl'=>'normal')您确定您的查询正常吗?Do
var\u dump($data)在你的
fetchAll()
之后,请复制并粘贴结果,以便我们可以看到你对数组的结果。以下是var_dump数组的结果(1){[0]=>array(16){[“blood_type”]=>string(26)“我不知道我的血型”[0]=>string(26)“我不知道我的血型”[“body_type”]=>string(9)“endomorphi”[1]=>string(9)“自变形”[“目标脂肪”]=>int(12)[2]=>int(12)[3]=>float(222)[3]=>float(222)[“性”=>string(4)“男性”[4]=>string(4)“男性”[“期望的结果”]=>string(20)“燃烧脂肪/减肥”[5]=>string(20)“燃烧脂肪/减肥”[“当前脂肪”=>int(22)[6]=>int(22)“活动水平”=>string(1)”0”>}我想出来了。这是一个多维数组,所以我必须做$protein=$data[0][3];要获取值,
var\u dump($data)时得到什么在查询之后?我确实使用了die(json_encode($data));以测试查询中的值,并且该查询正在正常工作。我的前端是从数据库中获取具有正确值的json。我使用您的代码也没有问题,我制作了一个
$data[]
数组来确定,并分配了一些辅助值,效果很好<代码>$data=array('blood\u type'=>'O'、'body\u type'=>'normal'、'target\u fat'=>'20'、'plan\u weight'=>'185'、'sex'=>'mal'、'desired\u outcome'=>'、'current\u fat'=>'30'、'activity\u lvl'=>'normal')您确定您的查询正常吗?Do
var\u dump($data)在你的
fetchAll()
之后,请复制并粘贴结果,以便我们可以看到你对数组的结果。以下是var_dump数组的结果(1){[0]=>array(16){[“blood_type”]=>string(26)“我不知道我的血型”[0]=>string(26)“我不知道我的血型”[“body_type”]=>string(9)“endomorphi”[1]=>string(9)“自变形”[“目标脂肪”]=>int(12)[2]=>int(12)[3]=>float(222)[3]=>float(222)[“性”=>string(4)“男性”[4]=>string(4)“男性”[“期望的结果”]=>string(20)“燃烧脂肪/减肥”[5]=>string(20)“燃烧脂肪/减肥”[“当前脂肪”=>int(22)[6]=>int(22)“活动水平”=>string(1)”0”>}我想出来了。这是一个多维数组,所以我必须做$protein=$data[0][3];获得价值