franknu Posted February 24, 2007 Share Posted February 24, 2007 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 More sharing options...
magic2goodil Posted February 25, 2007 Share Posted February 25, 2007 Get rid of the " around this variable: mysql_select_db("$database"); Should be: mysql_select_db($database); Link to comment https://forums.phpfreaks.com/topic/39976-connecting-to-database/#findComment-193298 Share on other sites More sharing options...
franknu Posted February 25, 2007 Author Share Posted February 25, 2007 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); Link to comment https://forums.phpfreaks.com/topic/39976-connecting-to-database/#findComment-193304 Share on other sites More sharing options...
Yesideez Posted February 25, 2007 Share Posted February 25, 2007 I would also edit your original post and remove the username and password. Link to comment https://forums.phpfreaks.com/topic/39976-connecting-to-database/#findComment-193305 Share on other sites More sharing options...
franknu Posted February 25, 2007 Author Share Posted February 25, 2007 dont get it Link to comment https://forums.phpfreaks.com/topic/39976-connecting-to-database/#findComment-193307 Share on other sites More sharing options...
magic2goodil Posted February 25, 2007 Share Posted February 25, 2007 This work? $conn = mysql_connect($host, $username, $password) or die ('this that ' .mysql_error()); $db = mysql_select_db($database, $conn); Link to comment https://forums.phpfreaks.com/topic/39976-connecting-to-database/#findComment-193308 Share on other sites More sharing options...
franknu Posted February 25, 2007 Author Share Posted February 25, 2007 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 Link to comment https://forums.phpfreaks.com/topic/39976-connecting-to-database/#findComment-193310 Share on other sites More sharing options...
magic2goodil Posted February 25, 2007 Share Posted February 25, 2007 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>"; } Link to comment https://forums.phpfreaks.com/topic/39976-connecting-to-database/#findComment-193326 Share on other sites More sharing options...
brentjlaf Posted February 25, 2007 Share Posted February 25, 2007 have you created you database in mysql yet? Link to comment https://forums.phpfreaks.com/topic/39976-connecting-to-database/#findComment-193443 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.