Updating remote database table from local database table - php

I want to update remote database from local database, I have written php script to do that but it is showing me fatal error.
Remote database and local database has same name, same table, same fields.
I have tried this way but its not working.
$tablename="pc_games";
$database = 'games';
$local_query = "SELECT * FROM $tablename LIMIT 100 OFFSET $remoterows";
$local_result = mysql_query($local_query, $connection) or trigger_error("SQL", E_USER_ERROR);
while($list=mysql_fetch_array($local_result))
{
$remote_update=mysql_query("INSERT INTO $tablename SETLECT * from $tablename");
$remote_update_result = mysql_query($remote_update, $remote_connection) or trigger_error("SQL", E_USER_ERROR);
}
Please see and suggest any possible approach to do this.

"INSERT INTO $tablename SETLECT * from $tablename"
This is an invalid SQL statement. SETLECT have to be SELECT

Your remote query has a syntax error: "INSERT INTO $tablename SETLECT * from $tablename". You mean SELECT instead of SETLECT.

Replacement code:
$tablename="pc_games";
$database = 'games';
$local_query = "SELECT * FROM $tablename LIMIT 100 OFFSET $remoterows";
$local_result = mysql_query($local_query, $connection) or die(mysql_error());
while($list=mysql_fetch_array($local_result))
{
$remote_update=mysql_query("INSERT INTO $tablename SELECT * from $tablename");
$remote_update_result = mysql_query($remote_update, $remote_connection) or die (mysql_error());
}
What you did wrong, was you mistyped this line:
$remote_update=mysql_query("INSERT INTO $tablename SETLECT * from $tablename");
Notice that you used SETLECT, instead of what you meant to use (perhaps): SELECT.
I also marvel at how long it takes to detect a typing error.
I changed all your error handlers to the ones I mentioned in the comments. Use them in the future. They're better.
UPDATE: What did you do with your code? I just realised something drastic:
$remote_update=mysql_query("INSERT INTO $tablename SELECT * from $tablename");
$remote_update_result = mysql_query($remote_update, $remote_connection) or die (mysql_error());
First of all, you're running mysql_query on a result. Second, you aren't inserting anything INSERT INTO $tablename. Is it my fault, or are you doing something dreadfully wrong?

Why are you looping through the results(with while($list=mysql_fetch_array($local_result))
) when you absolutely don't need it?
Why have you set the error function everywhere as: trigger_error("SQL", E_USER_ERROR);.
Why are you still using mysql_* method for databases?
If you just need to insert data from another table, just put:
mysql_query("INSERT INTO $tablename SELECT * FROM $tablename LIMIT 100 OFFSET $remoterows") or die(mysql_error());
and be done with it.

Related

php mysql_query won't find index

I am creating a property profile page for a real estate site. For some reason it wont check the index in the database to see if it is the same value as the ?id= in the address bar. When I use the code select * from properties it doesn't show any error but when I add WHERE index='$prop_id' it kills the page and echo's query error. The database connection file is included at the top off the index.php page. Can anyone help?
<?php
if(isset($_GET['id'])){
$prop_id = mysql_real_escape_string($_GET['id'])or die("get error");
$check = mysql_query("SELECT * FROM properties WHERE index='$prop_id'") or die("query error");
}
?>
index is a reserved keyword. Fix the errors in the query. Missing ' -
"SELECT * FROM properties WHERE `index` = '$prop_id'"
Try as below :
"SELECT * FROM properties WHERE `index` = ".$prop_id;
You forgot one single quote in the query. Please replace your query with the following:
$check = mysql_query("SELECT * FROM properties WHERE index='".$prop_id."'") or die("query error");

PHP error get value from database

