SQLSTATE[HY000] [2002] No such file or directory

[Website URL] : (https://seguidoresvip.wuaze.com)

https://seguidoresvip.wuaze.com

Erro mensage

SQLSTATE[HY000] [2002] No such file or directory

Other Information

I’m very noob at solving this problem, please explain it to me calmly, I have an SQL file that I uploaded to my database where I use a database from a website belonging to a friend of mine, I know the problem has to do with localhost/host but I don’t know how solve

This article should help:

If not, can you share the PHP code that makes the connection to the MySQL database without including sensitive parts like your password?

6 Likes

How do I know which php file connects to the MYSQL database?

Have you coded the software yourself, or are you using a script?

Visiting your website, I get an HTTP Error 500 now, activating Display Errors and any other needed settings should show where the issue is:

4 Likes

I didn’t write the code, I just copied it from my friend

I already solved http error 500

now it’s giving
SQLSTATE[HY000] [2002] Connection timed out

If you can’t figure out which file makes the connection, you can always ask your friend :slight_smile:
Alternatively, you could share your folder structure (the files inside your htdocs folder) so we can make a wild guess, but I’d recommend asking the original author for help since no one knows the code better than the one who wrote it.

5 Likes

So I already solved the problem but now I have another problem lol

Go to my website so you can view it and help me solve it, please.

erro
/
Fatal error : Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /home/vol19_1/infinityfree.com/if0_35396083/htdocs/system/glyconFunc/c3636f0afa193b189ba0e840c86b1c35.php:269 Stack trace: #0 /home/vol19_1/infinityfree.com/if0_35396083/htdocs/system/glyconFunc/c3636f0afa193b189ba0e840c86b1c35.php(269): PDOStatement->execute(Array) #1 /home/vol19_1/infinityfree.com/if0_35396083/htdocs/ProffessorwithAST.php(194): countRow(Array) #2 /home/vol19_1/infinityfree.com/if0_35396083/htdocs/index.php(10): require_once(‘/home/vol19_1/i…’) #3 {main} thrown in /home/vol19_1/infinityfree.com/if0_35396083/htdocs/system/glyconFunc/c3636f0afa193b189ba0e840c86b1c35.php on line 269

Replied in duplicate post.

4 Likes

Website URL

(https://seguidoresvip.wuaze.com)

Error Message

Fatal error : Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /home/vol19_1/infinityfree.com/if0_35396083/htdocs/system/glyconFunc/c3636f0afa193b189ba0e840c86b1c35.php:269 Stack trace: #0 /home/vol19_1/infinityfree.com/if0_35396083/htdocs/system/glyconFunc/c3636f0afa193b189ba0e840c86b1c35.php(269): PDOStatement->execute(Array) #1 /home/vol19_1/infinityfree.com/if0_35396083/htdocs/ProffessorwithAST.php(194): countRow(Array) #2 /home/vol19_1/infinityfree.com/if0_35396083/htdocs/index.php(10): require_once(‘/home/vol19_1/i…’) #3 {main} thrown in /home/vol19_1/infinityfree.com/if0_35396083/htdocs/system/glyconFunc/c3636f0afa193b189ba0e840c86b1c35.php on line 269

Other Information

I can’t solve it, I’m a noob in these matters

Hi Sixxxz,

When using PDO statements, make sure you have provided enough parameters on the prepare statement. If your SQL has 4 spots for filling in data, you must provide exactly 4 parameters after.

Cheers!

4 Likes

How can I do this? sorry I don’t know anything about

Is there any tutorial you can give me?

Hi Sixxxz,

Please share your SQL prepare statement here as PHP code and we can take a look.
You need not share the part where you create the connection.

Cheers!

3 Likes

c3636f0afa193b189ba0e840c86b1c35.php (27.3 KB)
index.php (228 Bytes)
ProffessorwithAST.php (15.5 KB)

I don’t know what to send you because I didn’t understand your question very well but I sent you the files described in the problem

Fatal error : Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /home/vol19_1/infinityfree.com/if0_35396083/htdocs/system/glyconFunc/c3636f0afa193b189ba0e840c86b1c35.php:269 Stack trace: #0 /home/vol19_1/infinityfree.com/if0_35396083/htdocs/system/glyconFunc/c3636f0afa193b189ba0e840c86b1c35.php(269): PDOStatement->execute(Array) #1 /home/vol19_1/infinityfree.com/if0_35396083/htdocs/ProffessorwithAST.php(194): countRow(Array) #2 /home/vol19_1/infinityfree.com/if0_35396083/htdocs/index.php(10): require_once(‘/home/vol19_1/i…’) #3 {main} thrown in /home/vol19_1/infinityfree.com/if0_35396083/htdocs/system/glyconFunc/c3636f0afa193b189ba0e840c86b1c35.php on line 269

Hi Sixxxz,

The code in question is in /home/vol19_1/infinityfree.com/if0_35396083/htdocs/system/glyconFunc/c3636f0afa193b189ba0e840c86b1c35.php from lines 254 to 272.

The original function is as follows:

function countRow($data)
{
    global $conn;
    $where = "";
    if ($data["where"]) {
        $where = "WHERE ";
        foreach ($data["where"] as $key => $value) {
            $where .= " " . $key . "=:" . $key . " && ";
            $execute[$key] = $value;
        }
        $where = substr($where, 0, -3);
    } else {
        $execute[] = "";
    }
    $row = $conn->prepare("SELECT * FROM " . $data["table"] . " " . $where . " ");
    $row->execute($execute);
    $validate = $row->rowCount();
    return $validate;
}

This function attempts to dynamically build a SQL PDO query based on a array of parameters, execute the query and return the row counts.

In this case we have to understand how the query is built.

The code attempts to build the query by first preparing the placeholders into a $where string and then appends to the master command in the prepare statement. However in PHP PDO, we need to bindParam after preparing the statement but before executing it, meaning there’s one more step to bind the params using the array again before executing the SQL command, this way you have filled in all the blanks.

With that in mind, please inspect this preliminary version:

<?php

function countRow($data)
{
    global $conn;
    $where = '';
    $execute = [];
    if ($data['where']) {
        $where = 'WHERE ';
        foreach ($data['where'] as $key => $value) {
            $where .= ' ' . $key . '=:' . $key . ' && ';
        }
        $where = substr($where, 0, -3);
    }
    $row = $conn->prepare('SELECT * FROM ' . $data['table'] . ' ' . $where . ' ');
    foreach ($execute as $key => $value) {
        $conn->bindPAram(':' . $key, $value);
    }
    $row->execute();
    $validate = $row->rowCount();
    return $validate;
}

Here we added the part to do bindParam based on $execute (the variable you put all values in) and run $row->execute() correctly.

However there are room for improvements still, and we can tidy the code up a bit to have a cleaner syntax, better handling, type hinting and string formatting:

<?php

function countRow(array $data = []):int
{
    // get connection
    global $conn;

    // early escape if table is not specified.
    if(!array_key_exists('table', $data)){
        return 0;
    }
    
    // trim the table variable
    $table = trim($data['table']);

    // early escape if table name is empty.
    if(empty($data['table'])){
        return 0;
    }

    // prepare the where statement if the where part exists in data.
    $where = '';
    if(array_key_exists('where',  $data) && sizezof($data['where']) > 0){
        $keys = array_keys($data['where']);
        $where = implode(' ', array_map(function ($e) {
            return sprintf('%s =: %s', $e, $e);
        }, $keys));
        $where = sprintf('WHERE %s', $where);
    }

    // tell sql to do the counting instead of having PHP to do,
    // the count(1) method makes this run fastest as it counts 1s instead of the whole record.
    $row = $conn->prepare(trim(sprintf('SELECT COUNT(1) AS c FROM %s %s', $table, $where)));
    if(!empty($where)){
        foreach ($data['where'] as $key => $value) {
            $conn->bindPAram(sprintf(':%s', $key), $value);
        }
    }

    // error handling with try catch
    try{
        // run the SQL
        $row->execute();
        // the count is in the first result c
        return $row->fetch_results_assoc()[0]->c;
    }catch(\Exception $e){
        return -1;
    }
}

Cheers!

3 Likes

Hello, excuse my ignorance, is the last code you sent and the code already solved the problem?

If so, where do I put it?

Hi Sixxxz,

You can replace the part exactly where it’s from.

Cheers!

2 Likes

thank you

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.