-4

This code is trying to get information from a table called R but I am getting an error on line 6:

Parse error: syntax error, unexpected T_LOGICAL_OR in get_R.php on line 6

I've looked it over as well as a good few of my colleagues and we cant figure it out. Here's a copy of the code:

<?php

function get_R( $s) {
(
$t = mysql_query ( $s) ) or die (mysql_error() );
if (mysql_num_rows($t) == 0) or die ("<br> No data in R<br>");


$out = "" ;
$out .=
    "<table style= \" color:red ; background:line; \" >";

/*................................*/
while ( $r = mysql_fetch_array($t) ) {
    /*.....................*/
};
    $out .= "....";
    return $out;
}
?>

Any ideas?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Renn
  • 5
  • 3
  • 1
    where the heck is `SELECT`? and what does all this do? `get_R( $s) { ( $t = mysql_query ( $s) )... while ( $r` query what? This is a terrible question. – Funk Forty Niner Feb 26 '16 at 22:08

4 Answers4

4

This line doesn't make sense:

if (mysql_num_rows($t) == 0) or die ("<br> No data in R<br>");

You can't have an if(...) or ... statement; it's not valid syntax. You're wanting to do this:

if (mysql_num_rows($t) == 0) {
    die ("<br> No data in R<br>");
}

Alternatively, you could do

mysql_num_rows($t) or die ("<br> No data in R<br>");

Also, please don't use mysql_*; the mysql_* functions are outdated, deprecated, and insecure. Use MySQLi or PDO instead.

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100
3

You should either use if or or, you shouldn't use both.

This is the usual way to write it:

if (mysql_num_rows($t) == 0) {
    die("<br> No data in R<br>");
}

But you could use:

mysql_num_rows($t) != 0 or die("<br> No data in R<br>");

Note I had to change the comparison from == to !=, because or executes the next expression when the first expression fails. I also could have changed or to and instead.

or die is usually used only when you're doing an assignment or calling a function, and you want to exit when that fails. It's not usually used when you're testing a simple condition, like this.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You have an if (condition) or die(..) What's that about?

Either use condition or die(..) or use if (condition) { .. } else { .. } Don't try combining the two syntaxes.

moopet
  • 6,014
  • 1
  • 29
  • 36
-1

Test this code:

<?php

function get_R( $s) {
$t = mysql_query ( $s) or die (mysql_error() );
if (mysql_num_rows($t) == 0) die ("<br> No data in R<br>");


$out = "" ;
$out .=
    "<table style= \" color:red ; background:line; \" >";

/*................................*/
while ( $r = mysql_fetch_array($t) ) {
    /*.....................*/
};
    $out .= "....";
    return $out;
}
?>
Daniel O.
  • 196
  • 2
  • 16