Jump to content


Photo

chpt 2 - "error querying database"


  • Please log in to reply
65 replies to this topic

#1 Nadine

Nadine

    New Member

  • Members
  • Pip
  • 4 posts

Posted 15 January 2009 - 10:03 AM

I continue to get "error querying database" when running the code for chpt 2. can someone help me try to pinpoint how to find the problem?

I ran file comparisons on the final code and everything matches where it should.

I'm running this locally and mySQL and PHP versions are 5.x.x

Any help would be greatly appreciated!

Nadine

#2 max128

max128

    Active Member

  • Members
  • PipPip
  • 14 posts

Posted 17 January 2009 - 01:37 PM

I'm having the same trouble as well! :cry:

I'm running XAMPP on my computer and my code seems to check out just fine i.e. correct location of database, username, password and database name, but it says it can't make the connection.

The other things is, the error gives no clue regarding the problem just "can't connect to database". It would be great to have some code that would output an error to give even a hint what could be wrong. :?

#3 max128

max128

    Active Member

  • Members
  • PipPip
  • 14 posts

Posted 17 January 2009 - 03:50 PM

Fyi, I tried a different script and I successfully connected to the database and inserted the query. The biggest difference that I can see between this new script and the one from the book is that in the new script I used mysql instead of mysqli for the connection and query statements.

So at this point it looks like one of two possibilities for this problem: a) The script from the book has a problem in it or cool.gif some setting on our installations of PHP/MySQL need to change so we can use mysqli

That's the best I can come up with; I hope someone with more knowledge responds soon. :?

#4 max128

max128

    Active Member

  • Members
  • PipPip
  • 14 posts

Posted 17 January 2009 - 04:02 PM

I would post the script but for some reason, this forum doesn't allow me to do it. I post the entire script go to preview it and it's not there. I don't know what's wrong.

#5 valentin.berbec

valentin.berbec

    Active Member

  • Members
  • PipPip
  • 22 posts
  • Gender:Male
  • Location:East Sussex , UK
  • Interests:SEO,SEMJoomla!,HTML,CSS,PHP & MySQL,

Posted 17 January 2009 - 05:05 PM

The code in the book should work just fine because i just finished chapter 2. I think you have a different version of either PHP or MySQL installed on your computer. I have installed PHP and MySQL on a MAC therefore I can't tell you exactly what to do,however,if you make sure you have PHP 5 and MySQL 5.0. it should work.smile.gif

#6 max128

max128

    Active Member

  • Members
  • PipPip
  • 14 posts

Posted 17 January 2009 - 05:32 PM

Thanks for the reply; I installed XAMPP ver.1.70 which has PHP/MySQL 5xxx in it so I dont' think that should be the issue.

Again, I think it *might* have something to do with the mysql vs mysqli commands; perhaps a configuration setting on mine needs to be tweaked?

If you think posting/attaching your coding for this chapter might help, by all means. If the system allows you that is. :roll:

#7 valentin.berbec

valentin.berbec

    Active Member

  • Members
  • PipPip
  • 22 posts
  • Gender:Male
  • Location:East Sussex , UK
  • Interests:SEO,SEMJoomla!,HTML,CSS,PHP & MySQL,

Posted 18 January 2009 - 02:18 AM

Here is the code for chapter 2 :
<?php
  $first_name = $_POST['firstname'];
  $last_name = $_POST['lastname'];
  $when_it_happened = $_POST['whenithappened'];
  $how_long = $_POST['howlong'];
  $how_many = $_POST['howmany'];
  $alien_description = $_POST['aliendescription'];
  $what_they_did = $_POST['whattheydid'];
  $fang_spotted = $_POST['fangspotted'];
  $email = $_POST['email'];
  $other = $_POST['other'];

$dbc = mysqli_connect('localhost','username','password','aliendatabase')
or die('Error connecting to MySQL server');
  $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
    "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
    "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
    "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

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

  mysqli_close($dbc);

  echo 'Thanks for submitting the form.<br />';
  echo 'You were abducted ' . $when_it_happened;
  echo ' and were gone for ' . $how_long . '<br />';
  echo 'Number of aliens: ' . $how_many . '<br />';
  echo 'Describe them: ' . $alien_description . '<br />';
  echo 'The aliens did this: ' . $what_they_did . '<br />';
  echo 'Was Fang there? ' . $fang_spotted . '<br />';
  echo 'Other comments: ' . $other . '<br />';
  echo 'Your email address is ' . $email;
  

  
  
?>

Hope it helps.It works perfectly for me. smile.gif

#8 Nadine

Nadine

    New Member

  • Members
  • Pip
  • 4 posts

Posted 19 January 2009 - 08:33 AM

Still experiencing this problem...and I'd really love to successfully get beyond chapter 2. I've been testing locally, so I thought I should try to test on my web host. Web host is running correct 5xxx versions of everything.

