How to print one value from one variables in php

Hi guys,

I have this script in php:

require ("conn.php");

    $sql = "SELECT * FROM student_lists where mark=1";
$result = $con->query($sql);
$counter = 0;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {

$query = "SELECT * FROM  student_detail where class_id=".$row["id"]." ORDER BY id DESC LIMIT 1";
 $result2 = mysqli_query($con, $query);

	   while($row2 = mysqli_fetch_array($result2)){
          	    $rowid = $row["id"];	   

echo $rowid;

//the output of the $rowid is = 12345 

Structure of student_lists Table:
id studentName Mark
1 John 1
2 Mark 1
3 Jimmy 1
4 Carol 1
5 Sam 1
6 Josef 0
7 Karl 0

Structure of student_detail table:
id Class_id value
10 1 50
11 1 60
12 1 100
13 2 80
14 3 90
15 4 50
16 5 60
17 6 80
18 7 82

"they are 5 different values from 1 to 5 and I want to print every value separately for example want to print the first value in $rowid or the second.

Any Idea please?


Try this:

while($row2 = mysqli_fetch_array($result2)){

echo ‘<pre >’: // add line feeds
// or verbose output to show variable types
// var_dump($row2);
echo’</pre >’;

          	    $rowid = $row["id"];	   

echo $rowid;

//the output of the $rowid is = 12345 

If this is all you’re echoing from your PHP isn’t “12345” what the output would be if it echoes the results, one after the other? So wouldn’t the following instead do the job (unless I have misunderstood):

echo "<p>Student ID is $rowid </p>";

or similar, depending on how you want the output?

How do you know which one you want to print, because this will determine the best solution?

Next, don’t run queries inside of loops. This is extremely inefficient due to the compunctions needed. Use one JOIN query instead.

this only print it in line!
and I want to print one of the, like choose 1 or 4 or 5?

here is the output:

Student ID is 1
Student ID is 2
Student ID is 3
Student ID is 4
Student ID is 5

and I want to store for example the student ID 2 in a variable then print it. Which need to store every ID in separate variable.

Actually I ran the second enquiry inside the loop because I want to choose which student id from student_lists table who has mark=1 then I select student_datail from another table. so Student_details is depend on student_lists.

Any help for this…guys?

You still haven’t told us how you know which one you want to print.

You have been stating what you are trying to make work. However, this doesn’t tell us what the overall goal is, in a programming/web page context. I suspect the answer will be that you want to produce links, with each id in them, that you can then click to get more information?

Actually I want to store every value in the fetch array in separate variable for example the output “12345” need to take “1” and store it in variable $x1, “2” in variable$x2, “3” in variable$x3 and so on…

Which I need them to use it in if condition for example:

  if ($rowid==$x1 ) {
        echo   "Pass";
   if ($rowid==$x2 ) {
         echo   "Failed";

So have I understood the aim correctly? (I think you have got the above bit wrong - you are comparing the id with a score?):

You want to echo out each student’s name and then either the word “pass” or the word “fail” depending on their mark?

If so, then it can certainly be done without having a query in a loop. You run a query with a JOIN and then loop through the result to create the output, outputting “pass” or “fail” depending on the mark.

I have a couple of observations/questions, looking at your database tables. I think the “id” in the “student_lists” (primary key) corresponds with the “Class_id” in the “student_detail” table, where it is a foreign key? And the “id” in the “Class_id” table is the primary key for that table? If that id correct I would rename with column titles, to, for example, the following:

  • student_id
  • studentName
  • Mark

student_detail (Which I might retitle exam_results)

  • result_id
  • student_id
  • result

I may have misunderstood things here though.
I’m not sure what the column “Mark” is in the “student_lists” column?

I hope I am not patronising you here - if I am I apologise.
If you don’t know the meaning of “primary key” and “foreign key” then I would Google them and SQL JOINS - it will make things much clearer. I suspect things may suddenly click in your mind and you will have then have a clearer view of how to proceed.