简体   繁体   English

查询未使用直接SQL正常工作的php返回结果

[英]A query is not returning results using php that works using straight SQL

UPDATE: - So I have found that the cause of the problem is that I am running a previous query (in this case a multi_query) that appears to be out of sync with the next query. 更新: -因此,我发现问题的原因是我正在运行上一个查询(在本例中为multi_query),该查询似乎与下一个查询不同步。 If I close and reopen the connection, it works fine, but there is probably a better way to go about solving this problem. 如果我关闭并重新打开连接,则可以正常工作,但是可能有更好的方法来解决此问题。 Any pointers on a better way are appreciated. 任何更好的方法的指针,表示赞赏。

Complete code: 完整的代码:

if (filter_var($_POST['survey']['contact']['contactEmail'], FILTER_VALIDATE_EMAIL)) {
    $survey = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
    $survey = $survey['survey'];
    $email = $survey['contact']['contactEmail'];
    include 'databaseconnection.php';
    /*
     * This part works...
     */
    $query = "BEGIN;INSERT INTO interests(contactEmail) VALUES ('$email');INSERT INTO equipment(contactEmail) VALUES ('$email'); INSERT INTO classes(contactEmail) VALUES ('$email');INSERT INTO housing(contactEmail) VALUES ('$email');INSERT INTO newSkills(contactEmail) VALUES ('$email');INSERT INTO selfIdentity(contactEmail) VALUES ('$email');INSERT INTO areYouA(contactEmail) VALUES ('$email');INSERT INTO locationIdeas(contactEmail) VALUES ('$email');INSERT INTO involved(contactEmail) VALUES ('$email');INSERT INTO contact(contactEmail) VALUES ('$email');";
    foreach ($survey as $key => $val) {
        foreach ($val as $k => $v) {
            $query.= "UPDATE $key SET $key.$k = '$v' WHERE $key.contactEmail='$email';";
        }
    }

    $query.="COMMIT;";
    if (!($mysqli->multi_query($query))) {
        echo "Error: (" . $mysqli->errno . ") " . $mysqli->error;
    }
    /*
     * This subsequent query does not work unless I close the connection and restart it, giving an " Error: (2014) Commands out of sync; you can't run this command now "
     */
/*******************************************/
    $mysqli->close();
    include 'databaseconnection.php';
/*******************************************/

    $query = "SELECT distinct * FROM interests, equipment, classes, housing, newSkills, selfIdentity, areYouA, locationIdeas, involved, contact WHERE interests.contactEmail='$email' AND equipment.contactEmail='$email' AND classes.contactEmail='$email' AND housing.contactEmail='$email' AND newSkills.contactEmail='$email' AND selfIdentity.contactEmail='$email' AND areYouA.contactEmail='$email' AND locationIdeas.contactEmail='$email' AND involved.contactEmail='$email' AND contact.contactEmail='$email';";
    if (!($result = $mysqli->query($query))) {
        echo "Error: (" . $mysqli->errno . ") " . $mysqli->error;
    }
    $statistics = $result->fetch_array(MYSQLI_ASSOC);
    var_dump($statistics);
} else {
    echo 'Please enter a valid email address';
}

I am baffled as to why this query is not returning any results. 我对为什么这个查询不返回任何结果感到困惑。 The code is supposed to be returning someone's entry in a survey for confirmation that it was received and the '$email' is the email address of the person who did the survey. 该代码应返回调查中某人的条目以确认已收到该条目,并且“ $ email”是进行调查的人的电子邮件地址。 But it appears not to be returning anything (nothing is returned from a var_dump of the $result variable) and I get the following message" 但它似乎未返回任何内容($ result变量的var_dump均未返回任何内容),我收到以下消息:

The relevant code: 相关代码:

$result = $mysqli->query("SELECT distinct * FROM interests, equipment, classes, housing, newSkills, selfIdentity, areYouA, locationIdeas, involved, contact WHERE interests.contactEmail='$email' AND equipment.contactEmail='$email' AND classes.contactEmail='$email' AND housing.contactEmail='$email' AND newSkills.contactEmail='$email' AND selfIdentity.contactEmail='$email' AND areYouA.contactEmail='$email' AND locationIdeas.contactEmail='$email' AND involved.contactEmail='$email' AND contact.contactEmail='$email';");
var_dump($result);
$statistics = $result->fetch_array(MYSQLI_ASSOC);
foreach ($statistics as $k => $v) {
    echo $k . ' : ' . $v;
}

The Error Message: Fatal error : Call to a member function fetch_array() on a non-object in C:\\xampp\\htdocs\\otherrealmhomepageredesign\\serversidestuff\\SurveySubmission.php on line 41 错误消息: 致命错误 :在第41行的C:\\ xampp \\ htdocs \\ otherrealmhomepageredesign \\ serversidestuff \\ SurveySubmission.php中的非对象上调用成员函数fetch_array()

When I do a dummy search for a test email using straight sql in MySQL Workbench, I get the results I am looking for. 当我在MySQL Workbench中使用直接sql对测试电子邮件进行虚拟搜索时,我得到了想要的结果。

Straight SQL: 直接SQL:

SELECT distinct *
FROM interests, equipment, classes, housing, newSkills, selfIdentity, areYouA, locationIdeas, involved, contact 
WHERE interests.contactEmail='bob@t.qdTT' AND equipment.contactEmail='bob@t.qdTT' AND classes.contactEmail='bob@t.qdTT' AND housing.contactEmail='bob@t.qdTT' AND newSkills.contactEmail='bob@t.qdTT' AND 
selfIdentity.contactEmail='bob@t.qdTT' AND areYouA.contactEmail='bob@t.qdTT' AND locationIdeas.contactEmail='bob@t.qdTT' AND involved.contactEmail='bob@t.qdTT' AND contact.contactEmail='bob@t.qdTT';

Results: 结果:

'bob@t.qdTT', NULL, NULL, 'on', 'on', NULL, NULL, 'dbf gbcvb', 'bob@t.qdTT', NULL, NULL, NULL, 'on', NULL, NULL, NULL, NULL, 'on', 'dfxbv ', 'bob@t.qdTT', 'on', NULL, NULL, 'on', NULL, NULL, NULL, NULL, NULL, 'erdgettrf', 'bob@t.qdTT', 'yes', 'rtfhthtrfgh', 'bob@t.qdTT', 'no', 'ehfnsrtehtertdf', 'bob@t.qdTT', 'other', 'dherdgdfxbdfb', 'bob@t.qdTT', 'on', 'on', 'on', 'on', 'on', 'on', 'on', 'on', 'on', 'on', 'on', 'on', 'on', 'rhgergbdbgdfbdf', 'bob@t.qdTT', 'sfgdregbegerg', 'bob@t.qdTT', 'no', 'aegsdgdr', 'estgdfbdfxbdfx ', 'bob@t.qdTT', 'Asghd', '378767'

This is a known feature / bug with mysqli. 这是mysqli的已知功能/错误。 You have to free all the results from the queries you've done so far before you can make more. 您必须先释放所有查询的所有结果,然后才能进行更多操作。

After this... 在这之后...

if (!($mysqli->multi_query($query))) {
    echo "Error: (" . $mysqli->errno . ") " . $mysqli->error;
}

Try... 尝试...

while($mysqli->more_results())
{
    $mysqli->next_result();
    $discard = $mysqli->store_result();
}

While INSERT and UPDATE don't return result sets, they do return statuses that will be considered results to mysqli 尽管INSERTUPDATE不返回结果集,但它们确实返回将被视为mysqli结果的状态

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

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