PHP code to retrive data from Mysql database and display the retrive data

4 replies
please can some one help me on this code to identify the error and suggest what should be added or removed to make it work without error.

this is the error message:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\examSystem\ViewStudentProfile.php on line 15.

this is my code.

<html>
<head> </head>
<body>
<?php

$con=mysqli_connect("localhost","root","","online_ exam");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$result = mysqli_query($con,"SELECT FROM student WHERE Student_ID = '$_POST[Student_ID]'");

while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
echo $row['Student_ID'] . " " . $row['FName']. " " . $row['LName']. " " . $row['DOB']. " " . $row['Nationality']. $row['NoSemester']. $row['CGPA']. $row['Contact_No']. $row['Address'];
echo "<br>";
}

mysqli_close($con);

?>
</body>
</html>
#code #data #database #display #mysql #php #retrive
  • Profile picture of the author Andrew H
    Well the error pretty much explains it, there appears to be a problem with your query.

    I would suggest adding var_dump($result) after your mysql query to see what the results are, if any.

    Also, I feel nauseous when I see post data going directly into a query. Your homework is to research what SQL injection is, and how it will destroy your application. Also, google PDO mysql tutorials and start using that - you can use binded params to deal with escaping your variables AND the code is much more elegant.
    Signature
    "You shouldn't come here and set yourself up as the resident wizard of oz."
    {{ DiscussionBoard.errors[8604533].message }}
    • Profile picture of the author lloydblinks
      please i need help on this C program code it can take in input but can not display out the values that was key in some thing must be wrong with the last for-loop statement.....

      this is the code
      .................................................. ..........................
      #include <string.h>
      #include<stdio.h>
      struct PersonalData

      {
      char *name;
      int rpm;
      int housepwr;
      struct mdate
      {
      int day;
      int month;
      int year;
      }mn;

      }info[1];
      int main ()
      {


      struct PersonalData *strptr;
      strptr = info;

      int i;
      for(i = 0; i < 1; i++)
      {
      printf("Enter name :");
      scanf(" %s", &info[i].name);

      printf("Enter rpm :");
      scanf(" %d", &info[i].rpm);

      printf("Enter housepwr :");
      scanf(" %d", &info[i].housepwr);

      printf("Enter day :");
      scanf(" %d", &info[i].mn.day);

      printf("Enter month :");
      scanf(" %d", &info[i].mn.month);


      printf("Enter month :");
      scanf(" %d", &info[i].mn.year);

      printf("\n");
      printf("\n");
      }




      for(i = 0; i < 1; i++)
      {
      printf("name %d %s :", i, info[i].name);

      printf("rpm %d %d :", i, info[i].rpm);

      printf("housepwr %d %d :", i, info[i].housepwr);

      printf("day %d %d :", i, info[i].mn.day);

      printf("month %d %d :", i, info[i].mn.month);

      printf("year %d %d :", i, info[i].mn.year);

      printf("\n");
      printf("\n");
      }

      return 0;
      }
      {{ DiscussionBoard.errors[8737446].message }}
  • Profile picture of the author Andrew H
    EDIT: I assume the problem is you are only getting one result (student_id is a unique value in that table?) and then treating it as an array. PDO makes situations like this easier as well. See the code below.

    PHP Code:
    $DB_HOST 'localhost';
    $DB_DATABASE 'online_exam'//database name here
    $DB_USER 'root'//db username here
    $DB_PASSWORD ''//db password here

    //you will probably want to do a test here to check if the POST variable exists before running the query

    $DBH = new PDO("mysql:host=$DB_HOST;dbname=$DB_DATABASE",$DB_USER,$DB_PASSWORD);
    $stmt $DBH->prepare("SELECT FROM student WHERE Student_ID = :student_id");
    $stmt->bindParam(':student_id'$_POST['Student_ID']);
    $stmt->execute();
    //if you were grabbing multiple results you would use fetchAll() instead to get an array
    $student$stmt->fetch();

    //did we get any results with that sudent ID?
    if($student) {
    echo 
    $student['Student_ID'] . " " $student['FName']. " " $student['LName']. " " $student['DOB']. " " $student['Nationality']. $student['NoSemester']. $student['CGPA']. $student['Contact_No']. $student['Address'];
    echo 
    "<br>";
    } else {
    echo 
    'no student found with that id';

    The above results arent being escaped so you will want to do that as well -> PHP: htmlentities - Manual

    PHP Code:
    //yes!
    echo htmlentities($student['FName']);
    //no :(
    echo $student['FName']; 
    Signature
    "You shouldn't come here and set yourself up as the resident wizard of oz."
    {{ DiscussionBoard.errors[8604553].message }}
    • Profile picture of the author lloydblinks
      THANKS FOR HELPING ME OUT
      {{ DiscussionBoard.errors[8620960].message }}

Trending Topics