0

I have written three queries similarly and they all run perfectly in PHPMyAdmin but only two run well in PHP. I've been at this for hours. Any help would be appreciated.

This is the code

//Get student ID
$id = 441;

//Get student name
$sql = "SELECT name FROM Student where id=$id";
$result=mysqli_query($connect, $sql);
$rows = $result->fetch_assoc();
$name = $rows['name'];

//get fees id
$sql1 = "SELECT fees_id FROM fees_info,Student where fees_info.student_id = Student.id and id=$id";
$result1=mysqli_query($connect, $sql1);
$rows1 = $result1->fetch_assoc();

// get statement info
$sql2="SELECT date,time,description,credit,debit,balance FROM finances WHERE fees_id = $fees_id";
$result2=mysqli_query($connect, $sql2);
$rows2= $results2->fetch_assoc();                
$date = $rows2['date'];
$time = $rows2['time'];
$description = $rows2['description'];
$credit = $rows2['credit'];
$debit = $rows2['debit'];
$balance = $rows2['balance']; 

With getting the statement info, var dump returns null on the result and U don't understand why because the first two are running in the same file which is connected to a database

  • 3
    (Possible) side note: Do not use string interpolation or concatenation to get values into SQL queries. That's error prone and might make your program vulnerable to SQL injection attacks. Use parameterized queries. See ["How to include a PHP variable inside a MySQL statement"](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) and ["How can I prevent SQL injection in PHP?"](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – sticky bit Feb 11 '21 at 23:57
  • If there are no rows `$mysqli_result->fetch_assoc()` returns `null`. By the way `$connect->query($sql);` will save you typing... Of course, you need to use prepared statements like @stickybit pointed out. – StackSlave Feb 12 '21 at 00:14
  • 2
    Also the `,` join syntax in that second query has been out of recommended use for decades already – Charlieface Feb 12 '21 at 00:15
  • Like @Charlieface way saying: `fees_info,Student where` should be `fees_info JOIN Student ON`. – StackSlave Feb 12 '21 at 00:18
  • There are values in the table and thank you for the education @StackSlave – Papa Darfoor Feb 12 '21 at 00:20
  • i just transferred the query into a single file and ran var dump on it and got bool(false). Help – Papa Darfoor Feb 12 '21 at 00:28
  • 1
    Fixed it! i created a variable called $result2 and called $results2 after. – Papa Darfoor Feb 12 '21 at 00:34

0 Answers0