0

I have come to the following problem. My code is like this:

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
mysql_select_db($mysql_database,$dbhandle); 

$today = date('Y-m-d');
$queryfirst = "SELECT * FROM `database`.`sm2014` WHERE pvm='$today'";
echo $queryfirst;
$result = mysql_query("$queryfirst")|| die(mysql_error()); 


//fetch tha data from the database
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $queryfirst;
    die($message);
}
if($result === FALSE) {
    die(mysql_error()); // TODO: better error handling
}
$num_rows = mysql_num_rows($result);

My output on page is:

Connected to MySQL
SELECT * FROM `database`.`sm2014` WHERE pvm='2014-12-09'
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in            /*path*/2014/index.php on line 86

So, i get no Error, i tried to copy-paste-run mysql query that is generated by php in PhpMyAdmin and it works perfectly. I have tried different queries:

SELECT * FROM `database`.`sm2014` WHERE pvm='2014-12-09'
SELECT * FROM `database`.`sm2014` WHERE pvm=2014-12-09
SELECT * FROM sm2014 WHERE pvm='2014-12-09'
SELECT * FROM `sm2014` WHERE pvm='2014-12-09'

And also different combinations of them, still it doesnt work. I have tried just put in php page this query:

$queryfirst = "SELECT * FROM `database`.`sm2014` WHERE pvm='2014-12-09'";

and it also gives error. In database pvm is DATE type.

Honza Zidek
  • 8,097
  • 4
  • 63
  • 97
Andrey
  • 1

4 Answers4

1

I think your error is here :

$result = mysql_query("$queryfirst")|| die(mysql_error()); 

try it without the "" like so :

 $result = mysql_query($queryfirst) || die(mysql_error());
Dyon
  • 131
  • 7
  • Was so before. But i have tried and no help. – Andrey Dec 09 '14 at 11:15
  • try putting database and sm2014 in [], also instead of WHERE pvm='$today'"; try WHERE pvm=' " . $today . " ' "; – Dyon Dec 09 '14 at 11:31
  • SELECT * FROM [database.sm2014] WHERE pvm='2014-12-09' SELECT * FROM [database].[sm2014] WHERE pvm='2014-12-09' SELECT * FROM [`database`.`sm2014`] WHERE pvm='2014-12-09' SELECT * FROM [`database`].[`sm2014`] WHERE pvm='2014-12-09' All 4 says: You have an error in your SQL syntax – Andrey Dec 09 '14 at 11:37
1

You should use operator or instead of || here:

$result = mysql_query("$queryfirst")|| die(mysql_error());

It is because of the operator precedence. "||" has a greater precedence than "=", , while "or" has lower precedence than "=".

So when you doing like this

$r = exp1 || exp2;

it will act like

$r = ($exp1 || $exp2);

and the result will be always boolean.

When you doing like this

$r = exp1 or exp2;

it will act like

($r = $exp1) or $exp2;

Taalaibek M
  • 111
  • 4
  • Strange but... it worked. And this $result = mysql_query("$queryfirst")|| die(mysql_error()); code is from some Stackoverflow topic and it also worked! – Andrey Dec 09 '14 at 11:47
0

Choose Database connection when running mysql query:

$result = mysql_query($queryfirst,$dbhandle)|| die(mysql_error());

See example on PHP.net

Mysteryos
  • 5,413
  • 2
  • 28
  • 50
  • Inserts without choosing connection in other scripts works fine. Tried with choosing connection and still getting same error. – Andrey Dec 09 '14 at 11:16
0

I dont know how or why its happened, but member "Taalaibek M" was right. Error was in "or" operator, i used "||" and should be "or". I hope that someone with reputation can give some vote for Taalaibek M. Thank you very much all, case closed.

Andrey
  • 1