Jump to content


Photo

Can't get my php script to connect to mysql


  • Please log in to reply
4 replies to this topic

#1 preacher

preacher
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 26 September 2006 - 07:16 PM

I have set up Apache, Mysql and php and from what i can tell. Its all working well. How i have this script which when i run gives me a blank screen. Some one help me please.

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'softna';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'auth';
mysql_select_db($dbname);
echo "database".$dbname"
?>


#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 26 September 2006 - 07:32 PM

change this

mysql_select_db($dbname);

to this
mysql_select_db($dbname, $conn);

Ray


#3 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 26 September 2006 - 07:50 PM

I noticed that you didn't put ; after the echo
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#4 preacher

preacher
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 27 September 2006 - 01:36 AM

please is there any thing wrond with this script: I just can't get it to add data to the database table.

<html>
<head>
  <title>Book-O-Rama Book Entry Results</title>
</head>
<body>
<h1>Book-O-Rama Book Entry Results</h1>
<?php
  // create short variable names
  $isbn=$_POST['isbn'];
  $author=$_POST['author'];
  $title=$_POST['title'];
  $price=$_POST['price'];

  if (!$isbn || !$author || !$title || !$price)
  {
     echo 'You have not entered all the required details.<br />'
          .'Please go back and try again.';
     exit;
  }

  $isbn = addslashes($isbn);
  $author = addslashes($author);
  $title = addslashes($title);
  $price = doubleval($price);

  @ $db = mysql_pconnect('localhost', 'root', 'softna');

  if (!$db)
  {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }

  mysql_select_db('books',$db);
  $query = "insert into books values
            ('".$isbn."', '".$author."', '".$title."', '".$price."')";
  $result = mysql_query($query);
  if ($result)
      echo  mysql_affected_rows().' book inserted into database.';
?>

</body>
</html>

thanks
EDITED BY WILDTEEN88: PLEASE USE THE CODE/PHP TAGS ([code][/code] or [php][/php]) WHEN POSTING CODE TO THE FORUMS

#5 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 29 September 2006 - 12:52 PM

How many fields you have in the table?? If you have more than 4 then sql is not going to know where to put them. use SET of list the fields then the values for the insert.

IE:

$query = "insert into books (`field1`, `field2`, `field3`, `field4`) values
            ('".$isbn."', '".$author."', '".$title."', '".$price."')";

OR
  $query = "insert into books SET
             `field1` = '".$isbn."', 
             `field2` = '".$author."', 
             `field3` = '".$title."', 
             `field4` = '".$price."'";
Ray






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users