Ad

PHP Script Displaying A Blank Page

- 1 answer

This is my script and it's displaying a blank, I have no idea what the problem is. HELP!!!! #new to PHP

 <?php include "connection.php"; 
    // Get the ID from URL.
   if(isset($_GET['id']));
    $id = $_GET['id'];
    $query="SELECT * FROM module WHERE id= '$id'"; 
    $result= mysqli_query($m, $query); 
     while ($row = mysqli_fetch_array($result)){ 
     $title=$row['title']; 
     $level=$row['level']; 
     $credits=$row['credits'];
     $school=$row['school']; 
 echo $title. " " . $level. " " . $credits. "<br />"; 
 }    
 ?> 

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE id= 'Careers'' at line 1

Ad

Answer

One obvious problem here is that you've included an "end of statement" character being a semi-colon.

The semi-colon (if that isn't a typo), is doing just that, "ending" the statement.

if(isset($_GET['id']));
                       ^ right there.

It should be a brace { for it instead and to read as:

if(isset($_GET['id'])){

and there should be a closing brace } for that conditional statement for it.

Sidenote: The semi-colon is considered a valid character in PHP, which won't throw you an error for it, should the GET array have a value.

However, you should check for errors for the rest of your code.

Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything, as well as or die(mysqli_error($m)) to mysqli_query().

While making sure you are indeed using the MySQLi_ API to connect with (different MySQL APIs do not intermix) and that the GET array has a value.


Here's a rewrite, and assuming a successful DB connection using the MySQLi_ API for it.

<?php

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

if(isset($_GET['id'])){
   $id = $_GET['id'];
} else{
    echo "ID is not set. You need to investigate it.";
    exit; // This will stop your script, dead in its tracks.
}

$query="SELECT * FROM module WHERE id= '$id'"; 
$result= mysqli_query($m, $query) or die(mysqli_error($m)); 

 while ($row = mysqli_fetch_array($result)){ 
 $title=$row['title']; 
 $level=$row['level']; 
 $credits=$row['credits'];
 $school=$row['school']; 

    echo $title. " " . $level. " " . $credits. "<br />"; 
}    

References:


Edit:

Taken from comments:

"this is my query, $query="SELECT id, title module WHERE id= '$id'"; – user5579012 38 mins ago"

Link to that comment...

That isn't what was posted in your original question.

You posted SELECT * FROM module WHERE id= '$id'.

You have a syntax error here, being a missing comma after title.

It should read as:

$query="SELECT id, title, module WHERE id= '$id'"; 

All columns need to be seperated by commas but not the last one being module here.

Ad
source: stackoverflow.com
Ad