Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

put sql into php statement

this code works in the database query editor
but dont know how to put it into php

I have all the connection strings and all I need is

$sql="  ";

dbquery($sql);
INSERT INTO spreadsheet7 ([dateordered], [orderid], [internalsku], [quantity], [deliveryservicecode], 
      [companyname], [shipaddress1], [shipaddress2], [shipzip], [country],[contactname], [shipphone1]) 
SELECT o.dateordered,o.OrderID,p.internalsku,i.Quantity,12, RTRIM ( LTRIM( o.shipfirstname + ' ' + o.shiplastname ) ) ,o.shipaddress1,o.shipaddress2,o.shipzip, RTRIM ( LTRIM( o.shipstate + ' ' + o.shipcountry ) ) , RTRIM ( LTRIM( o.shipfirstname + ' ' + o.shiplastname ) ) ,shipphone1 
from  
        products p 
        inner join orderitems i on  p.productid = i.productid 
        inner join orders o on o.orderid = i.orderid 
where i.orderid = @x

Open in new window

0
rgb192
Asked:
rgb192
  • 2
  • 2
  • 2
  • +2
1 Solution
 
sudhakarsp06Commented:
Can you please tell me which sql statement is this MySQL or MsSQL?
0
 
poptixCommented:
<?php
$myServer = "localhost";
$myUser = "your_name";
$myPass = "your_password";
$myDB = "examples";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "INSERT INTO spreadsheet7 ([dateordered], [orderid], [internalsku], [quantity], [deliveryservicecode],
      [companyname], [shipaddress1], [shipaddress2], [shipzip], [country],[contactname], [shipphone1])
SELECT o.dateordered,o.OrderID,p.internalsku,i.Quantity,12, RTRIM ( LTRIM( o.shipfirstname + ' ' + o.shiplastname ) ) ,o.shipaddress1,o.shipaddress2,o.shipzip, RTRIM ( LTRIM( o.shipstate + ' ' + o.shipcountry ) ) , RTRIM ( LTRIM( o.shipfirstname + ' ' + o.shiplastname ) ) ,shipphone1
from  
        products p
        inner join orderitems i on  p.productid = i.productid
        inner join orders o on o.orderid = i.orderid
where i.orderid = @x";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

//display the results
while($row = mssql_fetch_array($result))
{
  echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
0
 
sudhakarsp06Commented:
In the above solution, possibly upto mysql_query($query) would run correctly. Because you cannot use mysql_num_rows for the result set as this is an insert statement.
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
Cornelia YoderArtistCommented:
$sql = "My Query";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$somefield = $row["SomeFieldName"];

The last 2 lines are only needed on SELECT queries.
0
 
Ray PaseurCommented:
Do you have a php function named dbquery()?
0
 
rgb192Author Commented:
>>Do you have a php function named dbquery()?

yes


0
 
poptixCommented:
The code I posted will work just fine, cut off the following if you do not expect rows to be returned:

$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

//display the results
while($row = mssql_fetch_array($result))
{
  echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
0
 
rgb192Author Commented:
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now