mysqli queries not returning results inside function

Th3lmuu90

I have several php functions which serve different purposes. Each of them is executing a specified mysqli query yet only the first query returns results.

Here is my code

function setAsNonWorkingProxy(){

    GLOBAL $mysqli, $proxy;

    $port = explode(":",$proxy)[1];
    $proxy = explode(":",$proxy)[0];

    if ($sql2 = $mysqli->prepare("UPDATE proxies SET failed=failed+1 WHERE ip=? AND port=?")) {

        $sql2->bind_param("si",$proxy, $port);

        $sql2->execute();

        $sql2->close();

    }
}

function markProxyAsUsed(){

    GLOBAL $mysqli, $proxy;

    $port = explode(":",$proxy)[1];
    $proxy = explode(":",$proxy)[0];

    if ($sql3 = $mysqli->prepare("UPDATE proxies SET tried=tried+1 WHERE ip=? AND port=?")) {

        $sql3->bind_param("si",$proxy, $port);

        $sql3->execute();

        $sql3->close();
    }
}

setAsNonWorkingProxy();
markProxyAsUsed();

When I do this, only the first function that is called, executes the query inside. The second function is called and the query executed without any errors yet nothing happens. I am using UDPDATE queries inside both functions.

If I change the order of the functions:

markProxyAsUsed();
setAsNonWorkingProxy();

Again, only the first one works. Why is this happening?

bulforce

The problem is that you are bringing the global scope variable in your function. Then using explode on the $proxy and the result is stored back into $proxy(remember thats the same global var) what happens is that the second function called is now working with the modified $proxy variable. It doesn't even matter if you call the same function two times, it will fail the second time.

function markProxyAsUsed(){

    GLOBAL $mysqli, $proxy; //You are bringing global scope vars in here


    // If $proxy is something like '222.222.222.222:8080' on your first call
    // On the second call will be only '222.222.222.222' and your logic will be broken
    $port = explode(":",$proxy)[1];
    $proxy = explode(":",$proxy)[0]; //Here is where you are messing it
    //Quickfix
    $ip = explode(":", $proxy)[0]; // then use $ip in your query

    //Proper fix would be to declare the function as so function markProxyAsUsed($proxy) 
    //and pass it when you call the function.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Mysqli select multiple queries

분류에서Dev

filter function ng-repeat returning true but 0 results are displayed

분류에서Dev

Php mysqli gives no results

분류에서Dev

conditional statements to run mysqli queries

분류에서Dev

Page not returning results

분류에서Dev

Error when trying to run multiple mysqli queries

분류에서Dev

Looping MySQL Results With Additional Queries

분류에서Dev

php - MySQli row count always returning 0?

분류에서Dev

Why is Scrapy returning duplicate results?

분류에서Dev

Returning object oriented json from sparql queries

분류에서Dev

mysqli_fetch_array not showing results correct

분류에서Dev

PHP function not returning value

분류에서Dev

Function returning pointer to string

분류에서Dev

How to combine the results of two different queries with mongoose?

분류에서Dev

Add to Dictionary Query Results of Multiple Linq Queries

분류에서Dev

Combine results of multiple queries in Oracle Sql

분류에서Dev

Pointer to function returning function pointer

분류에서Dev

Mysqli - Are parameterized queries enough for preventing XSS second order attacks?

분류에서Dev

Handle queries inside loop in node.js

분류에서Dev

create php function for different queries

분류에서Dev

security of a function that queries a sql database

분류에서Dev

MySQL Full Text Search Returning 0 Results

분류에서Dev

Creating UTC time in momentjs is returning weird results

분류에서Dev

scraper only returning results for first 2 inputs

분류에서Dev

ElasticSearch Ruby returning results without ElasticSearch running

분류에서Dev

nslookup returning fake results for one particular domain

분류에서Dev

Internet category not returning any results in Synapse

분류에서Dev

Returning a pointer to a structure from function

분류에서Dev

Returning a value from a recursive function

Related 관련 기사

뜨겁다태그

보관