0

How to pass value inside row array to another variable since the result of the row array I need to do another query for selection in other table. In both echo $row['Plate'] and $plateID can show the value, but for another query doesn't show the result. This is my code:

<?php

$connection=mysqli_connect("localhost","root","");
$db=mysqli_select_db($connection,'db_car_identification');

$q="SELECT Plate FROM addplate ORDER BY ID DESC LIMIT 1 ";

$result=mysqli_query($connection,$q);

while($row = mysqli_fetch_array($result)) {
    echo $row['Plate'];

    $plateID = $row['Plate'];

    echo $plateID;

    $query="SELECT StuName FROM student_detail WHERE plateID=$plateID";

    $query_run=mysqli_query($connection,$query);

    while($row1=mysqli_fetch_array($query_run)) {
        echo $row1['StuName'];      
    }
}
?>
7
  • try to print $row1 and see if there is anything in it. Commented Oct 17, 2019 at 20:20
  • it show mysqli_fetch_array() expects parameter 1 to be mysqli_result, when i print it. Commented Oct 17, 2019 at 20:24
  • Sounds like you need to learn about MySQL Joins, You'll be able to perform what you need with one query rather than with lots of little ones. dev.mysql.com/doc/refman/8.0/en/join.html Commented Oct 17, 2019 at 20:37
  • actually there have 3 table i need to join up. if i join all together, it will have 4 table need to join.That why i put single selection first and then the other 3 are join by the result of the first selection. Thanks for your idea. i will take it look .So right now i need to configure first for this idea. Commented Oct 17, 2019 at 20:50
  • echo $query="SELECT StuName FROM student_detail WHERE plateID=$plateID"; and execute that query directly on db and check if that works. Commented Oct 17, 2019 at 20:54

2 Answers 2

1

You should really be using parameterized prepared statements.

However, in your example you could simply use an SQL subquery instead. It would make your code much simpler.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = new mysqli("localhost", "root", "", 'db_car_identification');
$connection->set_charset('utf8mb4');

$result = $connection->query('SELECT StuName, Plate 
    FROM student_detail 
    WHERE plateID = (SELECT Plate FROM addplate ORDER BY ID DESC LIMIT 1)');

foreach($result as $row1){
    echo $row1['StuName']; 
}
Sign up to request clarification or add additional context in comments.

Comments

0

Please check some step

  1. For 1st Query Result which datatype is it?
  2. If it is Integer or any numeric data type then echo the $query variable and check whatever query is right or wrong by simply copy and paste it into database query builder (phpmyadmin).
  3. If it is String data type then simply add '' to your $plateID variable in query. eg:"SELECT StuName FROM student_detail WHERE plateID='$plateID'" (If not working then echo again and check your query problem)

Let me know if this help.

2 Comments

third step it works..thank you boss.. but why i need add '' since the data type are set on varchar not the date?
Because you need to put quotation in your query for every non numeric datatype like varchar, string, date etc.

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.