Jump to content

[SOLVED] First time connecting to mysql database: PHP error


NovaArgon

Recommended Posts

This is my first time working with php and databases and I would like some advice trying to connect them.

 

This is my database table fields

 

http://www.tarantuladatabase.com/tf.jpg

 

This is my field values

 

http://www.tarantuladatabase.com/fv.jpg

 

I am using two php files one of them is called connect.php and it only has the information to connect to my database. The other files is called echo.php. The form is at

 

http://www.tarantuladatabase.com/assets/pages/search.html

 

When I try and connect I am getting this error message

 

Warning: include(includes/connect.php) [function.include]: failed to open stream: No such file or directory in /home/e0120590/public_html/tarantuladatabase.com/assets/php/echo.php on line 38

Warning: include() [function.include]: Failed opening 'includes/connect.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/e0120590/public_html/tarantuladatabase.com/assets/php/echo.php on line 38

Warning: mysql_query() [function.mysql-query]: Access denied for user 'e0120590'@'localhost' (using password: NO) in /home/e0120590/public_html/tarantuladatabase.com/assets/php/echo.php on line 40

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/e0120590/public_html/tarantuladatabase.com/assets/php/echo.php on line 40

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/e0120590/public_html/tarantuladatabase.com/assets/php/echo.php on line 43

Warning: mysql_close(): no MySQL-Link resource supplied in /home/e0120590/public_html/tarantuladatabase.com/assets/php/echo.php on line 48

 

I am kinda stuck right here so any advice would be greatly appreciated.

 

[attachment deleted by admin]

Link to comment
Share on other sites

It can't find the include file.  Make sure the path you supply it "includes/connect.php" is referenced correctly to the location of the script running it.

 

A Temporary test might be to supply it the FQDN , like include("http://www.tarantuladatabase.com/path/to/connect.php"); to see if it works, then if it does, work on getting the "../includes/connect.php" type path correct.

 

Solve that first and the mysql stuff should go away.

Link to comment
Share on other sites

Thanks so much for all the advice

 

It looks like that was the problem. I have a new error message so I think I am connected fine and now the problems are just in the echo.php file.

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/e0120590/public_html/tarantuladatabase.com/assets/php/echo.php on line 43

 

Here we go lol

Link to comment
Share on other sites

Ok guys no more error messages!  ;D

 

You try it out here

 

I think its connecting with the database even though its not valid code but I'll work on that :D So this is my new code in echo.php

 

<div class="c1">
    <table cellspacing="20">
<tr>
        	<th>Habitat</th>
            <th>Humidity</th>
            <th>Speed</th>
            <th>Size</th>
            <th>Location</th>
            <th>Aggression</th>
        </tr>
        
    <?
include("connect.php");
// now you are connected and can query the database
$request = mysql_query("SELECT * FROM common_traits 
WHERE habitat = \"".$_REQUEST['habitat']."\" 
AND humidity = \"".$_REQUEST['humidity']."\"
AND speed = \"".$_REQUEST['speed']."\"
AND size = \"".$_REQUEST['size']."\"
AND location = \"".$_REQUEST['location']."\",
AND aggression = \"".$_REQUEST['aggression']."\"");

// loop through the results with mysql_fetch_array()
if ( @mysql_num_rows($row) > 0 ) {
while($row = mysql_fetch_array($result)){
  echo "
	<tr>
			<td>".$row['habitat']."</td>
			<td>".$row['humidity']."</td>
			<td>".$row['speed']."</td>
			<td>".$row['size']."</td>
			<td>".$row['location']."</td>
			<td>".$row['aggression']."</td>
		</tr>
  ";
}

} else {

echo "<tr><td colspan = 6></td></tr>";

}

// don't forget to close the mysql connection
mysql_close();
?> 	 
</table>

<a href="http://www.tarantuladatabase.com/assets/pages/search.html"><input name="Back" type="button" value="Back" /></a></div>
  </div>

 

Please correct me if i'm wrong but i think my next step in the project is to create a form that will submit data to my database so that I have something to search?

 

I need to add a all of the database field values for genus there are a lot for them. I think about 113 give or take

 

in this pic I have 4 of them added already.

 

fv.jpg

 

I know there is a way to past them into my sql and submit them all at one time but I don't know the format. What I would like to do is creat the file in a .txt file and then copy and past it to the database once its all compiled.

 

Is there any way you can give me any example of the code I would need to submit that to the database?

 

I have also added a species field that will be typed in by my users when they submit a new species into the database. but before I start doing all this I just wanted to check and make sure it was necessary.

 

Just to explain how the submission form will work you select a genus from the drop down then type in the species name in the text box. Click one of each of the bullet points and click the tarantula submission button.

 

