PHP MySQL DB query error - php

I am trying to get the top 10 items from a table:
<?php
include DBConnect.php;
$dbname = 'Telejoke';
mysql_select_db($dbname);
$query = "SELECT * FROM jokes LIMIT 10";
$data = mysql_query($query) or die('Error, insert query failed');
mysql_close($conn);
$info = mysql_fetch_array( $data );
?>
The PHP script keeps executing the die part saying that my query insert failed.
UPDATE:
The error is No connection could be made because the target machine actively refused it.
UPDATE 2:
I think the user I connect to DB is not authorized to use the SELECT command. This would cause the preceding error?

In db connection.php you must use a user name with password who is allowed to do operations on db.

Related

query result is null from php

I have a mysql database with a table "tb"
Column Type
---------------------------------
id int(11)
chainid int(11)
commission decimal(10,2)
order_count int(11)
total_order_value decimal(10,2)
fromdt date
todt date
I run this code from my php file
$query = "DELETE FROM `tb` WHERE chainid=$row[0];";
error_log("1st : ".$query);
$storeit = mysql_query($query);
error_log("result of query : ".$storeit);
$query = "INSERT INTO `tb` VALUES(null,$row[0],$com,$oc,$ov,'$startdate','$enddate');";
error_log("2nd : ".$query);
$storeit = mysql_query($query);
error_log("result of query : ".$storeit);
but there's nothing happens in DB, and the prints in error_log was:
1st : DELETE FROM tb WHERE chainid=4 AND
fromdt='2017-05-01' AND todt='2017-05-31';
result of query :
INSERT INTO tb
VALUES(null,4,4755.69,94,6793.84,'2017-05-01','2017-05-31');
result of query :
while when I use these two sentences:
DELETE FROM `tb` WHERE chainid=4 AND fromdt='2017-05-01' AND todt='2017-05-31';
INSERT INTO `tb` VALUES(null,4,4755.69,94,6793.84,'2017-05-01','2017-05-31');
in SQL tab in phpmyadmin they worked perfectly!!
So, I don't know what is wrong with my code? is there something I can do?
UPDATE
after using [tag:mysql_error()] from http://php.net/manual/en/function.mysql-error.php
function q($query){
$result = mysql_query($query);
if (mysql_errno()) {
$error = "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$query\n<br>";
error_log($error);
}
}
and called this function instead, using:
$query = "DELETE FROM `tb` WHERE chainid=$row[0] AND fromdt='$startdate' AND todt='$enddate';";
$result = q($query);
$query = "INSERT INTO `tb` VALUES(null,$row[0],$com,$oc,$ov,'$startdate','$enddate');";
$result = q($query);
I got these:
MySQL error 2014: Commands out of sync; you can't run this command now
When executing: DELETE FROM tb WHERE chainid=4 AND
fromdt='2017-05-01' AND todt='2017-05-31';
MySQL error 2014: Commands out of sync; you can't run this command now
When executing: INSERT INTO tb
VALUES(null,4,4755.69,94,6793.84,'2017-05-01','2017-05-31');
UPDATE 2
after a little search about:
Commands out of sync; you can't run this command now
in Commands out of sync; you can't run this command now SQL
I figured out that I have to close connection and start it again. but the problem is I'm using a function to connect database in the beginning of my function.php which included in my header.php file :
function db_connect()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";
#mysql_connect($servername,$username,$password) or die ("error in host connection");
header('Content-Type: text/html; charset=utf-8');
mysql_set_charset('utf8_unicode_ci');
#mysql_select_db($dbname) or die("error in db connection");
mysql_query("SET NAMES 'utf8'");
}
db_connect();
So, how to close the connection using mysql_close(); ?
I just needed to close connection before mysql_query using:
mysql_close();
and open a connection again immediately using my function:
db_connect();
Many thanks to everyone who contributed trying to solve this problem.

Underscore in dbname giving error

I am trying to connect to a mysql database having the name "_query".
It gives me the error "No database selected" when I run the following code:
mysql_select_db("_query", $con);
$sql = "SELECT * from mdl_sms_msg";
$result = mysql_query($sql) or die(mysql_error());
I have spent a lot of time on trying to fix this, please help.

No Database selected when executing MySQL in php

