Jump to content

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

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.