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'];      
    }
}
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
john
  • 1
  • 1
  • try to print `$row1` and see if there is anything in it. – Mubin Oct 17 '19 at 20:20
  • it show mysqli_fetch_array() expects parameter 1 to be mysqli_result, when i print it. – john Oct 17 '19 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. https://dev.mysql.com/doc/refman/8.0/en/join.html – Scuzzy Oct 17 '19 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. – john Oct 17 '19 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. – Mubin Oct 17 '19 at 20:54
  • It works on database. – john Oct 17 '19 at 21:01
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Oct 25 '19 at 16:58

2 Answers2

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']; 
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
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.

  • third step it works..thank you boss.. but why i need add '' since the data type are set on varchar not the date? – john Oct 17 '19 at 21:08
  • Because you need to put quotation in your query for every non numeric datatype like varchar, string, date etc. – Zawadul Kawum Oct 18 '19 at 17:14