Esmail I Esmail I - 4 months ago 11
SQL Question

what is wrong with my PHP codes?

I'm new to PHP, and now I need to send table "t1" data to my Android application. I use this PHP code for get data from table "t1":

<?php

$con = mysql_connect("xxxxxx","xxxxxx","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("xxxxxx", $con);

$i=mysql_query("select * from t1",$con);

$num_rows = mysql_num_rows($i);

$check='';
while($row = mysql_fetch_array($i))

$r[]=$row;
$check=$row['f1']

if($check==NULL)
{
$r[$num_rows]="Record is not available";
print(json_encode($r));

}
else
{
$r[$num_rows]="success";
print(json_encode($r));

}

mysql_close($con);

?>


But I get error:


Parse error: syntax error, unexpected 'if' (T_IF) in /home/u894693029/public_html/selectall.php on line 24


Fields Name: f1, f2, f3.

Answer

Missing semicolon and curly brackets, so get this one:

<?php
    $con = mysql_connect("xxxxxx", "xxxxxx", "xxxxxx");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("xxxxxx", $con);
    $i = mysql_query("select * from t1", $con);
    $num_rows = mysql_num_rows($i);
    $check = '';
    while ($row = mysql_fetch_array($i)) {
        $r[] = $row;
        $check = $row['f1']; //missing semicolon here
    }
    if ($check == NULL) {
        $r[$num_rows] = "Record is not available";
        print(json_encode($r));
    } else {
        $r[$num_rows] = "success";
        print(json_encode($r));
    }
    mysql_close($con);
?>

On top of that mysql_* functions is already depreciated, better start using mysqli_* ones or PDO with prepared statements.