Forum Moderators: coopster

Message Too Old, No Replies

Error querying database

Warning: mysql_query(): supplied argument is not a valid MySQL-Link ...

         

tbritny

3:37 pm on Apr 16, 2009 (gmt 0)

10+ Year Member



Hi! I am working on learning how to get info into a database from a web form, but I have hit a road block pretty early on. My problem started when I entered the code from $dbc to mysql_close($dbc). More specifically this line " $result = mysql_query($dbc, $query) " is what is noted in the error message. I checked to make sure my location, user name, password, database name and table name were correct with the company and that seems to be fine.

This is the error message:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /web/web/web/web/web/web/website.com/html/test/report.php on line 35
Error querying database.

Here is my code (copied from a book tutorial):

<?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 = mysql_connect('location', 'username', 'password', 'database_name')
or die('Error connecting to MySQL server.');

$query = "INSERT INTO table_name (first_name, last_name, " .
"when_it_happened, how_long, how_many, alien_description, " .
"what_they_did, fang_spotted, other, email) " .
"VALUES ('Sally', 'Jones', '3 days ago', '1 day', 'four', " .
"'green with six tentacles', 'We just talked and played with a dog', " .
"'yes', 'I may have seen your dog. Contact me.', " .
"'sally@gregs-list.net')";

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

mysql_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;
?>

Thanks for any help you can give me! Let me know if more information is needed,

d40sithui

6:31 pm on Apr 16, 2009 (gmt 0)

10+ Year Member



You're getting this error because you misplaced the connection resource with the query string.

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

Should be::


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

OR::


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

lestresballoons

1:33 pm on Apr 26, 2009 (gmt 0)

10+ Year Member



tbritny.. I am also learning from the Head First PHP & MySQL. Have exactly the same problem.

Error querying the database !

Anyd40sithui, tried typing in thos other mysqli_query() commands and I got errors !

Help !

coopster

12:23 pm on Apr 27, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, tbritny.
Welcome to WebmasterWorld, lestresballoons.

There is a difference between mysql and mysqli. You have to use the correct error functions for each.