Can't seem to connect to mysql database and output results

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frustrated777
    New Member
    • Mar 2007
    • 6

    Can't seem to connect to mysql database and output results

    I'm new to mysql but seem to understand it enough to do what simple stuff I need but I can't get even this basic script to echo what is in the one colum, one row table.
    I am able to connect now it seems since I don't get any errors anymore in logs or browser.
    Im trying to get this working first then I need to access it from another domain and write to it.
    I only need a reference # that I can read and then add 1 to it. I tried to do it with a text file with fread and fwrite the way I used to but now I need to be able to write to it from other domains and can't figure it out to save my life.
    I was told mysql database would work. Any help would be GREATLY appreciated. I should be able to figure most of it out myself when I get some basic functionality going.
    Thanks.
    -----------------------------------------------------------
    PHP Version 4.3.9
    phpMyAdmin 2.6.4-pl3
    MySQL 4.1.20 running on localhost:3306 as counterlead2@lo calhost
    -------------------------------------------------------------
    SCRIPT:
    <?

    $mysql_server = 'localhost';
    $mysql_user = "counterlea d";
    $mysql_pass = "fhujeduu";
    $mysql_db = "leadsnumfi le";

    $ntable = 'ntable';
    $column = 'number';

    mysql_connect($ mysql_server,$m ysql_user,$mysq l_pass) or die("Unable toconnect1 to database");

    mysql_select_db ($mysql_db) or die("Unable toconnect2 to database");

    $sql = 'SELECT * FROM `ntable` LIMIT 0, 30 ' or die(mysql_error );

    echo "$sql";


    ?>
  • amrhi
    New Member
    • Sep 2006
    • 22

    #2
    Originally posted by frustrated777
    I'm new to mysql but seem to understand it enough to do what simple stuff I need but I can't get even this basic script to echo what is in the one colum, one row table.
    I am able to connect now it seems since I don't get any errors anymore in logs or browser.
    Im trying to get this working first then I need to access it from another domain and write to it.
    I only need a reference # that I can read and then add 1 to it. I tried to do it with a text file with fread and fwrite the way I used to but now I need to be able to write to it from other domains and can't figure it out to save my life.
    I was told mysql database would work. Any help would be GREATLY appreciated. I should be able to figure most of it out myself when I get some basic functionality going.
    Thanks.
    -----------------------------------------------------------
    PHP Version 4.3.9
    phpMyAdmin 2.6.4-pl3
    MySQL 4.1.20 running on localhost:3306 as counterlead2@lo calhost
    -------------------------------------------------------------
    SCRIPT:
    <?

    $mysql_server = 'localhost';
    $mysql_user = "counterlea d";
    $mysql_pass = "fhujeduu";
    $mysql_db = "leadsnumfi le";

    $ntable = 'ntable';
    $column = 'number';

    mysql_connect($ mysql_server,$m ysql_user,$mysq l_pass) or die("Unable toconnect1 to database");

    mysql_select_db ($mysql_db) or die("Unable toconnect2 to database");

    $sql = 'SELECT * FROM `ntable` LIMIT 0, 30 ' or die(mysql_error );

    echo "$sql";


    ?>
    you should use
    $sql1=mysql_que ry($sql);
    to show your data you should use
    while ($row=mysql_fet ch_array($sql1) ){
    echo "$row";
    }
    You still must learn alo of about mysql script try to open
    http://www.tizag.com

    Comment

    • frustrated777
      New Member
      • Mar 2007
      • 6

      #3
      Thank you that helps but now my browser output is:
      Array

      My database seems to be connecting fine. I'm at a loss.

      Comment

      • frustrated777
        New Member
        • Mar 2007
        • 6

        #4
        I changed the WHILE statement to:
        echo $sql1;

        and I get browser output:
        Resource id #3

        Still at a loss but plugging away.

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          The fact that you freely exchange part of the shown sample (while statement) with an echo and don't have a clue as what a resource id is, makes me advise you to follow some basic tutorials on MySQL (and maybe PHP).

          You cannot just learn databases and languages by trial and error or fiddling around. So here are some tutorials on the basics of MySQL:


          Founded in 1997, DEVShed is the perfect place for web developers to learn, share their work, and build upon the ideas of others.


          Learn how to use MySQL with PHP in Tizag.com's MySQL and PHP lesson.





          Using MySQL from PHP part 1
          Using MysQL from PHP part 2

          Ronald :cool:

          Comment

          • frustrated777
            New Member
            • Mar 2007
            • 6

            #6
            thank you for your reply. The resources you listed are very helpful.
            I have to disagree about trial and error as a way to learn. I need to learn this fast and get something up running now. I'm not trying to create some complex database. I am just trying to create a one column, one row table i a single database that I can read AND write to from different domain. At this point I just need to get it to echo the single value to see that mysql is running correctly so I can move on to the next step. I added two more rows for a total of three just for experimental purposes and now get browser output:
            ArrayArrayArray

            The data in the table is:
            22
            33
            55

            Type=text MIME: text/plain

            Comment

            • frustrated777
              New Member
              • Mar 2007
              • 6

              #7
              This the presesnt script:

              <?

              $mysql_server = 'localhost';
              $mysql_user = "counterlea d"; // Your Database Username eg: "you_you"
              $mysql_pass = "fhujeduu"; // Your Database Password eg: "mypass"
              $mysql_db = "leadsnumfi le"; // Your Database Name eg: "you_you"

              $ntable = 'ntable';
              $column = 'number';

              mysql_connect($ mysql_server,$m ysql_user,$mysq l_pass) or die("Unable toconnect1 to database");

              mysql_select_db ($mysql_db) or die("Unable toconnect2 to database");

              $sql = ("SELECT * FROM ntable");
              $sql1=mysql_que ry($sql);
              while ($row=mysql_fet ch_array($sql1) ){
              echo "$row";
              }
              ?>

              Comment

              • frustrated777
                New Member
                • Mar 2007
                • 6

                #8
                Finally got it all worked out. Thanks for your help.

                Comment

                Working...