Query Failed

+1 Hammad Ali Khan · July 6, 2014
every time query is failed although i am connected to database... please sought out the problem..


Database.php:

<?php


$mysql_host='localhost';
$mysql_user='root';
$mysql_pass='';
$mysql_db='user';
if(!@mysql_connect($mysql_host,$mysql_user,$mysql_pass) !@mysql_select_db($mysql_db) ){
echo 'not connected';}




?>


dbindex1.php:
<?php 
require 'database.php';
$query="SELECT  'food' ,'calories' FROM 'food' ORDER BY 'id' ";
if ($query_run=mysql_query($query)){
echo 'Query success';
}
else 
{ echo 'Query failed';}

?>


Post a Reply

Replies

Oldest  Newest  Rating
+1 James Grimshaw · July 14, 2014
Hi,

Here where you are doing the if statement, you are asking if $query_run is equal to mysql_query($query). $query_run doesn't exist yet, as you are just creating that variable in the if statement. Therefore, you will be returned false, as mysql_query($query) equals a value, but $query_run just equals null, as it has no value.

The best thing to try would be to take $query_run=mysql_query($query); out of the if statement, and just put $query_run in the if statement. This will now check whether the query has run or not, rather than comparing two values.

<?php 
require 'database.php';
$query="SELECT  'food' ,'calories' FROM 'food' ORDER BY 'id' ";
$query_run=mysql_query($query);
if ($query_run){
echo 'Query success';
}
else
{ echo 'Query failed';}

Hope this helps.
0 Glenn Cook · January 28, 2016
Php is now mysqli, and are you using back ticks in your query statement (`)
  • 1

PHP

107,067 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators