0

I am trying to fetch some record as below, but can't proceed. My code is below Please help to get this corrected. Here, have a variable $evaid against which I want to fetch a record from DB, but can't add this to query whoever, without adding "where" it's giving me data.

After getting data I am not able to run last part of this code If else Statement while it's directly giving the last msg as "Contact WebAdmin As Nothing Is Found !!!". I know, I did something wrong with this code, but need some help because I can't trace it out. :(

<?php
$evaid = "' + response[0].EvaluationId + '";
echo $evaid;

$dbhost = 'localhost'; 
$dbuser = 'root';
$dbpass = '123456';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
    die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM eval Where evaid=$evaid'; 

mysql_select_db('ops');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
    die('Could not get data: ' . mysql_error());
}

while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
    echo "Eva ID :{$row['evaid']}  <br> ".
        "Division: {$row['division']} <br> ".
        "Sub Type: {$row['evastype']} <br> ".
        "--------------------------------<br>";
} 

if ($row['division'] === "666" && $row['evaid'] === $evaid) {
    print '666';
} 
elseif($row['division']==="11" || $row['division']==="22" || $row['division']==="33" && $row['evaid'] === $evaid) {
    print '112222';
}
elseif($row['division']==="cc22cc" && $row['evastype']==="dd22dd" && $row['evaid'] === $evaid) {
    print 'cd123';
}
elseif($row['division']==="cc22cc" && $row['evastype']==="po1122" && $row['evaid'] === $evaid) {
    print 'c9o123';
}
elseif($row['division']==="cc22cc" && $row['evastype']==="dgd11" && $row['evaid'] === $evaid) {
    print 'cdgn666 ';
}
else { 
    print '<p style="border:2px solid MediumSeaGreen;"> Contact WebAdmin As Nothing Is Found !!! </p> ';
}

mysql_close($conn);

?>
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
A1Nasir
  • 81
  • 9
  • 1
    Don't use the old, deprecated and insecure `mysql_*`-drivers. They have been deprecated for years and completely removed in PHP 7. Use MySQLi or PDO instead. – M. Eriksson Oct 11 '18 at 12:09
  • 1
    interpolation (the replacement of variables with their values) only occurs on strings with double quotes, not single quotes. – noid Oct 11 '18 at 12:13
  • Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Nigel Ren Oct 11 '18 at 12:16
  • When you've switched to MySQLi or PDO, you should also look into [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) to protect your code form SQL injection attacks (which can turn any good day really bad... quickly) – M. Eriksson Oct 11 '18 at 12:17
  • 1
    But even if that issue gets fixed, with `$evaid = "' + response[0].EvaluationId + '";` the resulting query would become `SELECT * FROM eval Where evaid=' + response[0].EvaluationId + '` – are you trying to mix PHP and JavaScript syntax here, or what? This reads as if you are lacking a lot of basic knowledge, to be frank. – misorude Oct 11 '18 at 12:18
  • Misorude - Thanks for sharing dear, yes am new in PHP, trying to learn, not a regular user :(. Yes i am trying to mix php and javascript. – A1Nasir Oct 11 '18 at 13:04
  • _“Yes i am trying to mix php and javascript”_ - which you can not do this way … You should start by reading https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – misorude Oct 11 '18 at 13:32

1 Answers1

0

There is a few things here.

The problem with the if is that you have that outside of the while loop, so the ifs will only be checked towards the last row of the query.

But your code is also unsafe for how you use mysql. You might want to look into PDO or mysqli.

Here is the while loop as it should be

while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
    echo "Eva ID :{$row['evaid']}  <br> ".
    "Division: {$row['division']} <br> ".
    "Sub Type: {$row['evastype']} <br> ".
          "--------------------------------<br>";
    if ($row['division'] === "666" && $row['evaid'] === $evaid) {
        print '666';
    } elseif($row['division']==="11" || $row['division']==="22" || $row['division']==="33" && $row['evaid'] === $evaid) {
    print '112222';
    }
    elseif($row['division']==="cc22cc" && $row['evastype']==="dd22dd" && $row['evaid'] === $evaid) {
        print 'cd123';
    }
    elseif($row['division']==="cc22cc" && $row['evastype']==="po1122" && $row['evaid'] === $evaid) {
        print 'c9o123';
    }
    elseif($row['division']==="cc22cc" && $row['evastype']==="dgd11" && $row['evaid'] === $evaid) {
        print 'cdgn666 ';
    }
    else { 
        print '<p style="border:2px solid MediumSeaGreen;"> Contact WebAdmin As Nothing Is Found !!! </p> ';
    }
} 
Virre
  • 134
  • 1
  • 8
  • 1
    Did not read closer. Just saw a lot of bad formated $row['division'] so yeah you are right. – Virre Oct 11 '18 at 12:18
  • Hi Virre - Thanks for sharing - Yes now it's working but I am not able to pass this Value in PHP Query .. $evalid = "' + response[0].EvaluationId + '"; echo $evalid; $sql = 'SELECT * FROM eval where evaid= $evalid '; Can you please help me what was wrong - but yes when i echo this out of query it's giving me required result. – A1Nasir Oct 15 '18 at 11:37