NaturalDevCR
NaturalDevCR

Reputation: 848

Query was empty error | Trying to get a specific Mysql Value

Thanks in advance for any help you could bring, I'm really noob on all coding stuffs but this is the situation:

I have a wordpress website focused on Travel Agency Tours and hotels, and I'm working on an extranet system, the idea is that hotel managers will be able to change the price of their rooms, but this extranet works totally apart of wordpress, its a php script, with a different mysql database, so, what I would like to do is that when the price is changed in the extranet, then in wordpress this is going to be also changed in real time, so I'm trying in this way:

I'm planning to insert a php snippet on wordpress editor with a mysql code to get the price from the other database for each hotel and room, so, I'm going to do this for every hotel manually, the problem becomes from the code im trying to use to get that value:

<?php
$dbhost = 'localhost';
$dbuser = 'test_user';
$dbpass = 'pass';
$dbname = 'test_db';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = mysql_query("SELECT `room_price`, FROM `hotel_room_price` WHERE price_id = '1'");


$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "Room Price :{$row['room_price']}  <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>

So the result I get in my test.php file with this code is:

"Could not get data: Query was empty"

I'm not sure why is this error, and why is not printing the value I want.

PostData: I really appologize for my english since is not my language, but I'm trying to learn.

Upvotes: 0

Views: 78

Answers (4)

arif_suhail_123
arif_suhail_123

Reputation: 2509

and try connection like this

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);

You need to remove coma from your query after room price

//You are running the query twice so use this query 

$sql = "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'";

and learn mysqli or PDO as mysql are deprciated and soon going be drop

Upvotes: 1

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

For select database use mysql_select_db() . you can't pass dbname as an args in mysql_connect()

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);

Also if you are learner then learn mysqli or PDO

Note :- mysql_* has been deprecated

you are calling query run two times so change below lines

$sql = mysql_query("SELECT `room_price`, FROM `hotel_room_price` WHERE price_id = '1'");
$retval = mysql_query( $sql, $conn );

to

$retval = mysql_query("SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'");

Upvotes: 0

user4094161
user4094161

Reputation:

<?php
$dbhost      = 'localhost';
$dbuser      = 'test_user';
$dbpass      = 'pass';
$dbname      = 'test_db';
$conn        = mysql_connect($dbhost, $dbuser, $dbpass);
$db_selected = mysql_select_db($dbname,$conn);

if (!$db_selected) {
    die ('Can\'t use test_db : ' . mysql_error());
}

if(! $conn ) {
  die('Could not connect: ' . mysql_error());
}

$sql = "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'";

$retval = mysql_query( $sql, $conn );

if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "Room Price :{$row['room_price']}  <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>

Upvotes: 0

Giwwel
Giwwel

Reputation: 347

you have send the query two times...

try this

$sql = "SELECT `room_price`, FROM `hotel_room_price` WHERE price_id = '1'";
$retval = mysql_query( $sql, $conn );

Upvotes: 0

Related Questions