I'm trying to get the last id added to the database using mysql_insert_id but it doesn't work. The error I get is:
mysql_insert_id() [function.mysql-insert-id]: Can't connect to local MySQL server through socket '/var/tmp/mysql.sock' (2) in /proj/co639/assessment2/nicp3/public_html/book_create.php on line 46
Warning: mysql_insert_id() [function.mysql-insert-id]: A link to the server could not be established in /proj/co639/assessment2/nicp3/public_html/book_create.php on line 46
I've done my connection like this:
$handle = new PDO( 'mysql:host=;dbname=', '', '');
What could be the issue?
You are actually looking for this:
$handle->beginTransaction();
// here you execute your query
$handle->commit();
$id = $handle->lasInsertId();
Your code won't work because you mix two different classes.
More information can be found here: http://php.net/manual/en/pdo.lastinsertid.php
Related
$conn_161 = "192.168.0.161"; //local serwer adress
$user_161 = "ME";
$pass_161 = "what_is_the_password?";
$connect_161 = mssql_connect($conn_161,$user_161,$pass_161);
mssql_select_db ( 'DUKENUKEN3D' , $connect_161 );
//as requested - 1st DB connection and 1st query
$q_duke = "select * from DUKE /*DB1*/";
$r_duke = mssql_query($q_wartownik,$connect_161); //result
$connect_different_db = mssql_connect($conn_161,$user_161,$pass_161);
mssql_select_db ( 'BIGMAN' , $connect_different_db );
//second db and query
$q_bigman = "select * from BIGPEOPLE /*DB2*/";
$r_bigman = mssql_query($q_bigman,$connect_different_db ); //result
Error:
Warning: mssql_query() [function.mssql-query]: message: Invalid object name 'DUKE'.
Yes I know mssql_select_db is old, but I need to use it in this project. As you see I try to connect to same server but select 2 different DB at the same time, and run queries in the same php page (to connect data from 2 different DB). It seems it is not valiable? I even tried to run mssql_select_db just before doing the query to second DB, and then changing it back to first DB.
I understand this is limitation of the library (I will run all queries from LAST selected DB).
Is there a workaroud? Because all I got is to create page inside invisible iframe and there run php page with different db connection - far from good solution.
I would expect that this will work the same as it would if you were running this in a SQL environment directly (e.g. you can try it in SSMS or from the command line).
You can specify the database name when you reference the table in the query: e.g.
select * from db1.dbo.DUKE
This is standard SQL Server behaviour whenever you want to refer to an object which is outside the context of the current database.
I am using SQL Server 2012 and PHP 5.2.
To establish a connection and to select the DB I use this code:
$connection = mssql_pconnect(SYSTEM_DB_HOST, SYSTEM_DB_USER, SYSTEM_DB_PASSW);
mssql_select_db (SYSTEM_DB_NAME ,$connection);
$connection2 = mssql_pconnect(SYSTEM_DB_HOST, SYSTEM_DB_USER, SYSTEM_DB_PASSW);
mssql_select_db (SYSTEM_DB_NAME ,$connection2);
There is no error and everything works fine most of the time, but sometimes I get this error:
Warning: mssql_select_db(): Unable to select database: dbMyDB in ... on line 44
Warning: mssql_select_db(): Unable to select database: dbMyDB in ... on line 47
I dont know why. I dont know where the issue could be. I dont know how to debug it.
Anyone some tips?
Thank you very much.
try remove p from mssql_pconnect. Your driver don't persist.
Configure your mssql.ini or change your drive.
Replace to this: mssql_connect
I'm trying to get work this small project. I have master-admin over classic admin.
my database connection is establish on cookie $key. If i have cookie google, it looks into my master-admin database and search there for DB_PASSWORD, DB_HOST etc. And then it build config.php.
This cookie is set to expire after few days.
This is my config.php :
<?php
$key = $_COOKIE["nazev_webu"];
$sql = mysql_query("SELECT * FROM weby WHERE nazev='$key'");
while($row = mysql_fetch_array( $sql )) {
// Print out the contents of each row into a table
$name = $row['DB_NAME'];
$pass = $row['DB_PASSWORD'];
$user = $row['DB_USER'];
$host = $row['DB_HOST'];
}
//připojení dataze
$connect = mysql_connect($host, $user, $pass)
or die("Nelze se připojit k databázi");
//výběr databáze
$select = mysql_select_db($name ,$connect)
or die("Nemohu vybrat databázi");
?>
it works perfectly but only in "read only mode". I can select from database but if I'm trying to insert or update something i get this error.
Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /data20/website/html/config.php on line 7
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /data20/website/html/config.php on line 7
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /data20/website/html/config.php on line 9
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /data20/website/html/config.php on line 20
I have to say that insert and update action are build on ajax call, so i post via ajax id of article f.e.. to the update script.
I think problem can be that mysql are on another server, not on the same server as is my website.
The solution is too easy. I forgot to include static database connection into this file. So it can't look into database and get from here connection details for new config.php.
I am using mysql. And i want to connect with database using aura sql.
<?php
$connection_factory = include '/Aura/scripts/instance.php';
$connection = $connection_factory->newInstance(
// adapter name
'mysql',
// DSN elements for PDO; this can also be
// an array of key-value pairs
'host=localhost;dbname=db_aura',
// username for the connection
'root',
// password for the connection
''
);
$result = $connection->fetchAll('SELECT * FROM tbl_test');
?>
The above code shows the error
Parse error: parse error in C:\wamp\www\1\Aura\src\Aura\Sql\Connection\AbstractConnection.php on line 65
Finally I got it...
The code given above in my question is correct. Error occured due the version of my php i used. PHP version above 5.4 only support this. Now am using php 5.4.12 and aura sql is able to connect with mysql and working well.. Thanks.
I got this odd error and I can't figure out where it came from:
Warning: mysql_query(): 3 is not a valid MySQL-Link resource in (...)
What's up with the 3? I don't get it. Has anyone experienced this error themselves?
PHP uses resources as a special variable to hold links to external objects, such as files and database connections. Each resource is given an integer id. (Documentation)
Failed Connections
If the database connection fails you'll likely get a "Specified variable is not a valid MySQL-Link resource" error, as Dan Breen mentioned, since the variable that is supposed to hold the resource is null.
$link = mysql_connect('localsoth','baduser','badpass'); // failed connection
$result = mysql_query("SELECT 1", $link); // throws error
Since you're getting a specific resource ID in the error message, the database connection likely closed unexpectedly for some reason. Your program still has a variable with a resource ID, but the external object no longer exists. This may be due to a mysql_close() call somewhere before the call to mysql_query, or an external database error that closed the connection.
$link = mysql_connect();
mysql_close($link);
// $link may still contain a resource identifier, but the external object is gone
mysql_query("SELECT 1", $link);
Reusing Connections
An issue with the mysql extension and mysql_connect() is that by default if you pass the same parameters in successive calls, it will re-use the existing connection rather than create a new one (Documentation). This can be fixed by passing true to the $new_link parameter.
I encountered this myself on a test system where the data from two separate databases in production were combined on to one test server, and in testing the mysql_xxx() function calls walked over each other and broke the system.
$link1 = mysql_connect('localhost','user','pass'); // resource id 1 is given
$link2 = mysql_connect('localhost','user','pass'); // resource id 1 is given again
mysql_close($link2); // the connection at resource id 1 is closed
mysql_query("SELECT 1", $link1); // will fail, since the connection was closed
Using $new_link:
$link1 = mysql_connect('localhost','user','pass'); // resource id 1 is given
$link2 = mysql_connect('localhost','user','pass', true); // resource id 2 is given
mysql_close($link2); // the connection at resource id 2 is closed
mysql_query("SELECT 1", $link1); // the connection at resource id 1 is still open
Edit:
As an aside, I would recommend using the MySQLi extension or PDO instead, if possible. The MySQL extension is getting pretty old, and can't take advantage of any features past MySQL version 4.1.3. Look at http://www.php.net/manual/en/mysqli.overview.php for some details on the differences between the three interfaces.
I also had this problem. In examining my code I found I had an include of a script that closed the connection, so when php tried to close it again we got the error.
To solve this, just check if the connection is open before trying to close it:
instead of:
mysql_close($con);
Do this:
if( gettype($con) == "resource") {
mysql_close($con);
}
I had this error just a minute ago, it was because i was including a my database connection file which had a close connection function at the bottom. Get rid of your close connection and your be fine!
It sounds like you might be getting an error while trying to connect to the database, and the mysql handle is not actually a valid connection. If you post more code, like how you're connecting to the database, that would be more helpful. Make sure you're checking for errors, too.