Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][SQL][MYSQL] Nie można wykonać zapytania
Forum PHP.pl > Forum > Przedszkole
haRacz
Moj problem wyglada nastepujaco.
Robie przyklady z tej ksiazki Head First PHP & MySQL i w drugim rozdziale jest wykonywanie zapytan do bazy.
Robie wszystko dokladnie jak jest podane i nie moze sie polaczyc.
Stwierdzilem, ze odpale ich skrypt (jako, ze ze strony mozna pobrac gotowce) i zobacze co u mnie nie tak, ale w ich skrypcie jest dokladnie tak samo. Tzn. mam ten sam blad:

Kod
<b>Warning</b>:  mysql_query(): supplied argument is not a valid MySQL-Link resource in <b>F:\xampp\htdocs\xampp\test\wyslij.php</b> on line <b>44</b>
   Error querying database


  1. <?php
  2. $dbc = mysqli_connect('localhost', 'har', 'har', 'alien')
  3.    or die('Error connecting to MySQL server.');
  4.  
  5.  $query = "INSERT INTO test (first_name, last_name, when_it_happened, how_long, " .
  6.    "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
  7.    "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
  8.    "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";
  9.  
  10.  $result = mysqli_query($dbc, $query)
  11.    or die('Error querying database.');
  12.  
  13.  mysqli_close($dbc);
  14. ?>
porady-it.pl
Chyba nieco namieszałeś, w komunikacie błędu jest informacja z funkcji mysql_query, więc proponuję najpierw sprawdzić czy przypadkiem w skrypcie nie masz mysql_query zamiast mysqli_query, gdyż poniższy skrypt który podałeś działa prawidłowo, a problem pojawił się identyczny jak Twój gdy użyłem mysql_query winksmiley.jpg
haRacz
Tak wyglada formularz
  1.   <h2>Aliens Abducted Me - Report an Abduction</h2>
  2.   <p>Share your story of alien abduction:</p>
  3.   <form method="post" action="report.php">
  4.   <label for="firstname">First name:</label>
  5.   <input type="text" name="firstname" /><br />
  6.   <label for="lastname">Last name:</label>
  7.   <input type="text" name="lastname" /><br />
  8.   <label for="email">What is your email address?</label>
  9.   <input type="text" name="email" /><br />
  10.   <label for="whenithappened">When did it happen?</label>
  11.   <input type="text" name="whenithappened" /><br />
  12.   <label for="howlong">How long were you gone?</label>
  13.   <input type="text" name="howlong" /><br />
  14.   <label for="howmany">How many did you see?</label>
  15.   <input type="text" name="howmany" /><br />
  16.   <label for="aliendescription">Describe them:</label>
  17.   <input type="text" name="aliendescription" size="32" /><br />
  18.   <label for="whattheydid">What did they do to you?</label>
  19.   <input type="text" name="whattheydid" size="32" /><br />
  20.   <label for="fangspotted">Have you seen my dog Fang?</label>
  21.   Yes <input name="fangspotted" type="radio" value="yes" />
  22.   No <input name="fangspotted" type="radio" value="no" /><br />
  23.   <img src="fang.jpg" width="100" height="175"
  24.   alt="My abducted dog Fang." /><br />
  25.   <label for="other">Anything else you want to add?</label>
  26.   <textarea name="other"></textarea><br />
  27.   <input type="submit" value="Report Abduction" name="submit" />
  28.   </form>



A tak skrypt

  1. <?php
  2.   $first_name = $_POST['firstname'];
  3.   $last_name = $_POST['lastname'];
  4.   $when_it_happened = $_POST['whenithappened'];
  5.   $how_long = $_POST['howlong'];
  6.   $how_many = $_POST['howmany'];
  7.   $alien_description = $_POST['aliendescription'];
  8.   $what_they_did = $_POST['whattheydid'];
  9.   $fang_spotted = $_POST['fangspotted'];
  10.   $email = $_POST['email'];
  11.   $other = $_POST['other'];
  12.  
  13.   $dbc = mysqli_connect('localhost', 'har', 'har', 'alien')
  14.     or die('Error connecting to MySQL server.');
  15.  
  16.   $query = "INSERT INTO test (first_name, last_name, when_it_happened, how_long, " .
  17.     "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
  18.     "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
  19.     "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";
  20.  
  21.   $result = mysqli_query($dbc, $query)
  22.     or die('Error querying database.');
  23.  
  24.   mysqli_close($dbc);
  25.  
  26.   echo 'Thanks for submitting the form.<br />';
  27.   echo 'You were abducted ' . $when_it_happened;
  28.   echo ' and were gone for ' . $how_long . '<br />';
  29.   echo 'Number of aliens: ' . $how_many . '<br />';
  30.   echo 'Describe them: ' . $alien_description . '<br />';
  31.   echo 'The aliens did this: ' . $what_they_did . '<br />';
  32.   echo 'Was Fang there? ' . $fang_spotted . '<br />';
  33.   echo 'Other comments: ' . $other . '<br />';
  34.   echo 'Your email address is ' . $email;
  35. ?>


I nie wazne czy mam mysqli czy mysql to i tak mam ten sam blad.
DREEMus
  1. <?php
  2. $dbc = mysql_connect('localhost', 'har', 'har', 'alien')
  3.    or die('Error connecting to MySQL server.');
  4.  
  5.  $query = "INSERT INTO test (first_name, last_name, when_it_happened, how_long, " .
  6.    "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
  7.    "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
  8.    "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";
  9.  
  10.  $result = mysql_query($dbc, $query)
  11.    or die('Error querying database.');
  12.  
  13.  mysql_close($dbc);
  14. ?>
porady-it.pl
Sprubuj tak:
  1. <?php
  2. $dbc = mysql_connect('localhost', 'har', 'har') or die('Error connecting to MySQL server.');
  3. mysql_select_db('alien');
  4.  
  5. $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
  6.   "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
  7.   "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
  8.   "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";
  9.  
  10. $result = mysql_query($query) or die('Error querying database.');
  11. ?>
haRacz
Nie dziala, ani jedno ani drugie.
PawelC
Zobacz tak:
  1. <?php
  2.  
  3. mysql_connect('localhost', 'har', 'har') or die('Error connecting to MySQL server.');
  4. mysql_select_db('alien');
  5.  
  6. $first_name = $_POST['firstname'];
  7. $last_name = $_POST['lastname'];
  8. $when_it_happened = $_POST['whenithappened'];
  9. $how_long = $_POST['howlong'];
  10. $how_many = $_POST['howmany'];
  11. $alien_description = $_POST['aliendescription'];
  12. $what_they_did = $_POST['whattheydid'];
  13. $fang_spotted = $_POST['fangspotted'];
  14. $email = $_POST['email'];
  15. $other = $_POST['other'];
  16. $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')";
  17.  
  18. mysql_query($query) or die('Error querying database.');
  19. ?>
haRacz
Tez nie dziala  sad.gif

OK, teraz dziala, jakos sobie poradzilem smile.gif
Dziex za pomoc smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.