0
$con=mysqli_connect($localhost,$username,$password,'db');

$query = 'SELECT  `SN` FROM `list` WHERE `Floor` LIKE "LP60" AND `type`LIKE "pc"';
$result = mysqli_query($con,$query) or die(mysqli_error());

foreach ($result as $SN)
{             
    $get = mysqli_query($con,'SELECT * FROM pc WHERE pcSN LIKE '.$SN.'ORDER BY EvenID DESC LIMIT 1')

            while ($get_row = mysqli_fetch_assoc($get)) {
                echo '<tr>'; // printing table row
                echo '<td id="ID">'.$get_row[0].'</td>';
                echo '<td>'.$get_row[1].'</td>';
                echo '<td>'.$get_row[2].'</td>';
                echo '<td>'.$get_row[3].'</td>';
                echo '<td>'.$get_row[4].'</td>';
                echo '<td>'.$get_row[5].'</td>';
                echo '<td>'.$get_row[6].'</td>';
                echo '<td>'.$get_row[7].'</td>';
                echo '<td>'.$get_row[8].'</td>';
                echo '<td>'.$get_row[9].'</td>';
                echo '<td>'.$get_row[10].'</td>';
                echo '<td>'.$get_row[11].'</td>';
                echo '<td>'.$get_row[12].'</td>';
                echo '<td>'.$get_row[13].'</td>';
                echo '<td>'.$get_row[14].'</td>';
                echo'</tr>'; // closing table row

                }
}

I have tested both query are working fine, have tested to print_r($SN) as well but at the end i got Array to string conversion error, any help please

I have found the solution by using

'" . mysqli_escape_string($con,$SN) . "'

and now the new code like this

$con=mysqli_connect($localhost,$username,$password,'db');

// mysql select query
$query = 'SELECT  `SN` FROM `list` WHERE `Floor` LIKE "LP60" AND `type`LIKE "pc"';
$result = mysqli_query($con,$query) or die(mysqli_error());
while($row = mysqli_fetch_assoc($result))
{
foreach ($row as $SN){

    $sql = "SELECT * FROM pc WHERE pcSN LIKE '" . mysqli_escape_string($con,$SN) . "' ORDER BY EvenID DESC LIMIT 1";
    $get = mysqli_query($con,$sql);

                while ($get_row = mysqli_fetch_assoc($get)) {
                    echo '<tr>'; // printing table row
                    echo '<td>'.$get_row['xxx'].'</td>';
                                    .
                                    .
                                    .
                                    .
                    echo '<td>'.$get_row['yyy'].'</td>';
                    echo'</tr>'; // closing table row

}
}
}

Thanks everyone has been helping me here, i think add up all little bit to made it worls. Thanks Wish this post can help any other as well

0

3 Answers 3

3

You are trying to access the values in an associative array using numeric indexes. Use the column names instead. mysqli_fetch_assoc() returns an associative array.

Instead of this,

$get_row[1]

Try to use this,

$get_row['column_name']

Edit

As per your comment about still getting an error try to add a space between $SN and the next concatenation ORDER. This might be causing your problem since when you echo out the sql statement you have no space there.

This,

...LIKE ArrayORDER BY...

Should have been,

...LIKE Array ORDER BY...

So change your statement,

'SELECT * FROM pc WHERE pcSN LIKE '.$SN.'ORDER BY EvenID DESC LIMIT 1'

by adding a space to,

'SELECT * FROM pc WHERE pcSN LIKE '.$SN.' ORDER BY EvenID DESC LIMIT 1'
Sign up to request clarification or add additional context in comments.

3 Comments

i have changed to each column name but problem remain. When i echo the mysqli_query out and it will show like this "SELECT * FROM pc WHERE pcSN LIKE ArrayORDER BY EvenID DESC LIMIT 1" But when i print_r($SN) and var_dump($SN), both can show the individual SN
Hey , i have edited my answer to provide a possible solution. Take a look below the edit word.
unfortunately the Array to string conversion error still remain
0

Do it this way:

while ($get_sn_row = mysqli_fetch_assoc($result)) {
   $SN = $get_sn_row['SN'];
   $get = mysqli_query($con,'SELECT * FROM pc WHERE pcSN LIKE '.$SN.'ORDER BY EvenID DESC LIMIT 1');

   while ($get_row = mysqli_fetch_assoc($get)) {
        ...
   }
}

Comments

0

Instead of using

$get=mysqli_fetch_assoc...

Use

$get=mysqli_fetch_array...

Then you'll be able to get the data like this:

echo $get[0];

Comments

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.