Jump to content

connecting to database


franknu

Recommended Posts

ok

 

I need some help connecting i dont know what it is.

 

Here is my code

 


$host = "localhost";
$username = "townsfin_localho";
$password = "abc123";
$database = "townsfin_alegria";

$db = mysql_connect("$host", "$username", "$password")
or die ('this that ' .mysql_error());
mysql_select_db("$database");




$headline = addslashes ($_POST['headline']);
$body = addslashes($_POST['body']);
$writter = addslashes($_POST['writter']);
$date= addslashes($_POST['date']);
$picture = addslashes($_POST['$picture']);
$user = addslashes($_POST['$user']);
$password= addslashes($_POST['$password']);




if(!$db)

{
echo " Error: could not connect to database.";

exit;
   }

// news insert 

$sql="INSERT INTO `news`(`headline`,`body`,`writter`,`date`,`picture`, `user`,`password`)
VALUES ('".$headline."','".$body."','".$writter."','".$date."','".$picture."','".$user."','".$password."')";

$result = mysql_query($sql);
echo mysql_error();

if($result)
         {
echo mysql_affected_rows()." .$writter your News has been inserted. <br>";
          }

 

this is my display

 

No database selected

Link to comment
https://forums.phpfreaks.com/topic/39976-connecting-to-database/
Share on other sites

i did, i kept gettin the same error message

 

i also change it like this

$host = "localhost";
$username = "townsfin_localho";
$password = "abc123";
$database = "townsfin_alegria";

$db = mysql_connect($host, $username, $password)
or die ('this that ' .mysql_error());
mysql_select_db($database);

ok  this is the error message

 

Error: could not connect to database.

 

 



$host = "localhost";
$username = "townsfin_localho";
$password = "abc123";
$database = "townsfin_alegria";

$conn = mysql_connect($host, $username, $password)
or die ('this that ' .mysql_error());
$db = mysql_select_db($database, $conn);



$headline = addslashes ($_POST['headline']);
$body = addslashes($_POST['body']);
$writter = addslashes($_POST['writter']);
$date= addslashes($_POST['date']);
$picture = addslashes($_POST['$picture']);
$user = addslashes($_POST['$user']);
$password= addslashes($_POST['$password']);




if(!$db)

{
echo " Error: could not connect to database.";

exit;
   }

// news insert 

$sql="INSERT INTO `news`(`headline`,`body`,`writter`,`date`,`picture`, `user`,`password`)
VALUES ('".$headline."','".$body."','".$writter."','".$date."','".$picture."','".$user."','".$password."')";

$result = mysql_query($sql);
echo mysql_error();

if($result)
         {
echo mysql_affected_rows()." .$writter your News has been inserted. <br>";
          }

 

    // pictures upload

A.) put an or die() statement next to your mysql_select_db to see if it's having an error there.

 

B.) get rid of that entire if(!$db) piece of code

 

Code should look like this:

 

$host = "localhost";
$username = "townsfin_localho";
$password = "abc123";
$database = "townsfin_alegria";

$conn = mysql_connect($host, $username, $password)
or die ('this that ' .mysql_error());
$db = mysql_select_db($database, $conn) or die('Could not connect to database. ' .mysql_error());



$headline = addslashes ($_POST['headline']);
$body = addslashes($_POST['body']);
$writter = addslashes($_POST['writter']);
$date= addslashes($_POST['date']);
$picture = addslashes($_POST['$picture']);
$user = addslashes($_POST['$user']);
$password= addslashes($_POST['$password']);


// news insert 

$sql="INSERT INTO `news`(`headline`,`body`,`writter`,`date`,`picture`, `user`,`password`)
VALUES ('".$headline."','".$body."','".$writter."','".$date."','".$picture."','".$user."','".$password."')";

$result = mysql_query($sql);
echo mysql_error();

if($result)
         {
echo mysql_affected_rows()." .$writter your News has been inserted. <br>";
          }

Archived

This topic is now archived and is closed to further replies.

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