This is the submission form

 

I want that to create a new tarantula in my database under the selected genus. Is this adding up?

 

Thanks so much for all the help and advice.

Link to comment
Share on other sites

just create a php file to submit all the data in the form...

 

I would do something like this...

 

first create an array that holds the 113 values..

$genus = array (
'item1',
'item2',
'ECT..',
);

//then create a SQL statement variable
$i=0;
$sql = "INSERT INTO tablename (genus) VALUES ($genus[$i])";


//then make you a while loop that will loop through your Insert into query code 112 times

while($i<=112)
{
mysql_query($sql,$con);
$i++;
}

 

keep in mind this is simply an example. you will have to fill in the gaps here with full code.

 

once you have all the code in place nav to the file in your browser and bam.. database filled.

Link to comment
Share on other sites

Thanks so much how does this look?

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>upload</title>
</head>

<body>
$genus = array (
'Acanthopelma',
'Acanthoscurria',
'Annandaliella',
'Anoploscelus',
'Aphonopelma',
'Augacephalus',
'Avicularia',
'Batesiella',
'Bonnetina',
'Brachionopus',
'Brachypelma',
'Cardiopelma',
'Catumiri',
'Ceratogyrus',
'Chaetopelma',
'Chilobrachys',
'Chromatopelma',
'Citharacanthus',
'Citharischius',
'Citharognathus',
'Clavopelma',
'Coremiocnemis',
'Crassicrus',
'Cratorrhagus',
'Cyclosternum',
'Cyriocosmus',
'Cyriopagopus',
'Cyrtopholis',
'Davus',
'Encyocratella',
'Encyocrates',
'Ephebopus',
'Euathlus',
'Eucratoscelus',
'Eumenophorus',
'Eupalaestrus',
'Euphrictus',
'Grammostola',
'Hapalopus',
'Hapalotremus',
'Haploclastus',
'Haplocosmia',
'Haplopelma',
'Harpactira',
'Harpactirella',
'Hemiercus',
'Hemirrhagus',
'Heteroscodra',
'Heterothele',
'Holothele',
'Homoeomma',
'Hysterocrates',
'Idiothele',
'Iracema',
'Iridopelma',
'Ischnocolus',
'Lampropelma',
'Lasiodora',
'Lasiodorides',
'Loxomphalia',
'Loxoptygus',
'Lyrognathus',
'Mascaraneus',
'Megaphobema',
'Melloina',
'Melloleitaoina',
'Metriopelma',
'Monocentropus',
'Myostola',
'Neostenotarsus',
'Nesiergus',
'Nesipelma',
'Nhandu',
'Oligoxystre',
'Ornithoctonus',
'Orphnaecus',
'Ozopactus',
'Pachistopelma',
'Pamphobeteus',
'Paraphysa',
'Phlogiellus',
'Phoneyusa',
'Phormictopus',
'Phormingochilus',
'Plesiopelma',
'Plesiophrictus',
'Poecilotheria',
'Proshapalopus',
'Psalmopoeus',
'Pseudhapalopus',
'Pseudoligoxystre',
'Pterinochilus',
'Reversopelma',
'Schismatothele',
'Schizopelma',
'Selenobrachys',
'Selenocosmia',
'Selenogyrus',
'Selenotholus',
'Selenotypus',
'Sericopelma',
'Sickius',
'Sphaerobothria',
'Stichoplastoris',
'Stromatopelma',
'Tapinauchenius',
'Theraphosa',
'Thrigmopoeus',
'Thrixopelma',
'Tmesiphantes',
'Trichognathella',
'Vitalius',
'Xenesthis',
'Yamia',
);

//then create a SQL statement variable
$i=0;
$sql = "INSERT INTO tablename (genus) VALUES ($genus[$i])";


//then make you a while loop that will loop through your Insert into query code 114 times

while($i<=114)
{
mysql_query($sql,$con);
$i++;
}
</body>
</html>

Link to comment
Share on other sites

Change

//then create a SQL statement variable
$i=0;
$sql = "INSERT INTO tablename (genus) VALUES ($genus[$i])";


//then make you a while loop that will loop through your Insert into query code 114 times

while($i<=114)
{
mysql_query($sql,$con);
$i++;
}

to

foreach($genus as $value)
{
     $sql = "INSERT INTO tablename (genus) VALUES ($value)";
     mysql_query($sql, $con);
}

 

Also make sure you have connected to your mysql server/database too before running the code.

 

Link to comment
Share on other sites

ok just like this

 