I have php script like this
$query = "select * where userid = 'agusza' ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
echo $result;
}
when I execute, the result like this
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where userid = 'agusza'' at line 1
But when I run that sql in sqlserver, it running well
Anybody has solution ?
$query = "select * from table_name where userid = 'agusza' ";
See the corrections I have made. You haven't used the right syntax for SELECT query
You didn't select a table using FROM. Without that, it does not know which table you are selecting data from.
You should also stop using mysql as it is deprecated. Use mysqli or PDO as they are safer.
You are also echoing the wrong variable in your while loop, try this:
while ($row = mysql_fetch_array($result) {
echo $row['column_name'];
}
$query = "select * from table where userid = 'agusza'";
Right now, you're not telling which table SQL should look in.
You should format your query like so:
select * from `TableName` where userid='agusza'
In your query below you doesnt state the database table where you should get that data using FROM
$query = "select * where userid = 'agusza' "; // instead of this
$query = "select * FROM declaredtable where userid = 'agusza' "; used this

How to speed up this query with mysql_num_rows?

So I have 2 DBs. One is in my main host(Forums-Site) and the other one is for other info. The reason why I'm using the other DB is because my host doesn't allow remote connections. So I'm forced to try this way for now.
I want to simply write this code better. By better I'm talking about, is this the correct way to use it if I want to make another connection and if it can be optimized, please let me know how.
$connection = mysql_connect("Host", "User", "Pass") or die(mysql_error());
mysql_select_db("DB", $connection);
$gs_kdq = mysql_query("SELECT SUM(Kills) AS g_killz, SUM(Deaths) AS g_deathz FROM Users", $connection);
$gs_kd = mysql_fetch_assoc($gs_kdq);
$gs_players = mysql_query("SELECT * FROM Users", $connection) or die(mysql_error());
$gs_Something0 = mysql_query("SELECT * FROM Something0", $connection) or die(mysql_error());
$gs_Something1 = mysql_query("SELECT * FROM Something1", $connection) or die(mysql_error());
$gs_Something2 = mysql_query("SELECT * FROM Something2", $connection) or die(mysql_error());
mysql_num_rows($gs_players);
mysql_num_rows($gs_Something0);
mysql_num_rows($gs_Something1);
mysql_num_rows($gs_Something2);
Forums load a bit slow because of (I think) mysql_num_rows.
You're reading all of the data from your tables just to find out how big they are. SQL has a much faster way of doing this:
SELECT COUNT(*) FROM SomeTable
This will return one result with the number of rows in that table (you can add a WHERE clause and it will return the number that match).

mysql_fetch_array error when using WHERE clause

Im using:
$query = "SELECT * FROM mydb WHERE condition = New ORDER BY id ASC";
but i get this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mydb.php on line 84
however if i remove the where clause it works perfectly, can anyone point me in the right direction?
Is the Where clause not usable when doing a fetch array?
Thanks for any help.
edit: error message I've got:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition = 'New' ORDER BY id ASC'
always run all your queries this way (at least until you adopt some intelligent lib for this)
$query = "SELECT * FROM mydb WHERE condition = New ORDER BY id ASC";
$result = mysql_query($query) or trigger_error(mysql_error()." in ".$query);
just because not a single soul in the world can tell what's wrong with your query, but database itself. So, you have to ask it if there were any trouble. Not stackoverflow community (they have no idea anyway) but your db server. That's the point.
Note that you have to be able to watch errors occurred, either on-screen or in the error log.
After getting error message about syntax error you have to check syntax of the displayed query. If there are no visible errors, refer to http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html in case there are reserved word unescaped in your query. condition seems is. So
$query = "SELECT * FROM mydb WHERE `condition` = New ORDER BY id ASC";
will be solution
You appear to be missing quotes around the word "New".
$query = "SELECT * FROM mydb WHERE condition = 'New' ORDER BY id ASC";
Also, are you passing $query to mysql_fetch_array, or did you just not mention the mysql_query call in your question?
Since you have tried adding single quotes to the ('New'),
kindly ensure that the condition is a column in the table you are querying and
that mydb is a table in your database (and not your database name)!
You have to quote the string.
$query = "SELECT * FROM mydb WHERE `condition` = 'New' ORDER BY id ASC";
Edit:
condition is a reserved word.
Is New one of your columns or just a value?
Try this:
$query = "SELECT * FROM mydb WHERE condition = 'New' ORDER BY id ASC";
$query = "SELECT * FROM mydb WHERE condition = 'New' ORDER BY id ASC";
$result = mysql_query( $query );
while( $row = mysql_fetch_array( $result ) {
// use $row
}
Never assume that a query will work - expect errors and check for them before processing any results.
$query = 'SELECT * FROM `mydb` WHERE `condition` = "New" ORDER BY `id` ASC';
$result = mysql_query( $query );
if( !$result ){
// Query Failed. You can access the error details with mysql_error()
}elseif( mysql_num_rows( $result )==0 ){
// Query Returned No Results
}else{
while( $r = mysql_fetch_assoc( $result ) ){
// Do whatever you want with the row, which is $r
}
}

insert into mysql problem

i have a field in table opt named confirm of type tinyint. i want to insert value(1) by this statement but it is not working can any one help??
$connect= mysql_connect("localhost","root") or die ("Sorry, Can not connect to database");
mysql_select_db("login") or die (mysql_error());
$user=$_POST['staff'];
echo $user;
$query="SELECT * from users where username='$user' ";
$result=mysql_query($query,$connect) or die(mysql_error());
$row=mysql_fetch_array($result);
$uid=$row['userid'];
echo $uid;
$query="SELECT * from opt where userid='$uid' ";
$result=mysql_query($query,$connect) or die(mysql_error());
$row=mysql_fetch_array($result);
if($row['confirm']==0)
{
$query = "INSERT INTO opt (confirm) values(1)";
echo 'The user selected options has confirmed';
}
?>
You are not executing the query.
add an extra
$result=mysql_query($query,$connect) or die(mysql_error());
after the line
$query = "INSERT INTO opt (confirm) values(1)";
Apart from not executing the "InSERT STATEMENT",
You should probably be using an
"UPDATE OPT SET CONFIRM = '1' WHERE USERID = $user;"
as the row already exists ('cause you managed to select it!).
$query is a variable and there's no reason that it would cause a record to magically get inserted into the opt table.
You need to insert the following line after $query = "...":
mysql_query($query);
Also, I hopethat's not the code you're running in production.
You need to have the following somewhere:
$user = mysql_real_escape_string($user);
Why is not working? what error is throwing?
Check the other fields of the table...

Categories