I'm certain that I have the correct database location and user name, etc. I get the same response from this installation.

My code matches the book's code exactly.

#9 Michael Morrison

Michael Morrison

    Active Member

  • O'Reilly Author
  • PipPipPipPip
  • 114 posts
  • Location:Nashville, TN
  • Interests:nerdy tinkering, skateboarding, hockey, pond building, landscaping, architecture/construction

Posted 19 January 2009 - 08:51 AM

Just checking, did you change your database location to an actual path on the server, as opposed to 'localhost'? And please double-check this path if you haven't done so already. I have some domains on GoDaddy, and it was in no way obvious where to go to find the correct database location. It's also pretty cryptic for their virtual hosts - not something you would expect like data.mydomain.com.

Do you have phpMyAdmin installed on the server? It had to have been setup using the same database location/username/password, so that's another way to double-check those values and make absolutely sure they're right.
Michael Morrison
Author of Head First JavaScript, Head First PHP & MySQL

#10 Michael Morrison

Michael Morrison

    Active Member

  • O'Reilly Author
  • PipPipPipPip
  • 114 posts
  • Location:Nashville, TN
  • Interests:nerdy tinkering, skateboarding, hockey, pond building, landscaping, architecture/construction

Posted 19 January 2009 - 08:55 AM

One more thing, remember that you must create the database on the server before the script will work. So even if all your DB connection stuff is right, if you haven't done the CREATE DATABASE/CREATE TABLE stuff on pages 64-65 of the book, you'll still get errors because the database and table won't yet exist for you to connect to. And whatever name you specified in CREATE DATABASE should be the same as the last argument to mysqli_connect().

Apologies if you've already done that, just running through my mental checklist of common problems...
Michael Morrison
Author of Head First JavaScript, Head First PHP & MySQL

#11 Nadine

Nadine

    New Member

  • Members
  • Pip
  • 4 posts

Posted 19 January 2009 - 09:33 AM

Thanks for responding. I have gone through all of the above. ...and to make matters even more frustrating for myself - I installed php and mysql using wamp on my desktop (where this is not working) - then I installed php and mysql using xamp on a laptop and went through the same exercises creating database, tables, etc... - I'm experiencing the same issues.

Because I'm not getting the line "error connecting to MySQL server" I'm assuming that I'm successfully connecting to the database, it's just that the query isn't successfully running? Is that a correct assumption?

I don't know what else to check. Is there some alternative function that can be used to accomplish the same query/task?

Thanks for any and all help.

#12 max128

max128

    Active Member

  • Members
  • PipPip
  • 14 posts

Posted 19 January 2009 - 09:44 AM

I've narrowed my problem down the $query variable; when I substitute the $query I wrote with the one provided in the example code provided, everything works as it should.

Here's my code:

$query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how long, " . "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " . "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";


#13 Michael Morrison

Michael Morrison

    Active Member

  • O'Reilly Author
  • PipPipPipPip
  • 114 posts
  • Location:Nashville, TN
  • Interests:nerdy tinkering, skateboarding, hockey, pond building, landscaping, architecture/construction

Posted 19 January 2009 - 09:47 AM

(Nadine)
Because I'm not getting the line "error connecting to MySQL server" I'm assuming that I'm successfully connecting to the database, it's just that the query isn't successfully running? Is that a correct assumption?


OK, so you aren't seeing either of the error messages in the code (the ones triggered by die), correct? If not, then I'm assuming you know it's not working because you're somehow looking at the table and the data is never getting inserted?
Michael Morrison
Author of Head First JavaScript, Head First PHP & MySQL

#14 Michael Morrison

Michael Morrison

    Active Member

  • O'Reilly Author
  • PipPipPipPip
  • 114 posts
  • Location:Nashville, TN
  • Interests:nerdy tinkering, skateboarding, hockey, pond building, landscaping, architecture/construction

Posted 19 January 2009 - 09:49 AM

OK, so you aren't seeing either of the error messages in the code (the ones triggered by die), correct? If not, then I'm assuming you know it's not working because you're somehow looking at the table and the data is never getting inserted?
Michael Morrison
Author of Head First JavaScript, Head First PHP & MySQL

#15 Nadine

Nadine

    New Member

  • Members
  • Pip
  • 4 posts

Posted 19 January 2009 - 10:17 AM

Yeah!!!! success!!!! I'm doing my happy dance!

Thank you so much for trying to help me out! I just figured out my problem!!! I hadn't appropriately given rights to my username for the database!!!! DUH! Feeling a little stupid, but doing my happy dance anyway!


Thanks for your attempts to help.

#16 Michael Morrison

Michael Morrison

    Active Member

  • O'Reilly Author
  • PipPipPipPip
  • 114 posts
  • Location:Nashville, TN
  • Interests:nerdy tinkering, skateboarding, hockey, pond building, landscaping, architecture/construction

