Hack Forums
[PHP] [HELP] Parse error: syntax error, unexpected ' while' (T_STRING) in C:\AppServ\w - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] [HELP] Parse error: syntax error, unexpected ' while' (T_STRING) in C:\AppServ\w (/showthread.php?tid=5925858)



[HELP] Parse error: syntax error, unexpected ' while' (T_STRING) in C:\AppServ\w - blackcoder00 - Jan 4, 2019

hi guys when i execute my script i got this problem message
Parse error: syntax error, unexpected '            while' (T_STRING) in C:\AppServ\www\khalil\ser.php on line 19
PHP Code

<?php

mysql_connect
("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
 
   /*
        localhost - it's location of the mysql server, usually localhost
        root - your username
        third is your password
         
        if connection fails it will stop loading the page and display an error
    */
 
    
    mysql_select_db
("mat") or die(mysql_error());
 
   /* tutorial_search is the name of database we've created */
 
    $query = $_GET['query']; 
 
$raw_results = mysql_query("SELECT * FROM mat WHERE ref='$query' ");

if(
mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
             
            while
($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
             
                echo 
"<p><h3>".$results['ref']."</h3>".$results['designation']."</p>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }
             
        
}
        
         
    
    
?>



RE: [HELP] Parse error: syntax error, unexpected ' while' (T_STRING) in C:\AppServ\w - Abruptum - Jan 4, 2019

(Jan 4, 2019 07:53 AM)blackcoder00 Wrote:  hi guys when i execute my script i got this problem message
Parse error: syntax error, unexpected ' while' (T_STRING) in C:\AppServ\www\khalil\ser.php on line 19
PHP Code

<?php

mysql_connect
("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
    
/*
        localhost - it's location of the mysql server, usually localhost
        root - your username
        third is your password
         
        if connection fails it will stop loading the page and display an error
    */
     
    
mysql_select_db("mat") or die(mysql_error());
    
/* tutorial_search is the name of database we've created */
     
$query = $_GET['query']; 
 
$raw_results = mysql_query("SELECT * FROM mat WHERE ref='$query' ");

if(
mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
             
            
while($results = mysql_fetch_array($raw_results)){
            
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
             
                
echo "<p><h3>".$results['ref']."</h3>".$results['designation']."</p>";
                
// posts results gotten from database(title and text) you can also show id ($results['id'])
            
}
             
        }
        
         
    
    
?>

well i formated the code and it works.
Try this :
PHP Code

<?php
$conn 
= new mysqli("localhost", "root", "", "mat");
if (
$conn->connect_error) {
    die(
"Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("SELECT * FROM mat WHERE ref=?");
$stmt->bind_param('s', $_GET['query']);
if (
$stmt->execute()) {
    
$result = $stmt->get_result();
    while(
$row = $result->fetch_assoc()) {
        echo 
"<p><h3>{$row['ref']}</h3>{$row['designation']}</p>";
    }
}
?>
i think this is a charset error what do you have select UTF-8 ?

PS always use prepared statments.


RE: [HELP] Parse error: syntax error, unexpected ' while' (T_STRING) in C:\AppServ\w - Disease - Jan 4, 2019

(Jan 3, 2019 09:39 PM)Truth3356 Wrote:  Well yeah obviously, thank you for the advice

(Jan 4, 2019 07:53 AM)blackcoder00 Wrote:  hi guys when i execute my script i got this problem message
Parse error: syntax error, unexpected '            while' (T_STRING) in C:\AppServ\www\khalil\ser.php on line 19
PHP Code

<?php

mysql_connect
("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
 
   /*
        localhost - it's location of the mysql server, usually localhost
        root - your username
        third is your password
         
        if connection fails it will stop loading the page and display an error
    */
 
    
    mysql_select_db
("mat") or die(mysql_error());
 
   /* tutorial_search is the name of database we've created */
 
    $query = $_GET['query']; 
 
$raw_results = mysql_query("SELECT * FROM mat WHERE ref='$query' ");

if(
mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
             
            while
($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
             
                echo 
"<p><h3>".$results['ref']."</h3>".$results['designation']."</p>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }
             
        
}
        
         
    
    
?>

Sorry, but this code isn't worth salvaging. It appears you've taken it from an incredibly old tutorial or Stack Overflow question. Either way, it's using completed deprecated code by modern standards. It's also riddled with security flaws due to directly injecting user input into the query. Whatever resource you're using, please stop. It's not worth your time.

Start with PHP's MySQLi extension.


RE: [HELP] Parse error: syntax error, unexpected ' while' (T_STRING) in C:\AppServ\w - jumbo9900 - Jan 6, 2019

Dnt use mysqli or mysql both are prone to sql injection and other issues

try this code below

PHP Code

<?php 
//ini_set('display_errors', 1);  //uncomment to see errors if any
//ini_set('display_startup_errors', 1); //uncomment to see errors if any
//error_reporting(E_ALL);      //uncomment to see errors if any  
$user = "root";
$pass = '';
try {
   $dbh = new PDO('mysql:host=localhost;dbname=mat', $user, $pass);
}

catch (
PDOException $e) {
   print "Error!: " . $e->getMessage() . "<br/>";
   die();
} 
 
$query 
= $_GET['query'];
$stmt = $dbh->prepare("SELECT * FROM mat WHERE ref=?"); 
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute([$query]);
$count = $stmt->rowCount();

if (
$count >= 1) {

while (
$result = $stmt->fetch()) {

echo 
"<p><h3>".$result['ref']."</h3>".$result['designation']."</p>";

}

} 

$dbh = null;  //Close DB
?>


cheers


RE: [HELP] Parse error: syntax error, unexpected ' while' (T_STRING) in C:\AppServ\w - Abruptum - Jan 6, 2019

(Jan 6, 2019 02:35 AM)jumbo9900 Wrote:  Dnt use mysqli or mysql both are prone to sql injection and other issues

try this code below

PHP Code

<?php 
//ini_set('display_errors', 1);  //uncomment to see errors if any
//ini_set('display_startup_errors', 1); //uncomment to see errors if any
//error_reporting(E_ALL);      //uncomment to see errors if any  
$user = "root";
$pass = '';
try {
   
$dbh = new PDO('mysql:host=localhost;dbname=mat', $user, $pass);
}

catch (
PDOException $e) {
   print 
"Error!: " . $e->getMessage() . "<br/>";
   die();
}  
$query = $_GET['query'];
$stmt = $dbh->prepare("SELECT * FROM mat WHERE ref=?"); 
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute([$query]);
$count = $stmt->rowCount();

if (
$count >= 1) {

while (
$result = $stmt->fetch()) {

echo 
"<p><h3>".$result['ref']."</h3>".$result['designation']."</p>";

}

} 

$dbh = null;  //Close DB
?>


cheers

Since when is mysqli considered as unsecure ?
What makes pdo so different ?


RE: [HELP] Parse error: syntax error, unexpected ' while' (T_STRING) in C:\AppServ\w - Nachfolger - Jan 6, 2019

(Jan 6, 2019 02:35 AM)jumbo9900 Wrote:  Dnt use mysqli or mysql both are prone to sql injection and other issues

try this code below

PHP Code

<?php 
//ini_set('display_errors', 1);  //uncomment to see errors if any
//ini_set('display_startup_errors', 1); //uncomment to see errors if any
//error_reporting(E_ALL);      //uncomment to see errors if any  
$user = "root";
$pass = '';
try {
   $dbh = new PDO('mysql:host=localhost;dbname=mat', $user, $pass);
}

catch (
PDOException $e) {
   print "Error!: " . $e->getMessage() . "<br/>";
   die();
} 
 
$query 
= $_GET['query'];
$stmt = $dbh->prepare("SELECT * FROM mat WHERE ref=?"); 
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute([$query]);
$count = $stmt->rowCount();

if (
$count >= 1) {

while (
$result = $stmt->fetch()) {

echo 
"<p><h3>".$result['ref']."</h3>".$result['designation']."</p>";

}

} 

$dbh = null;  //Close DB
?>


cheers
OP was using MySQLi with a non-prepared statement. That's unsafe, totally agree. But what makes MySQLi so worse than PDO, or more importantly, what "other issues" is MySQLi prone to?

Either back your statements with legitimate facts or GTFO. If it was purely an opinion, then so be it. Don't spread propaganda. If you opt to not back your comments, you will enjoy a neg.


RE: [HELP] Parse error: syntax error, unexpected ' while' (T_STRING) in C:\AppServ\w - blackcoder00 - Jan 6, 2019

(Jan 6, 2019 05:52 AM)Nachfolger Wrote:  OP was using MySQLi with a non-prepared statement. That's unsafe, totally agree. But what makes MySQLi so worse than PDO, or more importantly, what "other issues" is MySQLi prone to?

Either back your statements with legitimate facts or GTFO. If it was purely an opinion, then so be it. Don't spread propaganda. If you opt to not back your comments, you will enjoy a neg.

ty guys