Jump to content

Arrays selection problem.


learningprobs

Recommended Posts

Hello,

 

I am having a little problem.

 

I have created this code here:

 

 

$mysqlsearch = implode(',',$additions_result);
$res = array();
$stmt = $conn->prepare("SELECT number1,number2 FROM table_2_numbers WHERE number1 IN($mysqlsearch)");
$stmt->bind_result($number1, $number2);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows() > 0){
while ($stmt->fetch()) {
$res[] = array ('number1' => $number1, 'number2' => $number2);
}}
$stmt->free_result();
$stmt->close();

All my values are going correctly in the table, if I print_r($res), I get this:

Array ( [0] => Array ( [number1] => 1 [number2] => 4 ) [1] => Array ( [number1] => 1 [number2] => 3 ) [2] => Array ( [number1] => 1 [number2] => 2 ) [3] => Array ( [number1] => 1 [number2] => 1 ) [4] => Array ( [number1] => 1 [number2] => 0 ) [5] => Array ( [number1] => 1 [number2] => 5 ) [6] => Array ( [number1] => 1 [number2] => 6 ) [7] => Array ( [number1] => 1 [number2] => 7 ) [8] => Array ( [number1] => 1 [number2] => 8 ) [9] => Array ( [number1] => 1 [number2] => 9 ) [10] => Array ( [number1] => 2 [number2] => 0 ) [11] => Array ( [number1] => 2 [number2] => 1 ) [12] => Array ( [number1] => 2 [number2] => 2 ) [13] => Array ( [number1] => 2 [number2] => 3 ) [14] => Array ( [number1] => 2 [number2] => 4 ) [15] => Array ( [number1] => 2 [number2] => 5 ) [16] => Array ( [number1] => 2 [number2] => 6 ) [17] => Array ( [number1] => 2 [number2] => 7 ) [18] => Array ( [number1] => 2 [number2] => 8 ) [19] => Array ( [number1] => 2 [number2] => 9 ) [20] => Array ( [number1] => 3 [number2] => 0 ) [21] => Array ( [number1] => 3 [number2] => 1 ) [22] => Array ( [number1] => 3 [number2] => 2 ) [23] => Array ( [number1] => 3 [number2] => 3 ) [24] => Array ( [number1] => 3 [number2] => 4 ) [25] => Array ( [number1] => 3 [number2] => 5 ) [26] => Array ( [number1] => 3 [number2] => 6 ) [27] => Array ( [number1] => 3 [number2] => 7 ) [28] => Array ( [number1] => 3 [number2] => 8 ) [29] => Array ( [number1] => 3 [number2] => 9 ) )

I was expecting to choose for example "Array ( [number1] => 1 [number2] => 4 )" by doing this:

echo $res[0]->number1;
echo $res[0]->number2;

But when doing this I am not getting any output.

 

What am I doing wrong please?

 

Thank you in advance for your help.

 

 

 

 

 

Link to comment
Share on other sites

The arrow -> is used to access properties of an object. But you have an array, not an object. The array index syntax looks like this:

$res[0]['number1']

Your prepared statement doesn't make sense either. Instead of passing the dynamic input to parameters (which is the whole point), you insert it straight into the query string, potentially introducing SQL injection vulnerabilities.

Link to comment
Share on other sites

Hi Jacques, 

 

yes sorry, i will add the ? and bind parameters to the query, I did not even notice i forgot it.

 

Regarding the arrays, thanks. i have no idea why, since I started php, I have always struggled with arrays....I need to find a good course on this.

 

From what I understand, an object would be like the title of a group of information. But also can an object be inside another object or is it called something else?

 

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.