<body>
<?php
include("connect.php");
$genus = array (
'Aphonopelma',
'Augacephalus',
'Avicularia',
'Batesiella',
'Bonnetina',
'Brachionopus',
'Brachypelma',
'Cardiopelma',
'Catumiri',
'Cerato*****',
'Chaetopelma',
'Chilobrachys',
'Chromatopelma',
'Citharacanthus',
'Citharischius',
'Citharognathus',
'Clavopelma',
'Coremiocnemis',
'Crassicrus',
'Cratorrhagus',
'Cyclosternum',
'Cyriocosmus',
'Cyriopagopus',
'Cyrtopholis',
'Davus',
'Encyocratella',
'Encyocrates',
'Ephebopus',
'Euathlus',
'Eucratoscelus',
'Eumenophorus',
'Eupalaestrus',
'Euphrictus',
'Grammostola',
'Hapalopus',
'Hapalotremus',
'Haploclastus',
'Haplocosmia',
'Haplopelma',
'Harpactira',
'Harpactirella',
'Hemiercus',
'Hemirrhagus',
'Heteroscodra',
'Heterothele',
'Holothele',
'Homoeomma',
'Hysterocrates',
'Idiothele',
'Iracema',
'Iridopelma',
'Ischnocolus',
'Lampropelma',
'Lasiodora',
'Lasiodorides',
'Loxomphalia',
'Loxoptygus',
'Lyrognathus',
'Mascaraneus',
'Megaphobema',
'Melloina',
'Melloleitaoina',
'Metriopelma',
'Monocentropus',
'Myostola',
'Neostenotarsus',
'Nesiergus',
'Nesipelma',
'Nhandu',
'Oligoxystre',
'Ornithoctonus',
'Orphnaecus',
'Ozopactus',
'Pachistopelma',
'Pamphobeteus',
'Paraphysa',
'Phlogiellus',
'Phoneyusa',
'Phormictopus',
'Phormingochilus',
'Plesiopelma',
'Plesiophrictus',
'Poecilotheria',
'Proshapalopus',
'Psalmopoeus',
'Pseudhapalopus',
'Pseudoligoxystre',
'Pterinochilus',
'Reversopelma',
'Schismatothele',
'Schizopelma',
'Selenobrachys',
'Selenocosmia',
'Seleno*****',
'Selenotholus',
'Selenotypus',
'Sericopelma',
'Sickius',
'Sphaerobothria',
'Stichoplastoris',
'Stromatopelma',
'Tapinauchenius',
'Theraphosa',
'Thrigmopoeus',
'Thrixopelma',
'Tmesiphantes',
'Trichognathella',
'Vitalius',
'Xenesthis',
'Yamia',
);

// $sql = "INSERT INTO tablename (genus) VALUES ('".$genus[$i]."')";


//then make you a while loop that will loop through your Insert into query code 110 times -- Edit lol?

/*
while($i<=110)
{
mysql_query($sql,$con);
$i++;
}*/

for ($x=0;$x<count($genus);$x++) {
  $sql = "INSERT INTO common_traits(genus) VALUES ('".$genus[$x]."')";
  echo "$sql <br>";  
  //mysql_query($sql);  // Uncomment when your ready for the real deal
}
?> 

Link to comment
Share on other sites

this:

 

for ($x=0;$x<count($genus);$x++) {

  $sql = "INSERT INTO common_traits(genus) VALUES ('".$genus[$x]."')";

  echo "$sql <br>"; 

  //mysql_query($sql);  // Uncomment when your ready for the real deal

}

 

 

is the same as this:

/*

while($i<=110)

{

mysql_query($sql,$con);

$i++;

}*/

Link to comment
Share on other sites

How long should it take to show up in my phpmyadmin?

 

this is exactly what I posted

 

