Don't know how to say this, but I have used mysql_insert_id() for many many years. Recently I included it in my web applications and worked well, I have successfully received 50 transactions and was happy.
All of a sudden all my transactions are coming in with ids equal 0, I Google'd around and read something about persistent connection being set to true? I just want me to make sure I give the right info to my network server guy, God knows, whenever something changes, they don't let me know, I only find out when my code all of a sudden breaks.
is there another function to use other than mysql_insert_id() in my PHP/MySQL code?
just for those who want to see code here is what i have: I am not including "id" as it is "auto increment not null primary key, etc"
<?php
$db_host2 = 'localhost';
$db_user2 = 'different';
$db_pass2 = 'different';
$db_name2 = 'different';
$conn2 = &NewADOConnection('mysqlt');
$db2 = $conn2->Connect($db_host2,$db_user2,$db_pass2,$db_name2);
if($conn2->isConnected()==false)
die("Could not connect to the database. Please try again in the next few minutes.");
$db_host = 'localhost';
$db_user = 'admin';
$db_pass = 'pass';
$db_name = 'name';
include_once('adodb/adodb.inc.php');
define('ADODB_FETCH_ASSOC',2);
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$conn = NewADOConnection('mysqlt');
$db = $conn->Connect($db_host,$db_user,$db_pass,$db_name);
if($conn->isConnected()==false)
die("Could not connect to the database. Please try again in the next few minutes.");
$q="insert into tableA(name,address,phone) values ('','','')"
$rs=$conn->execute($q);
$myid=mysql_insert_id();
?>
You just need 1 configuration example :
// database config
$DB_TYPE = "mysql";
$DB_HOST = "xxxxx";
$DB_USER = "xxxx";
$DB_PWD = "xxxx";
$DB_NAME = "xxx";
then use last insert ID for :
$db->Insert_ID
Related
$db_host = "";
$db_user = "";
$db_pass = "";
$db_name = "";
I have to fill out these, so I wanted to check but I used 'ampps' to install mysql.
so I couldn't find info in my database.
where should I check in below pic?
mysql wrokbench
wrokbench2
Detault mysql login credentials in AMPPS are :
Username : root
Pass : mysql
You can create a new database using phpMyAdmin :
http://localhost/phpmyadmin/
I'm hosting a domain at 1&1 and I want to connect with my database using pdo. Without using a port, it doesn't work and I don't know how to add the port to my code....
$mysql_host = "xxxxxxxxx";
$mysql_username = "xxxxxxxxxxxxxx";
$mysql_database = "xxxxxxxxxxxxxxx";
$mysql_password = "xxxxxxxxxxxxxxx";
$pdo = new PDO("mysql:host=" . $mysql_host . ";dbname=" . $mysql_database , $mysql_username , $mysql_password);
Im not sure but maybe the Problem doesn't go together with the connection, but with the mysql-commands...
$schematic_statement = $pdo->prepare('SELECT title FROM schematics-download WHERE id = 1');
$schematic_statement->bindParam('title', $Title);
$schematic_statement->execute();
$TITLE = $schematic_statement->fetch();
echo ($TITLE);
Thank you for your help!
"Without using a port, it doesn't work and I don't know how to add the port to my code...."
This (probably) has nothing to do with porting.
Your code however, contains a few (syntax) errors.
1) You can't bind a column (or a table)
Your SELECT title and bindParam('title' suggest it.
2) FROM schematics-download - mysql is interpreting that as FROM schematics MINUS download, therefore you need to escape the table name.
I.e.:
FROM `schematics-download`
If this is a porting issue, then this user contributed note shows you how to do it.
$conn = new PDO('mysql: host=123.4.5.6;dbname=test_db;port=3306','username','password');
^^^^^^^^^^
and from http://php.net/manual/en/ref.pdo-mysql.connection.php
More complete examples:
mysql:host=localhost;port=3307;dbname=testdb
mysql:unix_socket=/tmp/mysql.sock;dbname=testdb
As for error handling, PDO has that:
http://php.net/manual/en/pdo.error-handling.php
which would have thrown you a few.
Taken from Chris' comment:
"Later you fetch which returns an array so $TITLE can't be echoed."
Take a look at the documentation on fetching data in PDO:
http://php.net/manual/en/pdostatement.fetch.php
There are ample examples in there to show you on to do this.
I teach a little php and SQL and I would like my students to temporarily connect to a database they have testet on demo.phpmyadmin.net, is this done simply by using:
$username = "root";
$password = "";
$hostname = "192.168.30.23";
If so it doesn't seem to work for me. :)
I've recently made a new database that I had local. Everything worked fine and the scripts I've used (mysql and php codes) have been working properly. Since I've changed the database from local to online it has only caused problems with my script. Of course I've changed the details of connecting to MySQL database (having a password and such). Also, the content after the script won't work either.
Note: I haven't changed my script AT ALL. I've only changed the connection part.
$dbhost = "localhost";
$dbuser = "dbuser";
$dbpass = "dbpass";
$dbname = "compunll_itnj01";
$con = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$so = $con->prepare("SELECT * FROM besteloverzicht");
$so->execute();
--- rest of code
My apologies if I've forgotten to put anything further here. You can ask me and I'll respond asap!
Try to set the ip address of your db online
$dbhost = 'xx.xxx.xxx.xxx';
I cant seem to work out how to simply add a javascript variable into a mySQL table!
I have a html code which is just making a canvas for my game. I then have a javascript file doing all the game proccess and here is the code i am using to send the javascript variable to a php file:
var uiStats = $("#gameStats");
var uiHealth = $(".gameHealth");
var health = 10;
$.post('http:/localhost/basic_structure/game.php', { "health" : health});
Then here is the php file to insert the data into the database:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die
('Error connecting to mysql');
$dbname = 'game';
mysql_select_db($dbname, $conn);
$health = $_POST['health'];
mysql_query("INSERT INTO game_table (health)
VALUES ('$health')");
mysql_close($con);
I simply just want to save the health of the player in my game for later uses.
Any help is appreciated
Thanks
If you copy/pasted this from your actual code,
'http:/localhost/basic_structure/game.php'
should be
'http://localhost/basic_structure/game.php'
Your URL is incorrect in the post call
Also, use PDO or mysqli_ instead of mysql_ because mysql_ is deprecated.
A third suggestion is you should have a callback in your post (from php to javascript) to allow you to check that the process succeeded/failed and inform the user accordingly.