Jump to content


Photo

chpt 2 - "error querying database"


  • Please log in to reply
65 replies to this topic

#41 EZRider

EZRider

    New Member

  • Members
  • Pip
  • 1 posts

Posted 10 July 2009 - 05:55 AM

I had the same error when I tried this example. I'm using WAMP 2.0h on my laptop. So to make things as easy and simple as possible, let me tell you what worked for me.

When entering abduction data in report.html make sure you use an apostrope (`) instead of single quotes (') and you should be good to go smile.gif
Example:
I don't know. <- Not good - single quote not escaped
I don`t know <- Good - apostrophe

Now if you want, you could play around with str_replace() function and just in case user types a single quote (') instead of an apostrope (`) make sure you replace ' with `


Hope this helps....

#42 Hooded_Villian

Hooded_Villian

    New Member

  • Members
  • Pip
  • 3 posts

Posted 15 September 2009 - 02:32 PM

QUOTE (eimac @ Mar 27 2009, 07:43 PM) <{POST_SNAPBACK}>
My server didn't like the "i" forms of the commands used in the example and I was getting the same errors. So I used the older versions and got it all to work by replacing the mysqli_connect command with these two:

$dbc = mysql_connect('data.aliensabductedme.com', 'owen', 'aliensrool')
or die('Error connecting to MySQL server.');

mysql_select_db('aliendatabase');

Also, I used

mysql_query($query)
or die('Error querying database.');

instead of the two-argument form for mysqli_query.

I did the same thing in the Chapter 3 example and it works now also. I'm surprised that the "i" versions aren't on my provider's server as they host thousands of accounts running PHP. I'm wondering if I'm just missing an include. At least it works with these mods.

Hi Guys... I was having the same problem as all of you have been having. "Error querying database" I got as far as "theartofconfusion @ May 24 2009, 10:40 PM" and solved the problem. I haven't even read further than that, yet.

My problem, was not with the "i"s of the mysql commands, but rather connecting to the database. So mysqli_select_db(); did the trick.

For some people who rate this book as rubbish... I have to just say that I have 3 other PHP/MySQL books lieing on my desk, and this is the only one that has put what we need to learn into proper practice. So we can see how others do it, rather than $greeting = "Hello World" ; echo $greeting ; Seriously... What is that all about. Hehehe... For those that have tried studying online, you will know where that "Hello Word" comes from.

Basically... My point is perseverance. If PHP and MySQL were easy, where would the challenge be. If you give up, my opinion would be that this is not for you.

PS: I am only finishing off chapter 2 now, so not sure what the rest of the book entails & whether it will hold more errors. But I look forward to conquering them.

#43 blackcloud84

blackcloud84

    New Member

  • Members
  • Pip
  • 1 posts

Posted 08 May 2010 - 01:49 AM

I don't know how many of you are still having the problem, but get rid of the single quotes around your mysqli_connect string. It should look something like this:

$dbc = mysqli_connect(localhost, ********, ********, aliendatabase)
or die('Error connecting to MySQL server.');

It should work from there. Good Luck.

#44 learninberlin

learninberlin

    New Member

  • Members
  • Pip
  • 1 posts

Posted 22 July 2010 - 07:16 AM

QUOTE (blackcloud84 @ May 8 2010, 01:49 AM) <{POST_SNAPBACK}>
I don't know how many of you are still having the problem, but get rid of the single quotes around your mysqli_connect string. It should look something like this:

$dbc = mysqli_connect(localhost, ********, ********, aliendatabase)
or die('Error connecting to MySQL server.');

It should work from there. Good Luck.


Thanks. YES... this worked beautifully. WONDERFUL. EASY. using the mysqli functions. rolleyes.gif

I was a little suspicious of those single quotes shown in the book within the mysqli_connect() function since the other mysqli functions did not appear to use quotes around variables. so i would assume this is a typo or mistake in the book.

#45 pinginsehat

pinginsehat

    New Member

  • Members
  • Pip
  • 2 posts
  • Location:http://pinginsehat.info

Posted 04 August 2010 - 09:04 AM

i have error too. i will take a look to any reply here. thx

#46 NickoCP

NickoCP

    New Member

  • Members
  • Pip
  • 8 posts

Posted 04 April 2011 - 08:40 AM

Hello,

I am having the same error when I submit my form. I have read through every single reply to this forum and tried absolutely everything but still the problem persists. Please could someone offer some support?

--
Nick
(NickoCP)

#47 drewdin

drewdin

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 608 posts
  • Gender:Male
  • Location:Boston

Posted 07 April 2011 - 12:55 PM

post your code so we can take a look, also are you testing on your local machine or a web server?

#48 Atarstar

Atarstar

    New Member

  • Members
  • Pip
  • 5 posts

Posted 04 November 2011 - 12:04 PM

post your code so we can take a look, also are you testing on your local machine or a web server?


Hi I can't make this work- can anyone help please? thanks- here is the code that I have..

$dbc = mysql_connect('localhost','***','***,'aliensdatabase')
or die('Error connecting to MySQL server.');

mysql_select_db('aliensdatabase');

$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 = mysql_query($query)
or die('Error querying database.');

mysql_close($dbc);

#49 ChenQihang

ChenQihang

    New Member

  • Members
  • Pip
  • 1 posts

Posted 05 November 2011 - 08:47 PM

I have met the same problem... but in my case, the thing is that I made a typing mistake when I was creating the aliens_abduction table. I typed 'when_it_happend' instead of 'when_it_happened'. So the the mysqli_query() has trouble finding out the right column I specified in the report.php. I hope some of you might find it helpful if you made the same stupid mistake as I, haha~

#50 drewdin

drewdin

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 608 posts
  • Gender:Male
  • Location:Boston

Posted 05 November 2011 - 09:36 PM

also change

or die('Error querying database.');

to or die (mysqli_error($dbc))

and let us know what the error is

#51 Atarstar

Atarstar

    New Member

  • Members
  • Pip
  • 5 posts

Posted 07 November 2011 - 07:20 AM

also change

or die('Error querying database.');

to or die (mysqli_error($dbc))

and let us know what the error is


Thanks for the reply. I had to leave for a couple of days. Unfortunately now when I try it, I no longer even get an error message- the page is simply blank.
I am so frustrated..

#52 lemuel

lemuel

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 577 posts

Posted 07 November 2011 - 08:41 AM

You have to use either the mysql functions or the mysqli functions. The mysqli ("i" for "improved" is recommended). The correct syntax for mysql is:
$dbc = mysql_connect('localhost','***','***')
or die('Error connecting to MySQL server.');

mysql_select_db('aliensdatabase');

The database name is not included in the connect statement. Is there a reason you are not using mysqli?

#53 Atarstar

Atarstar

    New Member

  • Members
  • Pip
  • 5 posts

Posted 07 November 2011 - 11:22 AM

You have to use either the mysql functions or the mysqli functions. The mysqli ("i" for "improved" is recommended). The correct syntax for mysql is:

$dbc = mysql_connect('localhost','***','***')
or die('Error connecting to MySQL server.');

mysql_select_db('aliensdatabase');

The database name is not included in the connect statement. Is there a reason you are not using mysqli?


Actually the database name is there. I am using mysql without the "i" because after going through so many posts in numerous places, I read that it would work better without it.

#54 lemuel

lemuel

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 577 posts

Posted 07 November 2011 - 07:24 PM

Let me re-phrase that:
The database name should not be included in the connect statement when using mysql_connect.

Suggestion: use the "i" version.

#55 Atarstar

Atarstar

    New Member

  • Members
  • Pip
  • 5 posts

Posted 08 November 2011 - 08:00 AM

Let me re-phrase that:
The database name should not be included in the connect statement when using mysql_connect.

Suggestion: use the "i" version.


Hi thanks for trying to help. I have tried it all different ways and nothing works. I did open the php_error.log and it keeps repeating this:

PHP Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /Users/****/Sites/phpexamples/ch02/initial/aliens/report.php on line 26
PHP Warning: mysqli_query() expects at least 2 parameters, 1 given in /Users/****/Sites/phpexamples/ch02/initial/aliens/report.php on line 33

This is how my code stands now:
$dbc = mysql_connect('localhost', '****', '****')
or die('Error connecting to MySQL server.');

mysql_select_db('aliensdatabase');

$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 ('****', '****', '3 days ago', '6 hours', 'two', " .
"'blue', 'baby', 'no', 'nothing', 'email***')";

$result = mysql_query($query)
or die('Error querying database.');

mysql_close($dbc);

Edited by Atarstar, 08 November 2011 - 08:01 AM.


#56 lemuel

lemuel

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 577 posts

Posted 08 November 2011 - 09:25 AM

I'm confused. The errors indicate problems with running mysqli commands. But the code you posted only shows use of mysql, no "i". Are you sure you're running the same code that you are posting?

#57 Atarstar

Atarstar

    New Member

  • Members
  • Pip
  • 5 posts

Posted 08 November 2011 - 09:39 AM

I'm confused. The errors indicate problems with running mysqli commands. But the code you posted only shows use of mysql, no "i". Are you sure you're running the same code that you are posting?


I am a total newbie! Thank you so much for all your time- it is really appreciated.

Ok, it works now. What I did- I went back and copied the final code in the chapter and took out the select db after the initial connect. So I reinserted the database name in the connect options. Then I was getting an error in the log about indexes. I went into phpmyadmin and figured out that I needed to designate a PRIMARY key- which the book doesn't talk about at all. I know this all sounds confused but after moving stuff around it does now work! YEAH- I almost gave up...if you would like to see the final code that works- here it is- and THANK YOU once again.

$dbc = mysqli_connect('localhost', 'root', '****', 'aliensdatabase')
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);

#58 derajfast

derajfast

    New Member

  • Members
  • Pip
  • 4 posts

Posted 25 December 2011 - 01:17 PM

I too am having the same issue, and have tried all the fixes listed here to no avail. Can anyone tell me what they see wrong with my code?

Thanks,


<?php
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$alien_description = $_POST['aliendescription'];
$fang_spotted = $_POST['fangspotted'];
$email = $_POST['email'];
$how_many = $_POST['howmany'];
$what_they_did = $_POST['whattheydid'];
$other = $_POST['other'];

$dbc = mysqli_connect('localhost:8889', 'root', 'root', '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 commentsd: ' . $other . '<br />';
echo 'Your email address is '. $email;

?>

#59 drewdin

drewdin

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 608 posts
  • Gender:Male
  • Location:Boston

Posted 25 December 2011 - 07:29 PM

add or die(mysqli_error()); to your code and see what the problems are

#60 derajfast

derajfast

    New Member

  • Members
  • Pip
  • 4 posts

Posted 26 December 2011 - 06:28 AM

add or die(mysqli_error()); to your code and see what the problems are


In which part? When I replace - or die('Error querying database.'); - with - or die(mysqli_error()); - and click submit on the form, I just get a blank page. When I run it with the original code in there, it returns "Error querying database" which means I am able connect to the database, but not query it. Am I doing something wrong?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users