1

I made a SQL query in my database and it works perfectly, but when I try to put this same query into PHP, it doesn't work. I cannot figure out where is the error.

//connection variables
$host = "localhost";
$database = "kjnjkyeo3";
$user = "root";
$pass = "probajovo11";

//connection to the database
$connection = mysql_connect($host, $user, $pass, $database)
or die ('cannot connect to the database: ' . mysql_error());


$sql = "SELECT ps_orders.id_order, ps_order_detail.id_order, ps_order_detail.product_reference AS Itemno,  ps_order_detail.product_quantity AS Saleqty, ROUND(ps_order_detail.total_price_tax_incl, 2) AS Cost, DATE_FORMAT(ps_orders.date_add , \'%Y%m%dT%T\' ) AS Dateoftrans\n"
    . "FROM ps_orders\n"
    . "LEFT JOIN kjnjkyeo3.ps_order_detail ON ps_orders.id_order = ps_order_detail.id_order";

//loop to show all the tables and fields
$loop = mysql_query($sql)
or die ('cannot select tables');

I made a lot of changes in my query but I always get the message cannot select tables. When I make a simple query like Select tables from $database, it works fine.


I made the changes:

//connection variables
$host = "localhost";
$database = "kjnjkyeo3";
$user = "root";
$pass = "probajovo11";

//connection to the database
$connection = mysql_connect($host, $user, $pass)
or die ('cannot connect to the database: ' . mysql_error());

//select the database
mysql_select_db($database)
or die ('cannot select database: ' . mysql_error());

$sql = "SELECT ps_orders.id_order, ps_order_detail.id_order, ps_order_detail.product_reference AS Itemno,  ps_order_detail.product_quantity AS Saleqty, ROUND(ps_order_detail.total_price_tax_incl, 2) AS Cost, DATE_FORMAT(ps_orders.date_add , \'%Y%m%dT%T\' ) AS Dateoftrans FROM ps_orders LEFT JOIN ps_order_detail ON ps_orders.id_order = ps_order_detail.id_order";

//loop to show all the tables and fields
$loop = mysql_query($sql)
or die ('cannot select tables');

but it still doesn't work

4
  • whats are the "\n" on the querys ? Commented Jan 20, 2014 at 8:46
  • i made this query directly in mysql database and i generate the sql statment with the option in database and he puts the \n. Commented Jan 20, 2014 at 8:50
  • are you sure database is a he? Think about it, it may be she :P Commented Jan 20, 2014 at 9:09
  • :), i am not a native english speaker Commented Jan 20, 2014 at 9:11

4 Answers 4

1
$connection = mysql_connect($host, $user, $pass) or die ('cannot connect to the database: ' . mysql_error());
mysql_select_db($database);
$sql = "SELECT ps_orders.id_order, ps_order_detail.id_order,
 ps_order_detail.product_reference AS Itemno,  ps_order_detail.product_quantity AS Saleqty, ROUND(ps_order_detail.total_price_tax_incl, 2) AS Cost, DATE_FORMAT(ps_orders.date_add , \'%Y%m%dT%T\' ) AS Dateoftrans FROM ps_orders LEFT JOIN kjnjkyeo3.ps_order_detail ON ps_orders.id_order = ps_order_detail.id_order";
Sign up to request clarification or add additional context in comments.

Comments

0

mysql_connect doesn't allow you to select the database like you are doing. You have to use mysql_select_db and one day you'll also have to eventually move to mysqli or PDO.

$connection = mysql_connect($host, $user, $pass) or die ('cannot connect to the database: ' . mysql_error());
mysql_select_db($database);

Comments

0

mysql_connect does not take a $database parameter. https://www.php.net/manual/en/function.mysql-connect.php, instead use:

$connection = mysql_connect($host, $user, $pass)
mysql_select_db($database, $connection)

Comments

0

Use mysql_select_db($database) to tell on which databases will the queries run.

If you are selecting from multiple databases you can use this way of referecing a field

`database_name`.`table_name`.`field_name`

or just

`database_name`.`table_name` 

for a table in FROM.

mysql_* functions are deprecated, if you are starting a new project use mysqli or pdo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.