I want to map my PHPExcel based on the picture below.
When I started to map my data that I got from the database I got only the customers information. However, I still want to get the Items that the customer ordered and map it in my PHPExcel.
Here's what I've done so far.
Here's my code:
$sqlmanagement = "select `check`, concat(hour, ':', minute) as `time`,
customer, unit, amount, employee, manager from gndtndr where type = 3 and
typeid = 1 and STR_TO_DATE(DATE, '%m/%d/%Y') BETWEEN '$date1' AND '$date2'
order by STR_TO_DATE(DATE, '%m/%d/%Y')";
$connect = #mysql_connect($dbhost, $dbuser, $dbpass)
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" .
mysql_errno());
//select database
$Db = #mysql_select_db($dbname, $connect)
or die("Couldn't select database:<br>" . mysql_error(). "<br>" .
mysql_errno());
//execute query
$resultmanagement = #mysql_query($sqlmanagement,$connect)
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" .
mysql_errno());
$rowManagementDiscresult = 2;
while($rowmanagement = mysql_fetch_array($resultmanagement)){
$rowmanagement1 = $rowManagementDiscresult + 1;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowManagementDiscresult,
$rowmanagement['check']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowManagementDiscresult,
$rowmanagement['time']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowManagementDiscresult,
$rowmanagement['customer']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowManagementDiscresult,
$rowmanagement['unit']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowManagementDiscresult,
'1');
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowManagementDiscresult,
$rowmanagement['amount']);
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowmanagement1, 'ITEMS
HERE');
$rowManagementDiscresult++;
}
Related
In my scrolling feed model i'm returning the number of users in the database , and the last 50 users added to the database in ascending order. For some reason , the last 50 users are not returning.
My model code is the following:
<?php
//display total records in db
//add html
echo '<link rel="stylesheet" href="assets/css/feed.css" /><div id="feed"<marquee>
<B>Available Accounts: </B>';
//fetch amount of users
$usercount="SELECT user_name FROM store";
if ($res=mysqli_query($conn,$usercount))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($res);
printf("%d",$rowcount);
// Free result set
mysqli_free_result($res);
}
//add html
echo
'<b>' .
' . . . Last 50 Added Usernames . . .</b>';
//last 50 added to the database
$lastusers = mysqli_query
("SELECT user_name FROM (
SELECT *
FROM store
ORDER BY user_id DESC
LIMIT 50
) AS store ORDER BY user_id ASC");
$row = mysqli_fetch_array($lastusers);
echo $row['user_name'];
echo '</marquee></div>';
?>
Try this out!
<?php
//MySQLi information
$db_host = "localhost";
$db_username = "username";
$db_password = "password";
//connect to mysqli database (Host/Username/Password)
$connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());
//select MySQLi dabatase table
$db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());
//fetch amount of users
$usercount = mysqli_query($connection, "SELECT user_name FROM store");
$rows = mysqli_num_rows($usercount);
echo $rows . " Users!" . "<br>";
//last 50 added to the database
$row = mysqli_query($connection, "SELECT * FROM store LIMIT 50");
while ($lastusers = mysqli_fetch_array($row)) {
echo $lastusers['user_name'] . "<br>";
}
Tested and works fine, here is an example!
Good luck!
Hoping someone can help me, this is taking forever and I need to speed it up:
SELECT Col2, Col3 FROM Table1 Where Col1 = Whats in Table1 Col1
UPDATE Table2 SET Col2, Col3 Where Col1 = Whats in Table2 Col1
the table I am updating and updating from have no indexes and are around 2.5 millions rows each.
The query is processing around 60 entries per second and will take way too long to finish. The code is below:
<?php
//establish connection to my Database
$link = mysql_connect('localhost', 'info', 'connect') or die('Could not connect: ' . mysql_error());
mysql_select_db('test') or die('Could not select database');
set_time_limit(18000);
//maintain logged in status
mysql_query("INSERT INTO `logtest` (`loggedin`, `timesince`) VALUES ('1', NOW());");
$shere=0;
$sql = "SELECT UniqueID FROM temp WHERE LongDec IS NULL ORDER BY UniqueID DESC";
$fcr = mysql_query($sql) or die(mysql_error());
while($frr=mysql_fetch_array($fcr)){
$shere=$frr[0];
}
mysql_free_result($fcr);
$sql = "SELECT DISTINCT UniqueID, LocationNum FROM temp WHERE UniqueID >=" . $shere . ";";
$fcr = mysql_query($sql);
while($fcrow = mysql_fetch_array($fcr)){
$sql = "SELECT LatDec, LongDec FROM lotest WHERE UniqueID=" . $fcrow[0] . " AND LocationNum=" . $fcrow[1] . ";";
$frr = mysql_query($sql);
$fr = mysql_fetch_array($frr);
$sql = "UPDATE temp SET LatDec=" . $fr[0] . ", LongDec=" . $fr[1] . " WHERE UniqueID = " . $fcrow[0] . " AND LocationNum=" . $fcrow[1] . ";";
mysql_query($sql);
//echo $fcrow[0] . ": " . $fr[0] . "<br>\n";
mysql_free_result($frr);
}
mysql_free_result($fcr);
//Move data to pool
mysql_query("TRUNCATE pool;");
mysql_query("INSERT INTO pool SELECT * FROM temp;");
mysql_close($link);
?>
I am working on a script that updates from:
tendesig_zink_production | euid0_hikashop_product | product_quantity
to:
tendesig_zink_dev | euid0_hikashop_product | product_quantity
I have to do this because our inventory numbers are stored on the production instance. so before pushing a new version from our dev instance i have to update the dev's inventories from production prior to the push. I am a lot rusty with my mySQL, this is what i have so far I need to know what the proper query would be though.
<?php
$host1="localhost"; // destination
$base1="tendesig_zink_dev";
$user1="tendesig_zink";
$password1="1,&#GZAWiB5z";
$host2="localhost"; //source
$base2="tendesig_zink_production";
$user2="tendesig_zink";
$password2="1,&#GZAWiB5z";
$conection1 = #mysql_connect($host1, $user1, $password1)
or die("Error reaching destination<br>".mysql_error()." nr eroare: ".mysql_errno());
print "Succesfuly connected 1! <br>";
$Db1 = #mysql_select_db($base1, $conection1)
or die("Error reaching destination database:<br>" . mysql_error(). "<br>" . mysql_errno());
print "Database1 reached!<br>";
$conection2 = #mysql_connect($host2, $user2, $password2)
or die("Error reaching source<br>".mysql_error()." nr eroare: ".mysql_errno());
print "Succesfuly connected 2!<br>";
$Db2 = #mysql_select_db($base2, $conection2)
or die("Error reaching source database:<br>" . mysql_error(). "<br>" . mysql_errno());
print "Database2 reached!!<br>";
$query = 'create table destination_table select * from second_database.source_table';
//echo "<br>".$query."<br>";
$result2 = mysql_query($query2, $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());
mysql_close($conection1);
mysql_close($conection2);
?>
Your query is wrong. You want to do something like this:
CREATE TABLE destination_database.destination_table LIKE source_database.source_table
Then run this:
INSERT INTO destination_database.destination_table
SELECT * FROM source_database.source_table
In (untested) PHP code:
$create_query = 'CREATE TABLE destination_database.destination_table LIKE source_database.source_table';
$result = mysql_query($create_query, $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());
$insert_query = 'INSERT INTO destination_database.destination_table SELECT * FROM source_database.source_table';
$result = mysql_query($insert_query , $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());
Here is the stored procedure
DELIMITER $$
BEGIN
SET #sql = concat(#sql ,'SELECT SQL_CALC_FOUND_ROWS ');
SET #sql = concat(#sql ,';');
prepare stmt from #sql;
execute stmt;
deallocate prepare stmt;
SELECT found_rows();
END
The php that calls the stored procedure.
$mysqli = new mysqli($databaseHostname1, $databaseUsername, $databasePassword);
if(!$mysqli) die('Could not connect: ' . mysql_error());
mysqli_select_db($mysqli, '$db');
if(!$mysqli) die('Could not connect to DB: ' . mysql_error());
if ($keywordParam != ''){
$mysqli->query("SET #p_keywordParam = " . "'" . $mysqli->real_escape_string($keywordParam) . "'");
}else{
$mysqli->query("SET #p_keywordParam = NULL");
}
$mysqli->query("SET #p_pageOffset = $pageOffset");
$result = $mysqli->query("CALL getInventory2(#p_keywordParam,#p_pageOffset)");
if(!$result) die("CALL failed: (" . $mysqli->errno . ") " . $mysqli->error);
I then output the data
NUMBER OF RESULTS: <? echo <<<<<<< FOUND ROWS HERE >>>>>>>>> ?>
PAGE 1 of ?
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
<? echo $row['FIRST_NAME']; ?>
<? echo $row['LAST NAME']; ?>
What I need is the found_rows count from the stored procedure prior to the while loop so that I can setup pagination.
Just after running your query, run this one:
SELECT FOUND_ROWS() AS int_count
This is definitely a beginner's question. There are two issues. The id in my MYSQL table (set to autoincrement) keeps going up, even though I delete rows from my table (I'm using phpmyadmin). As for the other issue, I can't seem to find a way to work with the row most recently entered by the user. The code echos all existing rows from MYSQL.
I've bolded the most pertinent section of code.
<?php
//establish connection to mysql
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
/*retrieve user input from input form
and initialize variables */
$Word1 = $_POST["Word1"];
$Word2 = $_POST["Word2"];
$Word3 = $_POST["Word3"];
$Word4 = $_POST["Word4"];
$Word5 = $_POST["Word5"];
//select db
mysql_select_db("madlibs", $con);
//insert user input for word 1
$sql = "INSERT INTO test (Word1, Word2, Word3, Word4, Word5)
VALUES
('$Word1', '$Word2', '$Word3', '$Word4', '$Word5')";
if(!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$result = mysql_query ("SELECT * FROM test");
/* take note here */
while($row = mysql_fetch_array($result))
{
echo $row['Word1'] . " " . $row['Word2'] . " " . $row['Word3'] . " " .
$row['Word4'] . " " . $row['Word5'] . " " . $row['id'];
echo "<br />";
} /* take note here */
mysql_close($con);
?>
$result = mysql_query ("SELECT * FROM test order by id desc limit 1");
As for your id question...that's how ids work. They don't fill in gaps.
On a side note: Never ever put user submitted data directly into the query string. Always escape them with mysql_real_escape_string().
SELECT * FROM test ORDER BY Id DESC LIMIT 1