insert query not inputing 1 million results - php

I am having trouble cross computing and inserting the results from 2 different tables into another one,
table structure
2_o contains 210 results
4_e contains 5985 results
all_combinations should theoretically contain after cross computing the results 210 * 5985 =
1,256,850 results
my query is working thus my only problem is that its not reaching the result that it should 1,256,850 and it seems that its not finishing and cutting every time on a different total. no idea why. 1,247,102 or 1,153,550 etc...
<?php
$sql = mysql_connect("localhost", "root", "mysql");
if (!$sql) {
die("Could not connect: " . mysql_error());
}
mysql_select_db("numbers");
$result = mysql_query("INSERT INTO all_combinations (o1,o2,e1,e2,e3,e4) SELECT n2.o1, n2.o2, n4.e1, n4.e2, n4.e3, n4.e4 FROM 2_o AS n2, 4_e AS n4");
echo "done";
if (!$result) {
die("Could not input results. " . mysql_error());
}
?>

Related

Get mysql query when i dont need

I built a website that takes a random row from a MySQL table.
The values I am interested in are Exercise and solution.
When I check if the solution the user wrote is the same as the one I got from the database, a new query is running and replacing my solution.
the query:
$query="SELECT Exercise,solution FROM exercises WHERE rank = '".$Rank."' ORDER BY RAND() LIMIT 1";
$result=mysql_query($query) or die ("Query to get data from firsttable failed: ".mysql_error());
while ($row=mysql_fetch_array($result)) {
$Exercise=$row["Exercise"];
$Solution=$row["solution"];
checking the answer:
<?php
if (isset($_POST['postBTN']))
{
if($_POST['solution'] == $Solution){
echo "<script type='text/javascript'>alert('yes');</script>";
$progression = ($progression+1);
$query="UPDATE users SET progression = '".$progression."' WHERE username = '".$_SESSION['username']."'";
$result=mysql_query($query) or die ("Query to get data from firsttable failed: ".mysql_error());
}else{
echo "<script type='text/javascript'>alert('no');</script>";
}
}
?>
When I check the answer a new query is running and replacing the solution.

How to Combine 3 database tables form 3 different databases into Database 4

I am trying to create a separate search site (site4 using database4) that is updated every hour from 3 different websites each having their own database. I want to combine the data from database1, database2, and database3 into database4.
I also want to remove the duplicates during the combining, so I was told to use the MySQL UNION function.
On the same server I have 4 individual websites with each site having their own MySQL database:
site1 --> database1, 1 table, 16 fields
site2 --> database2, 1 table, 16 fields
site3 --> database3, 1 table, 16 fields
site4 --> database4, 1 table, 16 fields (currently empty)
All 4 databases have an identical structure where as each database has only 1 table with 16 fields.
In all 4 databases, the table names are the same (Post_Data) and all 16 fields are identical. The index field (field 11) is named Post_Date.
With some forum help I was able to write the following PHP code, but it does not work and I do not get errors.
Can you see what is wrong in the code below and what needs to be done to fix it?
Thanks in advance.
<?php
// Website 1
$host1 = 'site1.com';
$database1 = 'data_1';
$username1 = 'user_1';
$password1 = 'pass_1';
$TableName1 = 'Post_Data';
// Website 2
$host2 = 'site2.com';
$database2 = 'data_2';
$username2 = 'user_2';
$password2 = 'pass_2';
$TableName2 = 'Post_Data';
// Website 3
$host3 = 'site3.com';
$database3 = 'data_3';
$username3 = 'user_3';
$password3 = 'pass_3';
$TableName3 = 'Post_Data';
// Website 4 - Search Database
$host4 = 'site4.com';
$database4 = 'data_4';
$username4 = 'user_4';
$password4 = 'pass_4';
$TableName4 = 'Post_Data';
// Connect to all 4 Databases
$connection1 = mysql_connect($host1, $username1, $password1) or die ('Cannot connect to the database because: ' . mysql_error());
$connection2 = mysql_connect($host2, $username2, $password2, true) or die ('Cannot connect to the database because: ' . mysql_error());
$connection3 = mysql_connect($host3, $username3, $password3, true) or die ('Cannot connect to the database because: ' . mysql_error());
$connection4 = mysql_connect($host3, $username4, $password4, true) or die ('Cannot connect to the database because: ' . mysql_error());
// Combine all 3 Databases into the Search Database #4
mysql_select_db ($database1,$connection1);
mysql_select_db ($database2,$connection2);
mysql_select_db ($database3,$connection3);
mysql_select_db ($database4,$connection4);
mysql_query("USE $database4");
mysql_query("CREATE TABLE temp AS
SELECT * FROM $database1.$TableName1
UNION
SELECT * FROM $database2.$TableName2
UNION
SELECT * FROM $database3.$TableName3
");
mysql_query("CREATE INDEX ix_post_date ON temp.Post_Date");
mysql_query("RENAME TABLE Post_Data TO backup, temp TO Post_Data");
// Close databases connections
mysql_close($connection1);
mysql_close($connection2);
mysql_close($connection3);
mysql_close($connection4);
// Finished
$date_time = date('m-d-Y H:i:s');
echo '<h1>Finished - '.$date_time.'</h1>';
?>
You create 4 different connections to the database, but the mysql_query is going to run against one connection.
The query will need to know which database to connect to. You can see How do you connect to multiple MySQL databases on a single webpage? for a good example
You should be able to see that your create table query fails by displaying the error:
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}

How to count the most common value in SQL?

At the moment I've got the following code:
$forummost = mysql_query("
SELECT door,
COUNT(door) AS doorCount
FROM forum_posts
GROUP BY door
ORDER BY COUNT(door)
DESC
");
$forummostc = mysql_num_rows($forummost);
$forummostf = mysql_fetch_assoc($forummost);
Can somebody explain me why mysql_num_rows($forummost) doesn't work?
Thanks in advance!
GROUP BY selects only the different values of that column in mysql. In your case, as pointed out, it selects as many rows as different door values are there.
mysql_num_rows counts the number of rows returned by the query. Your query is what is the count for each door so only one row for each door is returned.
try this, I tested, it works..
<?php
$link = mysql_connect('localhost','root','');
if (!$link)
{
die('Could not connect to MySQL: ' . mysql_error());
}
echo 'Connection OK'."</br>";
$forummost = mysql_query("
SELECT door,
COUNT(door) AS doorCount
FROM forum_posts
GROUP BY door
ORDER BY COUNT(door)
DESC
");
$forummostc = mysql_num_rows($forummost);
if ($forummostc == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($forummost)) {
echo $row["door"];
echo $row["doorCount"];
}
mysql_free_result($forummost);
mysql_close($link);
?>

PHP MySQL Query Returning Rows Very Slow

I am having a problem with a MySQL query. When ever I run this query it takes over 10 seconds to return the rows. However if I change my limit to 29 it returns it in less than 1 second. My Question is my implementation and query in good shape, or am I making a mistake here that would cause this issue?
<?
try
{
$con = mysql_connect("XXX.XXX.XXX.XXX","XXX","XXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bis_co", $con);
$query = "SELECT Name, Value FROM `bis_co`.`departments` LIMIT 31";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
while($row = mysql_fetch_assoc($result)){
echo $row['Name'] . "<br />";
}
mysql_close($con);
}
catch(Exception $e)
{
echo $e;
}
?>
You could try getting rid of the
$row = mysql_fetch_array($result)
statement. The if statement will take care of returning the array. You could also change the query string to
"SELECT Name, Value FROM `departments` LIMIT 31"
since you're already setting the database name in the mysql_select_db statement. Though, none of these should be causing your issues. It seems like it's a table issue. Have you tried it with mysqli and prepared statements?

SQL Multiple Update Statements

I need to update 40 rows in a database, starting at the point where the userID is a match. I do not want to have to determine the row number because eventually this database will be huge.
What I would like to do is simply say:
"Update these the next 40 rows with my array where userID = myUserID"
Here is what I have:
<?php
// connect to the database and select the correct database
$con2 = mysql_connect("localhost","Database","Password");
if (!$con2)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ifaves_code", $con2);
$i = 0;
while ($i < 40) {
//cycle through the array
$cycleThrough2 = $updatedUserNames[$i];
//$query = "UPDATE tblUserLinks SET `URLName` = '$cycleThrough2' WHERE userID = '" . $mainUID . "' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
++$i;
}
mysql_close();
?>
The variable $mainUID is being set correctly, the problem I'm having is it appears there are no updates taking place in the database.
How can I alter my existing code to receive my desired behaviour?
I don't suppose it's because you have your query building statement commented out...
//$query = "UPDATE tblUserLinks SET `URLName` = '$cycleThrough2' WHERE userID = '" . $mainUID . "' LIMIT 1";
If this is just the result of debugging and it wasn't working before that, you must call mysql_error() right after mysql_query() to see why it is failing.
$result = mysql_query($query);
if (!$result) echo mysql_error();
Also, you are using LIMIT 1 at the end, but the userID never changes in the WHERE clause. Therefore, you are updating the same row over and over 40 times on each loop. What you need is a way in your WHERE clause to identify rows which have already been modified, and exclude them. Otherwise, the same row (first match) will always be caught by the LIMIT 1 and updated.

Categories