I'm having problems with this php code which needs to create a table in the database specified by the user. But whenever I try to execute the SQL it tells me no database selected. My code is as follow
<?php
$con = mysql_connect("127.0.0.1","peter")
or die('Error connecting to mysql'); // Check connection
// Create table
mysql_select_db("USE Ebooks");//Select Database
$foldername = mysql_real_escape_string($_POST['foldername']);//Obtain Folder Name
$sql = sprintf("CREATE TABLE %s (ID CHAR(3) ,Books CHAR(30))", $foldername);
mysql_query($sql) or die(mysql_error());
mysql_close($con);
?>
Use
mysql_select_db( "Ebooks" ) or die( 'Error'. mysql_error() );
Use this code:
mysql_select_db("Ebooks");//Select Database

using php to compare mysql columns to SQL Server

I have two databases, one online (mysql) and one in my office (SQL Server) which I would like to compare and update where a value is different.
I am using php to connect to the SQL Server database and run a query to retrieve the information, then connecting to the Mysql database running a query. Then I need to compare the two queries and update where necessary.
Is there somewhere I can look for tips on how to do this, I am sketchy on PHP and struggling really.
This is as far as I have got-:
<?php
$Server = "**server**";
$User = "**user**";
$Pass = "**password**";
$DB = "**DB**";
//connection to the database
$dbhandle = mssql_connect($Server, $User, $Pass)
or die("Couldn't connect to SQL Server on $Server");
//select a database to work with
$selected = mssql_select_db($DB, $dbhandle)
or die("Couldn't open database $DB");
//declare the SQL statement that will query the database
$query = "SELECT p.id, p.code, ps.onhand";
$query .= "FROM products p with(nolock)";
$query .= "INNER JOIN productstockonhanditems ps with(nolock)";
$query .= "ON ps.ProductID = p.ID";
$query .= "WHERE ps.StockLocationID = 1";
//execute the SQL query and return records
$get_offlineproduct2 = mssql_query($query);
mysql_connect("**Host**", "**username**", "**password**") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$get_onlineproducts = mysql_query(SELECT w.ob_sku, w.quantity
FROM product_option_value AS w
ORDER BY ob_sku)
or die(mysql_error());
//close the connection
mssql_close($dbhandle);
?>
I am looking to compare the value p.code to w.ob_sku and whenever they match copy the value of ps.onhand to w.quantity so the online database has the correct quantities from the office database.
My question I guess is how close am I to getting this right? Also am I doing this the right way, I don't want to get so far and realise that i am just wasting my time...
Thanks!
You do not need to fetch any record from MySQL, since you actually want to update it.
I would do something like this:
$query = 'SELECT p.code, ps.onhand FROM (...)';
// execute the SQL query and return a result set
// mssql_query() actually returns a resource
// that you must iterate with (e.g.) mssql_fetch_array()
$mssqlResult = mssql_query($query);
// connect to the MySQL database
mysql_connect("**Host**", "**username**", "**password**") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
while ( $mssqlRow = mssql_fetch_array($mssqlResult) ) {
$mssqlCode = $mssqlRow['code'];
$mssqlOnHand = $mssqlRow['onhand'];
mysql_query(
"UPDATE product_option_value SET quantity = $mssqlOnHand WHERE ob_sku = $mssqlCode"
// extra quotes may be required around $mssqlCode depending on the column type
);
}

what is wrong with this mysql code

$db_user="root";
$db_host="localhost";
$db_password="root";
$db_name = "fayer";
$conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn't connect to server");
// perform query
$query = 'SELECT * FROM posts';
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
// use returned data
while($row = mysqli_fetch_assoc($result))
{
echo $row['title'];
}
I get in the browser: "mysql problem".
Help!
UPDATE
I have echoed the query. It shows SELECT * FROM posts and when I query manually it gets the rows.
I think it has something to do with mysqli. I think i should use mysql. Do u think I have incompatibility problems with mysqli?
i have echoed it. it shows SELECT * FROM posts. and when i query manually it gets the rows.
i think it has something to do with mysqli. i think i should use mysql. do u think i have incompatibility problems with mysqli?
You have empty WHERE clause. Remove it or add a search condition.
Change
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
to
$result = mysqli_query($conn, $query) or die ("Couldn't execute query because: " . mysqli_error());
and you will know why the query is failing. Rule of thumb: Whenever you have a failed query, print it out and run it through phpmyadmin or some other raw-query executor and you will discover very quickly what the problem is.

Categories