MySQL selecting multiple data values

Started by srishtimehta, Feb 06, 2023, 03:01 AM

Previous topic - Next topic

srishtimehtaTopic starter

Hello, could you assist me in resolving an issue? I have a database query that uses two variables: $get_num = "3,4" and $one = "1". The query selects rows from the 'cards' table with a 'first_pay' value greater than or equal to 1 and with a 'manager_slc' value either 3 or 4. The current result is that there are two rows selected. However, if I swap the values of $get_num to "4,3", I get four rows returned. This indicates that the query is functioning as if it were 'manager_slc = ?' instead of 'manager_slc IN (?)', so only one value is being selected instead of both.

To resolve this issue and obtain a result of six rows, I need to modify the query to select both values 3 and 4 from the database.

$get_num = "3,4";
$one = "1";
 
$stmt = $mysqli->prepare("SELECT first_pay FROM `cards` WHERE first_pay >=? AND manager_slc IN (?)");
$stmt->bind_param("ss", $one,$get_num);
$stmt->execute();
$res = $stmt->get_result();
$row_cnt = mysqli_num_rows($res);

  •  

sumoncps

If you require a prepared query, you can use the IN (?, ?, etc.) syntax and pass the appropriate number of parameters. To pass a variable number of parameters in an array to a function, you can use a custom function such as wquery($query, $types, array $params). For parameters of the same type (type s), you do not need to specify $types. You can determine the value of the first parameter for bind_param() based on the length of the $params array or even take into account the actual types of elements in the $params array.

function wquery($query, $types, array $params)
  •  

hvye8gip

It seems like the issue is related to how you're passing the values for the 'manager_slc' column in your SQL query. Currently, you're using a single string value for the 'bind_param' function, which is causing the query to treat it as a single value instead of multiple values. To resolve this, you can modify your code as follows:

```
$get_num = "3,4";
$one = "1";

// Split the $get_num string into an array of values
$get_num_array = explode(',', $get_num);

// Create a string with placeholders for each value in $get_num_array
$placeholders = str_repeat('?,', count($get_num_array) - 1) . '?';

// Build the query with the correct number of placeholders based on the number of values in $get_num_array
$query = "SELECT first_pay FROM cards WHERE first_pay >= ? AND manager_slc IN ($placeholders)";

// Create the prepared statement
$stmt = $mysqli->prepare($query);

// Bind the parameters dynamically using call_user_func_array
$params = array_merge(array($one), $get_num_array);
$types = str_repeat('s', count($params)); // Assuming all parameters are strings
$stmt->bind_param($types, ...$params);

// Execute the query
$stmt->execute();

// Get the result
$res = $stmt->get_result();
$row_cnt = mysqli_num_rows($res);
```

This code will dynamically build the SQL query based on the number of values in the `$get_num` array and bind the parameters correctly. It should give you the desired result of selecting both values 3 and 4 from the database.
  •