Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/65635-need-some-really-basic-help/
Share on other sites

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';
}

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?

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>

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.