I am trying to connect to a mysql database having the name "_query".
It gives me the error "No database selected" when I run the following code:
mysql_select_db("_query", $con);
$sql = "SELECT * from mdl_sms_msg";
$result = mysql_query($sql) or die(mysql_error());
I have spent a lot of time on trying to fix this, please help.
Related
// NO PROBLEM WITH IT
$dbhandle = mssql_connect(TB_DB_SERVER, TB_DB_USERID, TB_DB_PASSWORD) or die("Database connection error.");
$imageDBhandle = mssql_connect(TB_IMAGE_SERVER, TB_IMAGE_USERID, TB_IMAGE_PASSWORD) or die("Database connection error.");
// THIS WORKS FINE TOO
mssql_select_db("database", $dbhandle);
$sql_query = "SELECT * FROM table1";
$result = mssql_query($sql_query, $dbhandle);
// THIS COMPLAINS - message: Invalid object name 'table2'
mssql_select_db("anotherDatabase", $imageDBhandle );
$sql_query = "SELECT * FROM table2";
$result = mssql_query($sql_query, $imageDBhandle );
mssql_connect and mssql_select_db never complains when executing the code. However, the second code seems like having an weird issue. Why the second part gives me an error?
I found out that my user id was not set to the owner of the DB I've been trying to access... :( but still have no clue what err msg means... Anyway, it's solved.
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 am trying to get the top 10 items from a table:
<?php
include DBConnect.php;
$dbname = 'Telejoke';
mysql_select_db($dbname);
$query = "SELECT * FROM jokes LIMIT 10";
$data = mysql_query($query) or die('Error, insert query failed');
mysql_close($conn);
$info = mysql_fetch_array( $data );
?>
The PHP script keeps executing the die part saying that my query insert failed.
UPDATE:
The error is No connection could be made because the target machine actively refused it.
UPDATE 2:
I think the user I connect to DB is not authorized to use the SELECT command. This would cause the preceding error?
In db connection.php you must use a user name with password who is allowed to do operations on db.
I have this query:
$res=mysql_query("SELECT * FROM `t_modules` WHERE nPageM='61'") or die(mysql_error());
In phpmyadmin it returns (as expected) 2 rows but on page it returns 0.
If i use
$res=mysql_query("SELECT * FROM `t_modules` WHERE nPageM<>'61'") or die(mysql_error());
or
$res=mysql_query("SELECT * FROM `t_modules`") or die(mysql_error());
it runs on the page correctly it's just the WHERE and = combination that doesn't work
I also checked that the type for nPageM is int(11)
UPDATE
I can run comparisons on other columns in the table but not on nPageM
$res=mysql_query("SELECT * FROM `t_modules` WHERE id_md='5'") or die(mysql_error());
Is working. But i still don't have a clue about why it's not working on the nPageM column
have you ensured that you have included a connection to the databse in your php script before running this code?
<?php
$con = mysql_connect('sqluser', 'sqlpassword', 'sqlserver');
$db = mysql_select_db('dbame', $con);
//now, make sure it's connecting
if (!$con) {
die('mysql connection error' . mysql_error());
}
if (!$db) {
die('mysql Database error' . mysql_error());
}
?>
Instead try putting brackets around it:
$res=mysql_query("SELECT * FROM `t_modules` WHERE (nPageM<>'61') ") or die(mysql_error());
$db_user="root";
$db_host="localhost";
$db_password="root";
$db_name = "fayer";
$conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn't connect to server");
// perform query
$query = 'SELECT * FROM posts';
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
// use returned data
while($row = mysqli_fetch_assoc($result))
{
echo $row['title'];
}
I get in the browser: "mysql problem".
Help!
UPDATE
I have echoed the query. It shows SELECT * FROM posts and when I query manually it gets the rows.
I think it has something to do with mysqli. I think i should use mysql. Do u think I have incompatibility problems with mysqli?
i have echoed it. it shows SELECT * FROM posts. and when i query manually it gets the rows.
i think it has something to do with mysqli. i think i should use mysql. do u think i have incompatibility problems with mysqli?
You have empty WHERE clause. Remove it or add a search condition.
Change
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
to
$result = mysqli_query($conn, $query) or die ("Couldn't execute query because: " . mysqli_error());
and you will know why the query is failing. Rule of thumb: Whenever you have a failed query, print it out and run it through phpmyadmin or some other raw-query executor and you will discover very quickly what the problem is.