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).
Related
i want to connect two mysql databases one is located in localhost and other one is located in a server
here is what i have done so far and i am not getting either error or data
<?php
$con=mysql_connect('120.247.201.8:3306','root','root');
$con1=mysql_connect('localhost','root','');
//mysql_connect('localhost','root','');
if(!$con){
die('Try Again');
}
if(!$con1){
die('Try Again');
}
mysql_select_db("iot",$con1);
mysql_select_db("lora_gateway",$con);
$result =mysql_query("SELECT lora_gateway.`server_log`.`created_at`, lora_gateway.`server_log`.`temperature`FROM iot.`device` inner JOIN `lora_gateway`.`server_log` on `lora_gateway`.`server_log`.`gateway_Id` = `iot`.`device`.`gatewayId` where iot.`device`.`deviceId`='23' ORDER BY lora_gateway.`server_log`.`created_at` desc");
$num= mysql_num_rows($result);
print_r($num);
?>
This is a solution for multiple servers, connections and DBs.
Two or more MySQL 5 db servers
Two or more locations (local and/or anything else)
Two or more different databases
Two or more tables and fields
Tested and Works fine :-)
//Define your database connections and select your database want to use. In this example I use two connections and two DBs. But you can use more than two.
<?php
//MySQL Server 1
$dbhost1 = "127.0.0.1";
$dbuser1 = "dbuser1";
$dbpassword1 = "dbpass1";
$db1 = "database1";
$connection1 = mysql_connect($dbhost1,$dbuser1,$dbpassword1) or die (mysql_error());
mysql_select_db($db1,$connection1);
//MySQL Server 2
$dbhost2 = "xxx.xxx.xxx.xxx";
$dbuser2 = "dbuser2";
$dbpassword2 = "dbpass2";
$db2 = "database2";
$connection2 = mysql_connect($dbhost2,$dbuser2,$dbpassword2) or die (mysql_error());
mysql_select_db($db2,$connection2);
//The SQL statement
$sql =" SELECT database1.tablename1.fieldname1 AS field1, database2.tablename2.fieldname2 AS field2 FROM database1.tablename1,database2.tablename2";
//Execute query and collect results in $results
$results = mysql_query($sql);
//Print result until end of records
while($rows = mysql_fetch_array($results)){
print $rows["field1"]." | ".$rows["field2"]."<br>";
}
?>
First of all, try to accustom yourself to using PDO or at least the mysqli_ functions. It's the future. :)
mysql_query's second parameter, the connection link, is optional. If omitted, it uses the last connection opened with mysql_connect. (See php.net Documentation)
Ergo, always use $con or $con1 as 2nd parameter in order to use the correct connection.
Then, provided that your queries are correct, it should work as expected.
what about to add connection to mysql_query() ?
$result = mysql_query("SELECT lora_gateway.`server_log`.... desc", $con);
I have a pretty basic website connected to a database and I want to access the result of an SQL query:
$connection = mysql_connect("localhost", "username", "password")
or die(mysql_error());
mysql_select_db("Database")
or die(mysql_error());
$query = mysql_query("SELECT path FROM db1 WHERE id > 4");
while($row = mysql_fetch_object($query))
{
echo $row->path;
}
On the website nothing shows, not even an error
(The SQL code works for sure)
Since you stated in comments that there were no id greater than 4,
you could have just done WHERE id >= 4 which means greater than or equal to 4 and you would have had results.
References:
http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html
http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_greater-than-or-equal
Im a noob and only 16, i have a table full of videos that can be added though the website.
How do i show the Number of videos in the my-sql database that i can just past in between some php tags?
the database has tables likes users etc but one is "post" and i need to show how many records there are in it.
there is a config.php file that connects to the database which is like this (with the real info)
<?php
$dbh = new PDO('mysql:dbname=mydbname', 'username', 'password');
echo $dbh->query('SELECT COUNT(*) FROM videos')->fetchColumn();
?>
Hi james this is the example code from php.net. Which echo the number of rows in table 1. You could try playing with it.
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
?>
Hope you understand
If you know how to connect database to PHP, mysql_num_rows() might come in handy. Read the manual here.
Sample code:
$db_selected = mysql_select_db("test_db",$con);
$sql = "SELECT * FROM person";
$result = mysql_query($sql,$con);
echo mysql_num_rows($result);
mysql_close($con);
?>
Just use COUNT() in your SQL
$con=new mysqli('localhost?', 'username', 'password', 'tablename');
$stmt=$con->prepare('SELECT COUNT(*) FROM videos');
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
echo 'There are '.$result.' Videos in the DB.';
By learning on tutorials, doing some PHP/MySQL tests, reading some books and searching on Google keywords like "MySQL counting rows in 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.
I'm wondering if i can use 2 variables with the same name on the same code and it will still work or i need to give every query a diff name? (I know its a super simple question) ;)
Here is an example:
$sql="DELETE FROM apps WHERE app_id='".$app_id."'";
mysql_query($sql) or die(mysql_error());
$sql="DELETE FROM statistics_apps WHERE app_id='".$app_id."'";
mysql_query($sql) or die(mysql_error());
Do i need to change the second $sql to $sql2?
Thanks!
Do i need to change the second $sql to $sql2?
No. as long as you make sure to execute $sql (version1) before $sql (version2)
There's no need.
On the other hand if you are doing:
$sql="SELECT * FROM apps WHERE app_id='".$app_id."'";
$result = mysql_query($sql) or die(mysql_error());
$sql="SELECT * FROM statistics_apps WHERE app_id='".$app_id."'";
$result2 = mysql_query($sql) or die(mysql_error());
You must safe the $result query handle in different vars, if you do not fetch all rows from query1 before you are start with query2.
as long as you call your first mysql_query($sql) before you refill your $sql-variable, it is no problem. You just use one variable which you just give another value :)
You don't need to change your $sql variable. What you need is keep the return value of mysql_query.
$sql="DELETE FROM apps WHERE app_id='".$app_id."'";
$query1 = mysql_query($sql) or die(mysql_error());
$sql="DELETE FROM statistics_apps WHERE app_id='".$app_id."'";
$query2 = mysql_query($sql) or die(mysql_error());
var_dump(mysql_affected_rows($query1)); // deleted in apps
var_dump(mysql_affected_rows($query2)); // deleted in statistics_apps
No, you do not need to have a separate variable for each query, unless you need/want to refer to the original query at some later point in the code.
No you do not... because at the end it like write that:
mysql_query("DELETE FROM apps WHERE app_id='".$app_id."'") or die(mysql_error());
mysql_query("DELETE FROM statistics_apps WHERE app_id='".$app_id."'") or die(mysql_error());
the value of your variable $sql is changed