php - Database Query setup

649

The DB columns are |name|userID|lastname|UserPass|age|gender| i am building a query to select multiple results in one request

   global $wpdb;

   $res = $wpdb->get_results( $wpdb->prepare(
        "SELECT name, lastname
         FROM datatable
         WHERE userID in ( %d , %d)
         AND
         UserPass in ( %d, %d  )",
         array(
            $val[1],
            $val[2],
            $val[3],
            $val[4],
         )

        ),
        ARRAY_A
    );

where $val[1] and $val[2] should be uset as userID, and $val[3] and $val[4] UserPass. And i think i have problem with ordering the placeholder values and that the query is not setup properly, could use a word of advice.

288

Answer

Solution:

Try with this :

 $querystr = "
    SELECT name, lastname
         FROM datatable
         WHERE userID in ('your user ids implode with commas')
         AND
         UserPass in ('password string implode with commas ')
 ";

 $result = $wpdb->get_results($querystr, OBJECT);
263

Answer

Solution:

This query returns all the users that match the given passwords.

<?php
    global $wpdb;

   $res = $wpdb->get_results( $wpdb->prepare(
        "SELECT name, lastname, UserPass
         FROM datatable
         WHERE userID in ( %d , %d)
         HAVING UserPass in ( %d, %d  )",
         array(
            $val[1],
            $val[2],
            $val[3],
            $val[4],
         )

        ),
        ARRAY_A
    );
?>

Maybe this helps?

People are also looking for solutions to the problem: PHP: Evaluate string as logical expression and return true or false

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.