Jump to content

Newbie getting error selecting db


karnegyhall

Recommended Posts

Hi i'm new to coding and php.  Is there a problem with this code?  I have checked and re-checked the host/db/table names.  Getting error:  "cannot select db: sportstalkadmin"

 

<?php

$athleteid = $_POST['name'];

$conn = mysql_connect("sportstalkadmin.db.********.hostedresource.com", "(username)", "(password)");

if (!$conn)

{

  die('Could not connect: ' . mysql_error());

}

$select = mysql_select_db('sportstalkadmin', $conn);

if ($select)

mysql_query("INSERT INTO tbathletepics (AthleteID) VALUES ('$athleteid')");

{

  die('could not select db: sportstalkadmin ' . mysql_error());

}

if ($select);

echo "your data has been posted, testing complete..";

{

  die('your data was not posted. ' . mysql_error());

}

$id = mysql_query("SELECT * FROM athletepics");

while($row = mysql_fetch_array($id))

  {

  echo $row['AthleteID'];

  echo "<br />";

  }

mysql_close($conn);

?>

Link to comment
Share on other sites

Your if doesn't make sense.

 

<?php
...
if (!$select)
{
   /* Code if $select is false */
}
....
?>

 

Not this :

 

<?php
...
if (!$select)
(???)     <-- don't put code there
{
   /* Code if $select is false */
}
....
?>

 

Don't put a ; after a if like that if ($select);

 

And you can use the [ code] [/code] tags when you post on forums it's easier to read.

Link to comment
Share on other sites

Ok, thank you for the help.  I think i'm making progress.

I changed the if statement to:

<code>

if ($select)

{

  die('your data was not posted. ' . mysql_error());

}

</code>

 

now i'm getting the following error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/m/i/k/mikekarnas/html/TEST1.php on line 29

 

.. hate to bug you more, but i do appreciate your help.

Link to comment
Share on other sites

i've been testing in PHPMyAdmin on Godaddy.com and now the code looks like this.  now i'm getting

 

Parse error: syntax error, unexpected '>' in /home/content/m/i/k/mikekarnas/html/TEST1.php on line 34

 

<?php
$athleteid = $_POST['name']; 
$conn = mysql_connect("sportstalkadmin.db.******.hostedresource.com", "(username)", "password"); 
if (!$conn)
{
  die('Could not connect: ' . mysql_error());
}
$select = mysql_select_db('sportstalkadmin', $conn);
if (!$select)
{
  die('could not select db: sportstalkadmin ' . mysql_error());
}
$Ins = mysql_query("INSERT INTO athletepics (AtheleteID) VALUES ('$athleteid')");
if ($Ins)
{
  die('your data was not posted. ' . mysql_error());
}
$query = "SELECT intAtheleteID FROM tblathletepics";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
echo $row['intAtheleteID']. ";
echo "<br />";
mysql_close($conn);

Link to comment
Share on other sites

did a bit more testing, and i've finally 'not' received any errors!  however now my result is simply a '0'!  does this have to do with my fetch statement? 

 

$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
echo $row['intAtheleteID'];
mysql_close($conn);
?>

Link to comment
Share on other sites

latest update..

this is my script now.  currently i am getting "your data was not posted".  i've also included the form that $athleteid is getting Post data from.

 

<?php
$athleteid = $_POST['name']; 
$conn = mysql_connect("sportstalkadmin.db.******.hostedresource.com", "(username)", "password"); 
if (!$conn)
{
  die('Could not connect: ' . mysql_error());
}
$select = mysql_select_db('sportstalkadmin', $conn);
if (!$select)
{
  die('could not select db: sportstalkadmin ' . mysql_error());
}
$Ins = mysql_query("INSERT INTO tblathletepics (intAtheleteID) VALUES ('$athleteid')");
if ($Ins)
{
  die('your data was not posted. ' . mysql_error());
}
$query = "SELECT intAtheleteID FROM tblathletepics";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
   echo $row['intAtheleteID']. ";
   echo "<br />";
mysql_close($conn);

 

<html>
<form action="TEST1.php" method="post">
Enter your Athlete ID: <input type="text" name="name" />
<input type="submit" />
</form>
</body>
</html>

Link to comment
Share on other sites

ok, i see your point.  i looked in phpmyadmin and i see the intAtheleteID field Cardinality increasing.

however, my code is still not right.

what i want is to to post the data, then select the data that was just posted, and echo it to the screen.  i just want to have confirmation that what i entered was posted.

any suggestions?

Link to comment
Share on other sites

man, i'm so close.

this is what i get now:

$query = "SELECT intAtheleteID FROM tblathletepics";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
echo $row['intAtheleteID'];
$q = "SELECT LAST_INSERT_ID() FROM tblathletepics";
    return ($q);

 

i figured out it's outputting zeros in place of text.  the 0's represent the default value for each entry i've submitted.  so as far as I'm concerned you have fixed the php-mysql code problem.  thank you.

 

in php myadmin the default value shows 0 for the field i've selected b/c that's the way the dba who built my db made it.  do you know how to change that in phpmyadmin by any chance?

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.