Insert query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jeeee
Forum Newbie
Posts: 10
Joined: Sun Aug 03, 2008 11:18 am

Insert query

Post by Jeeee »

I'm trying to use the MySqli class to add to my MySql DB but for some reason it doesn't work, but if I type the same line in the MySQL administrator panel everything works!!!

Code: Select all

 
 
$dbc = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE, DB_PORT);
if (!$dbc->query("INSERT INTO ftpcredentials (FTP_IP, FTP_USER, FTP_PWD, FTP_PORT, idFournisseur) VALUES ('122.122.122.122', 'test', 'test', '1234', '1')"))
{
      printf("Errorcode: %d\n", $dbc->error);
      printf("%d Row inserted.\n", $dbc->affected_rows);
}
 
I keep getting 0 rows inserted.

Thanks
harshil
Forum Newbie
Posts: 13
Joined: Fri Aug 01, 2008 1:21 am

Re: Insert query

Post by harshil »

Hi,

I had not used MySqli. But below can be the reason the problem u r facing !!
When u will typing the same line in MySQL u r getting the things, because u will be
rewriting the insert query which is same in MySQL & MySqli.
But I think the problem will be the object that u r making for connection - [new mysqli]
Due to this connection is established to mysqli and not to mysql.

Jeeee
Forum Newbie
Posts: 10
Joined: Sun Aug 03, 2008 11:18 am

Re: Insert query

Post by Jeeee »

Isn't Mysqli and extension of MySql??? Cause right now what I do in MySql_Query should work if I do the mysqli->query???

Unless I'm mistaken..
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Insert query

Post by ghurtado »

Your query should work either way. Did you try running the SQL directly through the mysql command line or phpMyAdmin? what is the exact error message you get?
Jeeee
Forum Newbie
Posts: 10
Joined: Sun Aug 03, 2008 11:18 am

Re: Insert query

Post by Jeeee »

No that's what I'm saying... If I execute that query in MySql Command line it works no problem, If I runit using the MySqli->query() thing I always get 0 rows inserted.
Jeeee
Forum Newbie
Posts: 10
Joined: Sun Aug 03, 2008 11:18 am

Re: Insert query

Post by Jeeee »

Ok I got it to work... I guess I was trying to insert data in a int field!!!

Thank you to everyone and sorry for the stupidity!!!

Cheers
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Insert query

Post by RobertGonzalez »

Make sure your error reporting is cranked up to E_ALL and display errors is on (which means you should be doing this locally and not on your production server).

Code: Select all

<?php
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE, DB_PORT);
 
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 
$sql = "INSERT INTO ftpcredentials (FTP_IP, FTP_USER, FTP_PWD, FTP_PORT, idFournisseur) VALUES ('122.122.122.122', 'test', 'test', '1234', '1')";
 
if (!$db->query($sql)) {
    printf("Query failed: %s\n", $db->error);
} else {
    printf("Query ran. Rows affected: %s\n", $db->affected_rows);
}
 
$db->close();
?>
Run it and report your errors back.
Post Reply