Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
Or, use your gamerDNA username: (more...)
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-29-2007, 01:11 AM   #1 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
Request: PHP/MySQL Help

So I've been taking a break from C++ and have been delving into PHP/MySQL.

I'm having the most insane problem adding information to my database. I have a database named wowqdb with a table called "simple" with only one parameter ("name"). I'm almost 90% confident that my syntax is correct, but for some reason when I get to mysqli_query() it fails .

Any assistance would be appreciated.

Quote:

$dbName = "wowqdb";
$host = "localhost";
$usrName = "root";
$usrPass = "elofedgela";
$name = $_POST["name"];

$mysqli = mysqli_connect($host, $usrName, $usrPass);

if ($mysqli == TRUE) {
echo "Connected successfully
";
}
else
{
echo "Connection failed";
}

$mysqliQuery = "INSERT INTO simple VALUES ('Bah')";

if (mysqli_query("$mysqliQuery") or die ("Bah!")) {
echo "A record has been inserted.";
}
else {
echo "Couldn't insert record.";
}

mysqli_close($mysqli);
Again, it just dies. =\
tikkus is offline   Reply With Quote
Old 03-29-2007, 02:14 AM   #2 (permalink)
Furism
Registered User
 
Furism's Avatar
 
Join Date: Sep 2002
Posts: 1,128
-4 Internets
I'm not too familiar with this function, as I usually use mysql_query. But a quick look on php.net tells me that you need to specify more arguments in the query.

Anyway, the SQL query itself is not correct. I believe you forgot to specify the field name in the table. With a mysql_query it would look like this:

Code:
mysql_query("INSERT INTO simple (name) VALUES ('$foo')") or DIE("Couldn't insert!" .mysql_error());
Which has the advantage of printing an SQL error message in case the query fails.
__________________
- Furism

Last edited by Furism : 03-29-2007 at 07:35 AM.
Furism is offline   Reply With Quote
Old 03-29-2007, 02:19 AM   #3 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
Didn't work. =\

If you don't specify the field name I'm pretty sure it just automatically fills the table with values in order.

It might have something to do with my database configuration, I'm pretty lost on setting up all of the table parameters.
tikkus is offline   Reply With Quote
Old 03-29-2007, 02:23 AM   #4 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
This is my output, btw:
Quote:
Testing
Connected successfully
Bah!
tikkus is offline   Reply With Quote
Old 03-29-2007, 07:29 AM   #5 (permalink)
Zippygoose
Math Enthusiast/Badass MC
 
Zippygoose's Avatar
 
Join Date: Jun 2002
Location: Seattle
Posts: 650
+0 Internets
Send a message via AIM to Zippygoose
Are you certain that your table and column names are correct and that your column type is varchar?
Zippygoose is offline   Reply With Quote
Old 03-29-2007, 07:36 AM   #6 (permalink)
Furism
Registered User
 
Furism's Avatar
 
Join Date: Sep 2002
Posts: 1,128
-4 Internets
Quote:
Originally Posted by tikkus View Post
If you don't specify the field name I'm pretty sure it just automatically fills the table with values in order.
Did you actually try to specify the field name even if there's only one?
__________________
- Furism
Furism is offline   Reply With Quote
Old 03-29-2007, 07:43 AM   #7 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
Yeah, I did. Same results.
tikkus is offline   Reply With Quote
Old 03-29-2007, 07:53 AM   #8 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
Quote:
Originally Posted by Zippygoose View Post
Are you certain that your table and column names are correct and that your column type is varchar?
It was TEXT, I wasn't aware that it should be a certain type. I can't find any reference on the different column types.

Changed it to VARCHAR and am still getting the same results. Also added it as primary key and it didn't change anything. =\

What should the collation be? It's set as utf_general_ci.
tikkus is offline   Reply With Quote
Old 03-29-2007, 08:00 AM   #9 (permalink)
Maio
Registered User
 
Join Date: Dec 2004
Posts: 65
-1 Internets
Quote:
Originally Posted by tikkus View Post
if (mysqli_query("$mysqliQuery") or die ("Bah!"))
Try:

Quote:
Originally Posted by Maio
if (mysqli_query( $mysqliQuery ) or die ("Bah!"))
..without the quotation marks.
Maio is offline   Reply With Quote
Old 03-29-2007, 08:16 AM   #10 (permalink)
Rufus
Registered User
 
Join Date: Sep 2002
Posts: 54
the above should fix this particular issue. You could also have it die and throw the last mysql error, which would help in debugging.


Quote:
if (mysqli_query( $mysqliQuery ) or die (mysqli_error()))
Rufus is offline   Reply With Quote
Old 03-29-2007, 11:14 AM   #11 (permalink)
Furism
Registered User
 
Furism's Avatar
 
Join Date: Sep 2002
Posts: 1,128
-4 Internets
For my own education, what's the difference between mysqli_query and the regular mysql_query?
__________________
- Furism
Furism is offline   Reply With Quote
Old 03-29-2007, 11:30 AM   #12 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
I'm not exactly sure. I installed Apache/PHP/Ruby/MySQL on my own PC using some guide I got from the internet and it told me to uncomment the mysqli portion of the php.ini.

Supposedly, its supposed to be faster but a quick google shows you that it isn't the case in all situations.

mysql functions don't even work for me, I have to use mysqli.

Edit: PHP: MySQL Improved Extension - Manual

Last edited by tikkus : 03-29-2007 at 11:34 AM.
tikkus is offline   Reply With Quote
Old 03-30-2007, 11:21 AM   #13 (permalink)
elderec
Administrator
 
elderec's Avatar
 
Join Date: Dec 2004
Posts: 38
+0 Internets
After you connect to the database server, you need to select the database you wish to work with before attempting to insert into it.

Code:
$jl = mysql_connect('hostname', 'username', 'password') or die('Could not connect: ' . mysql_error()); mysql_select_db("database",$jl) or die(mysql_error()); $sql = 'INSERT INTO simple VALUES ('Bah')'; mysql_query($sql); mysql_close($jl);
__________________
Site Administrator
Uberguilds.org
elderec is offline   Reply With Quote
Old 03-30-2007, 12:13 PM   #14 (permalink)
Rufus
Registered User
 
Join Date: Sep 2002
Posts: 54
Quote:
Originally Posted by Furism View Post
For my own education, what's the difference between mysqli_query and the regular mysql_query?
Some performance differences...

MySQLi vs MySQL (benchmarks)

I also believe mysqli is required to access some of the newer features in mysql 5.
Rufus is offline   Reply With Quote
Old 03-31-2007, 06:07 AM   #15 (permalink)
Furism
Registered User
 
Furism's Avatar
 
Join Date: Sep 2002
Posts: 1,128
-4 Internets
Quote:
Originally Posted by Rufus View Post
Some performance differences...

MySQLi vs MySQL (benchmarks)

I also believe mysqli is required to access some of the newer features in mysql 5.
Thanks for that link. Looks like for small to medium-sized website you want to stick to mysql_query.
__________________
- Furism
Furism is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 01:20 AM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6