nscherneck Posted August 18, 2007 Share Posted August 18, 2007 Hey guys, Im a noob so go easy. I am trying to learn the basics of using PHP to work with MySQL. I have created a database on my hosting account and want to connect to my database via PHP and echo something that shows that ive successfully connected. Once i establish that what do you guys suggest i learn to do? I was thinking of making a form to insert records into the database then displaying data on another page using loops or something. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/65635-need-some-really-basic-help/ Share on other sites More sharing options...
matthewhaworth Posted August 18, 2007 Share Posted August 18, 2007 $db = new mysqli("LOCALHOST", "USERNAME", "PASSWORD", "DATABASE"); if(!$db) { echo 'Connection failed'; } else { echo 'Connection successful'; } Quote Link to comment https://forums.phpfreaks.com/topic/65635-need-some-really-basic-help/#findComment-327799 Share on other sites More sharing options...
nscherneck Posted August 18, 2007 Author Share Posted August 18, 2007 this is what i get when i tried.... Fatal error: Class 'mysqli' not found in /home/safewaye/public_html/nathan/page2.php on line 102 Quote Link to comment https://forums.phpfreaks.com/topic/65635-need-some-really-basic-help/#findComment-327810 Share on other sites More sharing options...
matthewhaworth Posted August 19, 2007 Share Posted August 19, 2007 It's new to php5.. let's go back to the old style cos it's looking like you don't have php5 installed or the mysqli class. $connection = mysql_connect("localhost","username","password"); if(!$connection) { echo 'mySQL connection failed'; } else { echo 'mySQL connection successful'; $db = mysql_select_db("DatabaseName", $connection if(!$db) { echo 'connection to database failed.'; } else { echo 'connection to database successful'; } Quote Link to comment https://forums.phpfreaks.com/topic/65635-need-some-really-basic-help/#findComment-327819 Share on other sites More sharing options...
nscherneck Posted August 19, 2007 Author Share Posted August 19, 2007 excellent, i made that one work. my host is using PHP 5.1.4. Any suggestions or tutorials to do now that ive got my connection? thank you! Quote Link to comment https://forums.phpfreaks.com/topic/65635-need-some-really-basic-help/#findComment-328187 Share on other sites More sharing options...
nscherneck Posted August 20, 2007 Author Share Posted August 20, 2007 im having another problem. i have created a table within my database and inserted a record. i have three columns, one being a primary key. so i am querying the database like so... $connection = mysql_connect("localhost","safewaye_nathan","daniel"); if(!$connection) { echo "<br />mySQL connection failed."; } else { echo "<br />mySQL connection successful."; $db = mysql_select_db("safewaye_nathan_test", $connection); if(!$db) { echo "<br />connection to database failed."; } else { echo "<br />connection to database successful."; } } $select = "SELECT * FROM Employees order by name"; $data = mysql_query($select, $connection) or die(mysql_error()); echo "<br />" . $data; the result i get is.... Resource id #4 this isnt my one record, and it isnt echoing three columns. what am i doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/65635-need-some-really-basic-help/#findComment-328473 Share on other sites More sharing options...
The Little Guy Posted August 20, 2007 Share Posted August 20, 2007 This may help you out: db.php <?php $dbHost = "localhost"; //Location Of Database usually its localhost $dbUser = "xxxx"; //Database User Name $dbPass = "xxxx"; //Database Password $dbDatabase = "xxxx"; //Database Name $db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database."); mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database."); ?> index.php <?php include 'db.php'; if(!isset($_POST['submit'])){ $firstName = addslashes($_POST['first_name']); mysql_query("insert INTO names (`firstName`) VALUES ('$firstName')")or die(mysql_query()); unset($firstName); unset($_POST['submit']); } $sql = mysql_query("SELECT * FROM names"); echo '<h1>Inserted Names</h1>'; while($row = mysql_fetch_array($sql)){ echo '<p>'.stripslashes($row['firstName']).'</p>'; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> First name: <input type="text" name="first_name"> <input type="submit" name="submit" value="Insert"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/65635-need-some-really-basic-help/#findComment-328547 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.