Posted 19 January 2009 - 10:21 AM

Whew, glad you got that one figured out. Sorry for asking all the obvious questions along the way. Permission stuff was further down the list. :wink:
Michael Morrison
Author of Head First JavaScript, Head First PHP & MySQL

#17 abmorris

abmorris

    New Member

  • Members
  • Pip
  • 2 posts
  • Location:South Carolina

Posted 21 January 2009 - 03:28 AM

Pretty much the same problem, though I'm leaning toward it being my hosting provider only having PHP 4.4.9. Any clue if this would cause problems with the script provided in the book, along with the suggestions already given in this thread?

If not, then I'll go ahead and post all the information I have, and what I have done, to try and help you guys pinpoint the problem (if I can't figure it out by then).

Anyway, thanks in advance.

#18 Michael Morrison

Michael Morrison

    Active Member

  • O'Reilly Author
  • PipPipPipPip
  • 114 posts
  • Location:Nashville, TN
  • Interests:nerdy tinkering, skateboarding, hockey, pond building, landscaping, architecture/construction

Posted 26 January 2009 - 08:49 AM

You may want to try changing mysqli to mysql in your code - check out page 714 for more details. But I really didn't think that was an issue in PHP versions greater than 4.1.
Michael Morrison
Author of Head First JavaScript, Head First PHP & MySQL

#19 Donkzilla

Donkzilla

    New Member

  • Members
  • Pip
  • 5 posts

Posted 01 February 2009 - 06:51 AM

(Nadine)
I continue to get "error querying database" when running the code for chpt 2. can someone help me try to pinpoint how to find the problem?

I ran file comparisons on the final code and everything matches where it should.

I'm running this locally and mySQL and PHP versions are 5.x.x

Any help would be greatly appreciated!

Nadine


I had an exactly similar problem but the solution was different. In the user unput form (report.html) I entered a string that included the word "don't." I kept getting "error querying database" until I changed don't to don\'t

Escaping ' with \' solved my problem.

#20 embtech

embtech

    Active Member

  • Members
  • PipPipPip
  • 73 posts

Posted 21 February 2009 - 08:58 PM

I am having the same difficulty that others have had. I am just not able to get data added to my database tables. I never see any connection or query errors, just no update to the database. Please any help would be appreciated. I am getting frustrated with not seeing what is wrong. Below is the code as it is exactly on my webhost server. I have even had the person hosting this for me look at it and he says that it should work. I have confirmed all the permissions with him and they are correct.


code:

<html>

<head>

<title>Aliens Abducted Me - Report an Abduction</title>

</head>

<body>

<h2>Aliens Abducted Me - Report an Abduction</h2>

<?php




//declaring variables

$name = $_POST['firstname'].' ' .$_POST['lastname'];

$when_it_happened = $_POST['whenithappened'];

$how_long = $_POST['howlong'];

$how_many = $_POST['howmany'];

$alien_description = $_POST['aliendescription'];

$what_they_did = $_POST['whattheydid'];

$fang_spotted = $_POST['fangspotted'];

$other = $_POST['other'];

$email = $_POST['email'];



//This area generates the report confirmation page

echo 'Thanks for submitting the form.<br>';

echo 'You were abducted ' . $when_it_happened;

echo ' and were gone for ' . $how_long. '<br>';

echo ' How many were there? ' . $how_many . '<br>';

echo 'Describe them: ' . $alien_description . '<br>';

echo 'What did they do? ' . $what_they_did . '<br>';

echo ' Was Fang There? ' . $fang_spotted . '<br>';

echo ' Any other peritnent information: ' . $other . '<br>';

echo 'Your email address is: ' . $email .' <br>';

echo 'This is the database query result: ' . $result .' <br>';

//echo 'This is the database conn result: ' . $dbc;



//Below is the area that generates the email.


//body of email


//$msg ="$name was abducted $when_it_happened and was gone for $how_long.\n".

//"Number of aliens: $how_many\n".

//"Alien description: $alien_description\n".

//"What they did: $what_they_did\n".

//"Fang spotted: $fang_spotted\n".

//"Other comments: $other";


//Fills in subject line of email

//$subject = 'Alien Abduction Report';


//Uses the respondant's email as from address

//$email = $_POST['email'];


//Sets up the to address

//$to = 'info.emb.tech@gmail.com';

//mail ($to, $subject, $msg, 'From:' . $email);



//This area below is the beginning of the sql database code

$dbc = mysqli_connect('localhost', 'greg', 'Petrosh', 'greg_aliens') or die('Error connecting to MySQL server.');

$query = "INSERT INTO aliens_abduction (first_name, last_name, " .
"when_it_happened, how_long, how_many, alien_description, " .
"what_they_did, fang_spotted, other, email) ".
"VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', " .
"'$fang_spotted', '$other', " .
"$email')";

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

mysqli_close($dbc);



?>

</body>

</html>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users