<?php
include("connect.php");
$genus = array (
'Aphonopelma',
'Augacephalus',
'Avicularia',
'Batesiella',
'Bonnetina',
'Brachionopus',
'Brachypelma',
'Cardiopelma',
'Catumiri',
'Ceratogyrus',
'Chaetopelma',
'Chilobrachys',
'Chromatopelma',
'Citharacanthus',
'Citharischius',
'Citharognathus',
'Clavopelma',
'Coremiocnemis',
'Crassicrus',
'Cratorrhagus',
'Cyclosternum',
'Cyriocosmus',
'Cyriopagopus',
'Cyrtopholis',
'Davus',
'Encyocratella',
'Encyocrates',
'Ephebopus',
'Euathlus',
'Eucratoscelus',
'Eumenophorus',
'Eupalaestrus',
'Euphrictus',
'Grammostola',
'Hapalopus',
'Hapalotremus',
'Haploclastus',
'Haplocosmia',
'Haplopelma',
'Harpactira',
'Harpactirella',
'Hemiercus',
'Hemirrhagus',
'Heteroscodra',
'Heterothele',
'Holothele',
'Homoeomma',
'Hysterocrates',
'Idiothele',
'Iracema',
'Iridopelma',
'Ischnocolus',
'Lampropelma',
'Lasiodora',
'Lasiodorides',
'Loxomphalia',
'Loxoptygus',
'Lyrognathus',
'Mascaraneus',
'Megaphobema',
'Melloina',
'Melloleitaoina',
'Metriopelma',
'Monocentropus',
'Myostola',
'Neostenotarsus',
'Nesiergus',
'Nesipelma',
'Nhandu',
'Oligoxystre',
'Ornithoctonus',
'Orphnaecus',
'Ozopactus',
'Pachistopelma',
'Pamphobeteus',
'Paraphysa',
'Phlogiellus',
'Phoneyusa',
'Phormictopus',
'Phormingochilus',
'Plesiopelma',
'Plesiophrictus',
'Poecilotheria',
'Proshapalopus',
'Psalmopoeus',
'Pseudhapalopus',
'Pseudoligoxystre',
'Pterinochilus',
'Reversopelma',
'Schismatothele',
'Schizopelma',
'Selenobrachys',
'Selenocosmia',
'Selenogyrus',
'Selenotholus',
'Selenotypus',
'Sericopelma',
'Sickius',
'Sphaerobothria',
'Stichoplastoris',
'Stromatopelma',
'Tapinauchenius',
'Theraphosa',
'Thrigmopoeus',
'Thrixopelma',
'Tmesiphantes',
'Trichognathella',
'Vitalius',
'Xenesthis',
'Yamia',
);

// $sql = "INSERT INTO tablename (genus) VALUES ('".$genus[$i]."')";


//then make you a while loop that will loop through your Insert into query code 110 times -- Edit lol?

/*
while($i<=110)
{
mysql_query($sql,$con);
$i++;
}*/

for ($x=0;$x<count($genus);$x++) {
  $sql = "INSERT INTO common_traits(genus) VALUES ('".$genus[$x]."')";
  echo "$sql <br>";  
  //mysql_query($sql);  // Uncomment when your ready for the real deal
}
?> 

 

I just get this message on the php page

 

INSERT INTO common_traits(genus) VALUES ('Aphonopelma')

INSERT INTO common_traits(genus) VALUES ('Augacephalus')

INSERT INTO common_traits(genus) VALUES ('Avicularia')

INSERT INTO common_traits(genus) VALUES ('Batesiella')

INSERT INTO common_traits(genus) VALUES ('Bonnetina')

 

 

It repeats over and over for each of the genus

 

Could I not be connecting to the database?

Link to comment
Share on other sites

You're not passing the query to mysql_query function. You have it commented out

  //mysql_query($sql);  // Uncomment when your ready for the real deal

Remove the // at the start of the line and your query will execute. Just echo'ing $sql will not perform the query.

 

Also any reason why you didn't use my code suggestion earlier?

Link to comment
Share on other sites

I'm sorry I thought I was using your code  ??? I must have got it mixed up when I was trying to put it into dreamweaver.

 

Warning: mysql_query() [function.mysql-query]: Access denied for user 'e0120590'@'localhost' (using password: NO) in /home/e0120590/public_html/tarantuladatabase.com/assets/php/fd.php on line 138

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/e0120590/public_html/tarantuladatabase.com/assets/php/fd.php on line 138

INSERT INTO common_traits(genus) VALUES ('Batesiella')

 

Let me look at my code again and see where I missed it.

Link to comment
Share on other sites

no thats the table prefix I dont know if I need it. I just alwas had to use it when connecting to the database with a forum.

 

<?
/*--------- DATABASE CONNECTION INFO---------*/
$hostname="localhost";
$mysql_login="e0120590_tester";
$mysql_password="**Hidden**";
$database="e0120590_tarantuladatabase";

// connect to the database server
if (!($db = mysql_pconnect($hostname, $mysql_login , $mysql_password))){
  die("Can't connect to database server.");    
}else{
  // select a database
    if (!(mysql_select_db("$database",$db))){
      die("Can't connect to database.");
    }
}
?>

Link to comment
Share on other sites

in your previous code you are doing this:

mysql_query('your query') or die(mysql_error())

 

where as with your code you just posted, you need to have this:

mysql_query('your query', $db) or die(mysql_error())

 

OR you just remove the $db = from your connect.php, (be sure to remove the $db from the databse select too)

that would make the connection over the entire page, so theres no need to repeat it (this is what i do)

Link to comment
Share on other sites

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.