Jump to content

Error in querying database


genzedu777

Recommended Posts

Hi guys,

 

I need help in my code,

 

it states 'Error querying database' when I set

$result = mysql_query($query, $dbc).

 

And strange thing happens when I switched $query and $dbc place, and I  got a different error msg

$result = mysql_query($dbc, $query).

'Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\inetpub\vhosts\championtutor.com\httpdocs\report.php on line 31

Error querying database'

 

Any idea what is happening? Below is my code

Lastly, do you guys have any debug tool software to recommend, so that it can assist me in debugging my code. Thanks

 

 

<?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('*******', '*******', '*******', '*******')

    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 = mysql_query($query, $dbc)

    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;

?>

Link to comment
https://forums.phpfreaks.com/topic/214161-error-in-querying-database/
Share on other sites

Hi,

 

The error states 'No database selected', but that is weird. I have clearly added the database in the server.

 

Just to check with you guys the sequence of the database details

 

$dbc = mysql_connect('localhost', 'DATABASE USERNAME', PASSWORD', 'DATABASE NAME')

 

Is it correct?, first i will imply 'localhost', thereafter the username, perhaps 'cat', then password '1234567', then name of database 'cat_database'.

 

Is there a way to track why my database is not connected?

Hey,

I found unnecessary string concatenation in  query.

 

Use below string as your query.

 $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."')";

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.