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
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);
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
Can you figure out my code? All the code does is: No database selected It won't get the data from the db. The server os is Ubuntu or OS X. I been pulling my hair out for hours.
<?php
mysqli_connect("localhost", "root", "");
mysql_select_db("hit-counter");
$sql_get_count = mysql_query("SELECT id FROM hit_info ORDER BY id DESC LIMIT 1");
if($sql_get_count === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($sql_get_count)) {
print_r($row);
}
?>
I try this, it does the same
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("hit-counter");
$sql_get_count = mysql_query("SELECT id FROM hit_info ORDER BY id DESC LIMIT 1");
if($sql_get_count === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($sql_get_count)) {
print_r($row);
}
?>
you have an error in your code. You use mysqli_ function to connect the server but you use a deprecated function mysql_ to select the database.
Try this code:
mysqli_connect("localhost", "root", "");
mysqli_select_db("hit-counter");
Another option when using mysqli_ is to select the database you want during connecting to the server:
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
You didn't mention database name:try this
<?php
$con = mysqli_connect("127.0.0.1","root","654321","testV2") or die("Some error occurred during connection " . mysqli_error($con));
// Write query
$strSQL = "SELECT id FROM did ORDER BY id DESC LIMIT 1";
// Execute the query.
$query = mysqli_query($con, $strSQL);
while($result = mysqli_fetch_array($query))
{
echo $result["id"]."
";
}
// Close the connection
mysqli_close($con);
?>
You cannot interchange the mysql and mysqli functions, please modify your mysql_select_db to mysqli_select_db.
I will not go over the errors everyone else has pointed out. But, I will mention one that no one has. I think the - character in your database name will also cause problems. You should enclose the database name in back ticks. The back tick is this ` character, most likely the far left key above the TAB key. If you had error reporting turned on, or looked at your php error log, you would have seen the error.
This is my second question about this problem. I would like get sum of column roll_sum
script:
($sum_number + (SELECT SUM(roll_sum) FROM table_name))
not work because collumn ROLL_SUM is NULL. But if try use replacement:
($sum_number + (SELECT SUM(ISNULL(roll_sum, 0)) FROM table_name))
not work aswell. But second script should replace NULL to 0?
Swap ISNULL() with SUM():
SELECT isnull(SUM(roll_sum), 0) FROM table_name;
I think there is no problem in your sql query but, you are calling sql query in php statement directly.
The below is sample code. please refer to it.
$db = mysql_connect("hostname", "username", "password");
mysql_select_db("dbname", $db) or die("connection failed");
$query = mysql_query("select sum(roll_sum) as sum from table_name", $db);
$query_row = mysql_fetch_array($query);
// to do something you want
$value = $sum_number + $query_row["sum"];
mysql_close($db);
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"...
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).