简体   繁体   English

PHP查询结果转换成变量

[英]PHP query result into variable

Hi everyone I am a beginner programmer trying to get query results into variable in PHP. 大家好,我是一个初学者,试图将查询结果转换成PHP变量。 For registeration purposes, I want to assign an available campaignID to variable $campaignID, so I can insert $campaignID along with other variables to the new user row. 为了进行注册,我想为变量$ campaignID分配一个可用的campaignID,因此我可以将$ campaignID以及其他变量插入新的用户行。 I only need help on getting my result of my SELECT Statement into the variable campaignID! 我仅需要帮助将我的SELECT语句的结果放入变量campaignID中! I am aware that there are multiple questions asking the exact thing want like 1 & 2 . 我知道有多个问题问的确切内容是12 However, I have been trying the solutions for hours now but to no avail and I need help to apply the answers to my situation. 但是,我已经尝试了数小时的解决方案,但无济于事,我需要帮助将答案应用于我的处境。

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_reporting(E_ERROR);
try{
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

    $query = "SELECT campaignID from ebaycampaigns WHERE Taken='N' LIMIT 1";
    $result = $mysqli->query($query);

    if(!$result){
        echo "No data was selected";
    }

    if (mysqli_num_rows($result)==1){
        $row = mysql_fetch_array($result);
        echo "campaignID: " . $row[0];

        //or
        //$row = $result->fetch_array(MYSQLI_BOTH);
        //printf ("%s (%s)\n", $row['campaignID']);

    }

    else{
        echo "not found!";
    }

    $conn->close();
}
catch(Exception $e) {
    $json_out = "[".json_encode(array("result"=>0))."]";
    echo $json_out;

}

?>

None of the responses were echoed, $row['campaignID'] , $row[0] , $campaignID . $row['campaignID']$row[0]$campaignID没有响应。 I am also not conerned with SQL injections so far as this is a small project! 就这个小项目而言,我也不惧怕SQL注入! So feel free to recommend any methods! 因此,随时推荐任何方法!

First off change mysql_fetch_array to mysqli_fetch_array :) 首先,将mysql_fetch_array更改为mysqli_fetch_array :)

nevermind the following line, iil just leave it in, I saw you used LIMIT. 没关系以下行,我只是把它留在里面,我看到您使用了LIMIT。

And secondly change mysqli_num_rows($result)==1 to mysqli_num_rows($result) > 0 然后将mysqli_num_rows($ result)== 1更改为mysqli_num_rows($ result)> 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM