0

I'm having a problem querying a MySQL DB with PHP

I'm using MAMP 1.9.4 on Mac OS 10.6.6

The connections seems to work

$dbc  =  mysqli_connect('localhost', 'root', 'password', 'dbname') or  
die('error connecting to MySQL server.');

But whenever I run a query I get the die error...

$query = "INSERT INTO table_name (first_name, last_name) VALUES ('John', 'Doe')";

$result  = mysqli_query($dbc,  $query) or die('error querying database.');

Any ideas?? Could it be something to do with MAMP?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Kevin
  • 565
  • 12
  • 25

2 Answers2

1

Don't die with a fixed error message like you are. That's basically useless, the equivalent of saying "something happened!"

Instead, try:

$result = mysqli_query(...) or die("Mysql error: " . mysqli_error());

which would spit out the exact reason there was a problem.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • It actually came back blank .. I also tried mysql_errno() but that came back as 0 .. could it have something to do with the environment? – Kevin Feb 16 '11 at 15:11
  • hard to say. check the webserver and/or php error logs, in case something's redirecting the error message elsewhere. not likely, but possible. Otherwise, try connecting to MySQL directly with the mysql monitor, using the same credentials, and pasting in the exact same query, see what blows up there. – Marc B Feb 16 '11 at 15:13
0

Run the following in phpmyadmin under SQL in the database, to see what the problem is.

INSERT INTO table_name (first_name, last_name) VALUES ('John', 'Doe')

It should give you some detail as to what is wrong with your query.

elliotanderson
  • 442
  • 4
  • 14