0

I am using the below code to query an pull Category and Stock of the first 30 Product ID that are store into the $IDs array in PHP.

//Category
for ($n = 0; $n < 30; $n++) {
    $ID = $IDs[$n];
    $result = mysql_query("SELECT Category FROM Products where Code= '$ID'", $link);
    if (!$result) {
        die("Database query failed: " . mysql_error());
    }
    while ($row = mysql_fetch_array($result)) {
       $productCategory[$n] = $row["Category"];
    }
}
//Stock
for ($n = 0; $n < 30; $n++) {
    $ID = $IDs[$n];
    $result = mysql_query("SELECT Stock FROM Products where Code= '$ID'", $link);
    if (!$result) {
        die("Database query failed: " . mysql_error());
    }
    while ($row = mysql_fetch_array($result)) {
       $productStock[$n] = $row["Stock"];
    }
}

Is there a way to query and pull both Category and Stock with the same SELECT so i can minimise the queries to mySQL DB to only 30 not 60?

1
  • So not use mysql_* functions. These api is depricated. Use mysqli_* functions or pdo and prepared statement. Commented Feb 13, 2015 at 19:16

1 Answer 1

3

SELECT Stock, Category FROM Products WHERE Code= '$ID'

Please note that the mysql_* methods have been deprecated in PHP due to their vulnerability to SQL injection. You should use either PDO or mysqli functions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i know is deprecated but i dont know how to convert it to PDO, i will start learn .. Thanks again for the answer btw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.