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

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.

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

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 );

<?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);
?>

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'");

Related

select data from mysql and display it in order of expiry date

I want to be able to display document name and expiry date in order of expiry date in my browser. Mysql table name is employee_doc and the database name is employee_info. Here is my code
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'employee_info';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $conn ) {
die('Could not connect: ' . $mysqli->connect_error());
}
$mysqli = 'SELECT * FROM `employee_doc` ORDER BY `employee_doc`.`PPExp` ASC';
mysqli_connect($conn,"employee_info");
$retval = mysqli_query($conn,$mysqli);
if(! $retval ) {
die('Could not get data: ' . $mysqli->connect_error());
}
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
echo $row['PPNO'];
echo "<br>";
echo $row['PPExp'];
echo "<br>";
}
mysqli_close($conn);
?>
PPExp and PPNO are the column headings for expiry date and document name respectively. I am using Xampp. When I open the file from localhost die('Could not get data: ' . mysql_error()); is executed. What am I doing wrong?
You need to include the database name in your mysql connection.
Your code:
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
Revised Code:
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, "employee_info");
mysqli_connect: Open a new connection to the MySQL server.
mysqli_select_db: Selects the default database to be used when
performing queries against the database connection. This function
should only be used to change the default database for the connection.
This explains the basic concepts of both the functions.
Check mysqli_connect and mysqli_select_db for detailed explanation.
Change your code as follows:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'employee_info';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $conn )
{
die('Could not connect: ' . mysqli_connect_error());
}
$sql = 'SELECT * FROM `employee_doc` ORDER BY `employee_doc`.`PPExp` ASC';
$retval = mysqli_query($conn,$sql);
if(! $retval )
{
die('Could not get data: ' . mysqli_error($conn));
}
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
echo $row['PPNO'];
echo "<br>";
echo $row['PPExp'];
echo "<br>";
}
mysqli_close($conn);
?>
Also you are mixing mysql and mysqli
mysqli_select_db is use for changing current database to new database and using that for selecting database is not currect
You should select db in the process of mysqli_connect as others explained.
see: http://php.net/manual/en/mysqli.select-db.php
Provide database name:
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
and changes in
$retval = mysqli_query($conn,$sql);
and
while($row = mysqli_fetch_array($retval))

PHP - Column count doesn't match

I've recently trying to add data into a database, (New to php), I've looked over what other people has with this error but yet still I cannot see where I have gone wrong.
The error is:
Column count doesn't match value count at row 1
Code:
$dbhost = 'localhost';
$dbuser = 'evocityi_admin';
$dbpass = 'password';
$database = 'evocityi_stocks';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $database);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$Dtime = "30/04/16";
$StockName = "FUMUKU";
$FUMUKUPrice = 1000;
$sql = "INSERT INTO stocks".
"(Stock,Price, TimeD) ".
"VALUES ".
"('$StockName,$FUMUKUPrice, $DTime')";
mysql_select_db('evocityi_stocks');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>
SQL Database:
https://gyazo.com/e74a6b9e87c18d60855424dcae647cdf
Change the column type for Stock and TimeD to varchar in your table definition. On the link you posted, they are both int.
as suggested by #PaulB12345, change column type to varchar and there is error in quotes.
ideally your query should be like (see quotes after values)
$sql = "INSERT INTO stocks".
"(Stock,Price, TimeD) ".
"VALUES ".
"('$StockName','$FUMUKUPrice', '$DTime')";

How can I use the data I get from my database using PHP, MySQL, Bootstrap?

My question simple Ex. Country, --> State --> City.
How to make like above this, I need to implement in my website. Like dropdown menu.
After the select the City its redirect to the City peoples list.
How its will make ???
// Create Connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
trigger_error("Connection failed: " . mysqli_connect_error());
}
//Run Query
$stmt = "SELECT * FROM country";
$result = mysqli_query($conn,$stmt) or die(mysqli_error($conn));
while(list($category) = mysqli_fetch_row($result)){
echo '<option value="'.$category.'">'.$category.'</option>';
}
mysqli_close($conn);
?>
</select>
Any other alternative code ?
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified
<?php
$dbname = "myDB";
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM country';
mysql_select_db($dbname);
$retval = mysql_query( $sql, $conn );
if(! $retval ){
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)){
echo '<option value="'.$row["id"].'">'.$row["name"].'</option>';
}
mysql_close($conn);
?>
http://php.net/manual/en/function.mysql-query.php
example http://www.tutorialspoint.com/mysql/mysql-select-query.htm

Query failed in php

$query = "SELECT * FROM Student WHERE student_id = $child AND school_id = $college";
$result = mysqli_query($conic, $query);
if(!$result){
die(" query failed");
echo 'error here';
} else {
i am trying to retrieve some information from the SQL database but the query seems to fail any ideas?
You must learn more : how to retrieve data from database using select in php
so try out following example to display all the records from Student table
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM student WHERE student_id = '"$child"' AND school_id = '"$college"' ";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
// some code here
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
NOTE
The mysql extension is deprecated and will be removed in the future:
use mysqli or PDO

mysql update query is not running

MySQL update query is not working on the php based webpage also not showing any MySQL error but when copy the query and run it in SQL it works fine.
Here is my code:
$query = "UPDATE table_name SET page_name = '".$page_name."' WHERE ip = '".$ip."'";
$update = mysql_query($query) or die(mysql_error());
please if anyone sees anything wrong in the code please let me know.
You should try this syntax instead:
$query = "UPDATE table_name SET page_name = '$page_name' WHERE ip = '$ip'";
$update = mysql_query($query) or die (mysql_error());
Please note that mysql_* methods are deprecated and you should use mysqli_* methods instead.
Demo Code : Its Work Fine for me.Please Use it.
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$page = 'Hello';
$ip = '102.101.22.23';
$sql = "UPDATE `pages`
SET `pagename` = '".$page."'
WHERE `ip` = '".$ip."'";
/* OR use $sql = "UPDATE pages SET pagename= '".$page."' WHERE ip='".$ip."'"; */
mysql_select_db('demo');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);

Categories