im new to sql and im tired of usuing txt files to store users data.

this is what i have so far but i dont know what to do next. i want to have people add their usernames to a list in an sql database

    mysql_connect("localhost","username","password") or die ("Cant connect to db");
    mysql_select_db("database") or die ("Cant select db");

edit: this is the old way i did it.

$path = 'data.txt';
$contents = get_file_contents($path);
if (isset($_POST['submit']) && $_POST['newline'])
{
    $handle=fopen($path,"w");
    $contents .= stripslashes(trim($_POST['newline']));
    fwrite($handle, $contents) or exit('error writing to file');
    fclose($handle);
?>

    Looks like this tutorial link will come in handy twice (just posted it for another user.) I suggest you read through it extensively.

      BTW, this isn't the best tutorial, but it will get you started. The one thing I hate about it, I have ammended

      I changed...

      $username="username";
      $password="password";
      $database="your_database";
      
      mysql_connect(localhost,$username,$password);
      @mysql_select_db($database) or die( "Unable to select database");
      $query="SELECT * FROM contacts";
      $result=mysql_query($query);
      
      $num=mysql_numrows($result);
      
      mysql_close();
      
      echo "<b><center>Database Output</center></b><br><br>";
      
      $i=0;
      while ($i < $num) {
      
      $first=mysql_result($result,$i,"first");
      $last=mysql_result($result,$i,"last");
      $phone=mysql_result($result,$i,"phone");
      $mobile=mysql_result($result,$i,"mobile");
      $fax=mysql_result($result,$i,"fax");
      $email=mysql_result($result,$i,"email");
      $web=mysql_result($result,$i,"web");
      
      echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";
      
      $i++;
      }
      

      to...

      $username="username";
      $password="password";
      $database="your_database";
      
      mysql_connect(localhost,$username,$password);
      @mysql_select_db($database) or die( "Unable to select database");
      $query="SELECT * FROM contacts";
      $result=mysql_query($query);
      
      echo "<b><center>Database Output</center></b><br><br>";
      
      while ($row = mysql_fetch_array($result)) {
      	$first=$row["first"];
      	$last=$row["last"];
      	$phone=$row["phone"];
      	$mobile=$row["mobile"];
      	$fax=$row["fax"];
      	$email=$row["email"];
      	$web=$row["web"];
      	echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";
      }
      
      mysql_close();
      

      ..which is a MUCH more efficient way to retrieve records.

        Interesting that you say "MUCH more efficient" so conclusively...

        Doesn't this rather depend on your setup? I'm not disagreeing wth you, just looking for your justification.

        The first solution puts all the rows into a user-defined array in one read. Cheap from a database point of view, potentially expensive in terms of RAM. This could also make it scale very poorly when large datasets are involved.

        I've never looked into the internal mechanics of mysql_fetch_array, but I'm guessing that the result array is stored in on the database server rather than in the web server. If that's the case, then it causes a lighter load on the webserver's RAM, but doesn't it cause a connection to the database for each row. This is expensive from a network latency point of view, no? It's faster to move to the next row of an array in RAM, than it is to request another row from a remote database? This would scale more linearly, I guess, but would only be more efficient at high loads.

        What do you think?

          I've never dived into the nuts and bolts. I've just put blind faith in the PHP manual. The part that is inefficient, is the use of mysql_result on each column for every iteration per row.

          From the manual...

          When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result(). Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.

            bretticus wrote:

            The part that is inefficient, is the use of mysql_result on each column for every iteration per row.

            Yeah, it's probably inefficient use of the database server, and it's calling much more code on the app server. Just loading an array ought to be faster.

            But if you're returning thousands of rows, it might be inefficient from the app server side to be storing those in app server RAM, or forcing the app server into swap.

            If it's a small site with everything running on the same physical server, it might get even more confusing.

            I would imagine that the best solution decision is a factor of the frquency of the query, the frequency of change in the data returned (could the result be cached), the number of people who could be running the same query simultanously, and the underlying architecture (size/configuration of servers).

            But we're straying away from the original poster's question. I just wanted to check with you what your reasons for your strong opinion were. I'm always glad to learn new tips and tricks, and I'll certainly use your idea in some applications, I just don't believe it will be more efficient for every case.

            Thanks for the tip.

              i wanted to add just a username not a read the database

              i tried this but i get an error: Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/xxxxxxx/public_html/xxxx/index.php on line 5

              if (isset($_POST['submit']) && $_POST['username']) {
              mysql_connect("localhost","xxxxx","xxxxx") or die ("Cant connect to db");
              mysql_select_db("xxxxx") or die ("Cant select db");
              $sql = 'INSERT INTO `username` (`username`) VALUES ('.$_POST['username'].')';
              mysql_query($sql);
              mysql_close();
              

                if (isset($POST['submit']) && $POST['username']) {
                mysql_connect("localhost","xxxxx","xxxxx") or die ("Cant connect to db");
                mysql_select_db("xxxxx") or die ("Cant select db");
                $sql = 'INSERT INTO username (username) VALUES ('.$_POST['username'].')';
                mysql_query($sql);
                mysql_close();

                The only thing that stands out here is I see no closign bracket for your if statement (}) I t shoudl come right after "mysql_close();" Otherwise, this looks fine.

                  well there is a closeing bracket its just a little further down the line.

                  this is line 5 whats wrong with it???

                  $sql = 'INSERT INTO `username` (`username`) VALUES ('.$_POST['username'].')';

                    Change:
                    $sql = 'INSERT INTO username (username) VALUES ('.$POST['username'].')';
                    to:
                    $sql = "INSERT INTO username (username) VALUES ('.$
                    POST['username'].')";

                      Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/xxx/public_html/xxx/index.php on line 5

                      🙁 this is hopeless isnt it

                      edit: nvm i figured it out

                      $content = stripslashes(trim($_POST['username']));
                      $sql = "INSERT INTO `username` (`username`) VALUES ('$content')";
                      

                        Remove the ticks around username and try to echo out $sql.

                          Write a Reply...