Connecting to mysql with php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Howard

    Connecting to mysql with php

    Hi,

    I have been racking my brain trying to figure out what I am missing trying
    to follow this book to get a php script to work. I would really appreciate
    if someone could point out my errors. I've been up really late going over
    all the config files. Why do they always scim installation for the
    applications and put it in the Appendix when its the first thing that new
    users need to do. Then we run into these problems because the author takes
    so much for granted

    Thanks

    Howard

    I'm using Apache 2.0.55 with PHP 5.1.2 and MySql AB 5.0.18. Windows XP SP2.

    What I believe I'm missing is how do I tell PHP & Apache where the Databases
    are??

    Here is the pathing my my installs.
    C:/mysql/bin, c:/mysql/data/books, c:/apached/apache2/htdocs for servering
    up web pages, and c:/php.

    Here is the error log in apache, and the script

    [Tue Feb 14 03:31:27 2006] [error] [client 127.0.0.1] PHP Parse error:
    syntax error, unexpected T_STRING in
    C:\\Apache\\Apa che2\\htdocs\\r esults.php on line 30, referer:


    And here is the script. The line 30 is the $result = mysql_query if you
    count starting with 1 or $query = select if the first line is 0.


    <?php

    include config.php;
    include opendb.php;

    // create short variable names

    $searchtype=$_P OST['searchtype'];
    $searchterm=$_P OST['searchterm'];
    $searchterm= trim($searchter m);

    if (!$searchtype || !$searchterm)

    {
    echo 'You have not entered search details. Please go back and try again.';
    exit;
    }

    mysql_select_db ('books');
    $searchtype = addslashes($sea rchtype);
    $searchterm = addslashes($sea rchterm);

    $query = select * from books where ".$searchty pe." like '%".$searchterm ."%'
    " ;
    $result = mysql_query($qu ery);

    $num_results = mysql_num_rows( $result);

    echo '<p>Number of books found: '.$num_results. '</p>';

    for ($i=0; $i <$num_results ; $i++)
    {
    $row = mysql_fetch_arr ay($result);
    echo '<p><strong>'.( $i+1).'. Title: ';
    echo htmlspecialchar s(stripslashes( $row['title']));
    echo '</strong><br />Author: ';
    echo stripslashes($r ow['author']);
    echo '<br />ISBN: ';
    echo stripslashes($r ow['isbn']);
    echo '<br />Price: ';
    echo stripslashes($r ow['price']);
    echo '</p>';
    }
    ?>


  • jonathan.beckett

    #2
    Re: Connecting to mysql with php

    Okay... for a start, you have a quote mark missing at the start of your
    query.

    Here's some boilerplate code that will come in handy...

    // you can put these in a config file
    $db_server = "locahost";
    $db_name = "foo";
    $db_username = "root";
    $db_password = "";

    // description : connects to database and returns handle
    function db_connect(){

    global $db_server;
    global $db_name;
    global $db_username;
    global $db_password;

    $con =@ mysql_connect($ db_server,$db_u sername,$db_pas sword);
    if ($con!=false){
    if (!(mysql_select _db($db_name,$c on))){
    // could not connect
    }
    }
    return $con;
    }


    // how to do a connection...
    $con = db_connect();
    $sql = "SELECT foo FROM bar";
    $result = mysql_query($sq l,$con);
    if ($result!=false ){
    if (mysql_num_rows ($result)>0){
    while ($row =@ mysql_fetch_arr ay($result)){
    // $row now holds an associative array of one row from your query
    // i.e. do something with it
    }
    } else {
    // no records returned
    }
    } else {
    // problem with SQL
    }

    Comment

    • Peter Fox

      #3
      Re: Connecting to mysql with php

      Following on from Howard's message. . .[color=blue]
      >Hi,
      >
      >I have been racking my brain trying to figure out what I am missing trying
      >to follow this book to get a php script to work. I would really appreciate
      >if someone could point out my errors. I've been up really late going over
      >all the config files. Why do they always scim installation for the
      >applications and put it in the Appendix when its the first thing that new
      >users need to do. Then we run into these problems because the author takes
      >so much for granted[/color]

      T_STRING ... [FX whirr of brain cogs ] ... Check your quotes.

      Use a highlighting editor.
      [color=blue]
      >$query = select * from books where ".$searchty pe." like '%".$searchterm ."%'
      >" ;[/color]

      --
      PETER FOX Not the same since the borehole business dried up
      peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
      2 Tees Close, Witham, Essex.
      Gravity beer in Essex <http://www.eminent.dem on.co.uk>

      Comment

      • Dave Mennenoh

        #4
        Re: Connecting to mysql with php

        >>$query = select * from books where ".$searchty pe." like[color=blue][color=green]
        >>'%".$searchte rm."%'" ;[/color][/color]

        Besides missing the leading quotation mark, you might like to know that you
        don't need to do concatenation like this using PHP - so you can simply do
        this:

        $query = select * from books where $searchtype like '%$searchterm%' " ;

        weird huh!

        --
        Dave -




        Comment

        • David Haynes

          #5
          Re: Connecting to mysql with php

          Dave Mennenoh wrote:[color=blue][color=green][color=darkred]
          >>> $query = select * from books where ".$searchty pe." like
          >>> '%".$searchterm ."%'" ;[/color][/color]
          >
          > Besides missing the leading quotation mark, you might like to know that you
          > don't need to do concatenation like this using PHP - so you can simply do
          > this:
          >
          > $query = select * from books where $searchtype like '%$searchterm%' " ;
          >
          > weird huh!
          >[/color]

          You also might like to use a format like:

          $query = <<<EOQ
          SELECT *
          FROM books
          WHERE {$searchtype} LIKE '%{$searchterm} %'
          EOQ;

          Which make the whole query a lot clearer to those used to SQL CLI
          environments.

          -david-

          Comment

          • Howard

            #6
            Re: Connecting to mysql with php

            The script runs but there is no data. I get blank page.

            I copied the info for making a config.php file.
            So I guess I want to try using an include statment to call those files and
            see if
            that works.
            The scripts were copied right from the CD-ROM so I don't know why the
            echo statements don't seem to be doing anything.

            I put a require ('config.php') in the script. Still nothing shows up and no
            errors in
            the apache log.

            I have a feeling that it has to do with a configuration file with php or
            mysql.

            Thanks So far

            Howard


            "David Haynes" <david.haynes2@ sympatico.ca> wrote in message
            news:LXnIf.2372 8$Hj6.11243@fe2 9.usenetserver. com...[color=blue]
            > Dave Mennenoh wrote:[color=green][color=darkred]
            >>>> $query = select * from books where ".$searchty pe." like
            >>>> '%".$searchterm ."%'" ;[/color]
            >>
            >> Besides missing the leading quotation mark, you might like to know that
            >> you don't need to do concatenation like this using PHP - so you can
            >> simply do this:
            >>
            >> $query = select * from books where $searchtype like '%$searchterm%' " ;
            >>
            >> weird huh!
            >>[/color]
            >
            > You also might like to use a format like:
            >
            > $query = <<<EOQ
            > SELECT *
            > FROM books
            > WHERE {$searchtype} LIKE '%{$searchterm} %'
            > EOQ;
            >
            > Which make the whole query a lot clearer to those used to SQL CLI
            > environments.
            >
            > -david-
            >[/color]


            Comment

            • David Haynes

              #7
              Re: Connecting to mysql with php

              Howard wrote:[color=blue]
              > The script runs but there is no data. I get blank page.
              >
              > I copied the info for making a config.php file.
              > So I guess I want to try using an include statment to call those files and
              > see if
              > that works.
              > The scripts were copied right from the CD-ROM so I don't know why the
              > echo statements don't seem to be doing anything.
              >
              > I put a require ('config.php') in the script. Still nothing shows up and no
              > errors in
              > the apache log.
              >
              > I have a feeling that it has to do with a configuration file with php or
              > mysql.
              >
              > Thanks So far
              >
              > Howard
              >
              >
              > "David Haynes" <david.haynes2@ sympatico.ca> wrote in message
              > news:LXnIf.2372 8$Hj6.11243@fe2 9.usenetserver. com...[color=green]
              >> Dave Mennenoh wrote:[color=darkred]
              >>>>> $query = select * from books where ".$searchty pe." like
              >>>>> '%".$searchterm ."%'" ;
              >>> Besides missing the leading quotation mark, you might like to know that
              >>> you don't need to do concatenation like this using PHP - so you can
              >>> simply do this:
              >>>
              >>> $query = select * from books where $searchtype like '%$searchterm%' " ;
              >>>
              >>> weird huh!
              >>>[/color]
              >> You also might like to use a format like:
              >>
              >> $query = <<<EOQ
              >> SELECT *
              >> FROM books
              >> WHERE {$searchtype} LIKE '%{$searchterm} %'
              >> EOQ;
              >>
              >> Which make the whole query a lot clearer to those used to SQL CLI
              >> environments.
              >>
              >> -david-[/color][/color]

              Why not post snippets of your database connection code, your query
              execution code and your html display code? It's very difficult to help
              you with no examples...

              -david-

              Comment

              • Skeets

                #8
                Re: Connecting to mysql with php

                howard, this is where debug skills come in. try something like

                echo 'here';
                die;

                and place it within your script. if you see it, move it forward. if
                you don't move it backwards until you see it. you can find the
                location of the error.

                i *highly* recommend the adodb db abstraction layer. i use it with
                postgresql, but i would use it exactly the same with the mysql, too.
                that's the point. ;-)

                The Phone Tracker App by Snoopza is the best tracking app for cell phones. You can track mobile locations for free, as well as track calls, chats and text messages with this Android application.


                you should also test to make sure you are connecting to the db. is a
                recordset being produced? you need to know how to check for these
                kinds of things.

                adodb has db debug capability, too.

                set

                $db->debug = true;

                it tells you if and where the deb chokes.

                i never was able to get phpp error reporting when displaying php
                (winxp, php-cgi), but i use and ide that is able to debug my php. this
                *really* helps.

                i also use a forms generation and validation class by manuel lemos - it
                is great. it can be found on phpclasses.org.

                this is no easy project, but if you go head first and keep moving your
                feet, you will get somewhere. i know i did.

                Comment

                • Jerry Stuckle

                  #9
                  Re: Connecting to mysql with php

                  Howard wrote:[color=blue]
                  > The script runs but there is no data. I get blank page.
                  >
                  > I copied the info for making a config.php file.
                  > So I guess I want to try using an include statment to call those files and
                  > see if
                  > that works.
                  > The scripts were copied right from the CD-ROM so I don't know why the
                  > echo statements don't seem to be doing anything.
                  >
                  > I put a require ('config.php') in the script. Still nothing shows up and no
                  > errors in
                  > the apache log.
                  >
                  > I have a feeling that it has to do with a configuration file with php or
                  > mysql.
                  >
                  > Thanks So far
                  >
                  > Howard
                  >
                  >
                  > "David Haynes" <david.haynes2@ sympatico.ca> wrote in message
                  > news:LXnIf.2372 8$Hj6.11243@fe2 9.usenetserver. com...
                  >[color=green]
                  >>Dave Mennenoh wrote:
                  >>[color=darkred]
                  >>>>>$query = select * from books where ".$searchty pe." like
                  >>>>>'%".$searc hterm."%'" ;
                  >>>
                  >>>Besides missing the leading quotation mark, you might like to know that
                  >>>you don't need to do concatenation like this using PHP - so you can
                  >>>simply do this:
                  >>>
                  >>>$query = select * from books where $searchtype like '%$searchterm%' " ;
                  >>>
                  >>>weird huh!
                  >>>[/color]
                  >>
                  >>You also might like to use a format like:
                  >>
                  >>$query = <<<EOQ
                  >>SELECT *
                  >>FROM books
                  >>WHERE {$searchtype} LIKE '%{$searchterm} %'
                  >>EOQ;
                  >>
                  >>Which make the whole query a lot clearer to those used to SQL CLI
                  >>environment s.
                  >>
                  >>-david-
                  >>[/color]
                  >
                  >
                  >[/color]

                  David,

                  How about your PHP error log? Is there anything in it?

                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • jonathan.beckett

                    #10
                    Re: Connecting to mysql with php

                    PHP reports to the Web Server error logs.

                    Comment

                    • Jerry Stuckle

                      #11
                      Re: Connecting to mysql with php

                      jonathan.becket t wrote:[color=blue]
                      > PHP reports to the Web Server error logs.
                      >[/color]

                      Not necessarily. It can log to its own log file, also. It depends on
                      your configuration.

                      --
                      =============== ===
                      Remove the "x" from my email address
                      Jerry Stuckle
                      JDS Computer Training Corp.
                      jstucklex@attgl obal.net
                      =============== ===

                      Comment

                      • Howard

                        #12
                        Re: Connecting to mysql with php

                        Thanks for the Help to All,

                        All your code was a lot easier to follow than the books. A lot cleaner.
                        Begs the question why they wrote it that way??

                        So It's working and I'm even on to getting phpMyAdmin working to make life
                        easier.

                        Howard


                        "Gary L. Burnore" <gburnore@datab asix.com> wrote in message
                        news:dstmk4$1mt $1@blackhelicop ter.databasix.c om...[color=blue]
                        > On Tue, 14 Feb 2006 16:44:19 GMT, "Dave Mennenoh"
                        > <dave@blurredis tinction.com.in valid> wrote:
                        >[color=green][color=darkred]
                        >>>>$query = select * from books where ".$searchty pe." like
                        >>>>'%".$search term."%'" ;[/color]
                        >>
                        >>Besides missing the leading quotation mark, you might like to know that
                        >>you
                        >>don't need to do concatenation like this using PHP - so you can simply do
                        >>this:
                        >>
                        >>$query = select * from books where $searchtype like '%$searchterm%' " ;[/color]
                        >
                        > You have a mismatched double quote. :)
                        >[color=green]
                        >>weird huh![/color]
                        >
                        > I always thought so.
                        > --
                        > gburnore at DataBasix dot Com
                        > ---------------------------------------------------------------------------
                        > How you look depends on where you go.
                        > ---------------------------------------------------------------------------
                        > Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                        > | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                        > Official .sig, Accept no substitutes. | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                        > | ÝÛ 0 1 7 2 3 / Ý³Þ 3 7 4 9 3 0 Û³
                        > Black Helicopter Repair Services, Ltd.| Official Proof of Purchase
                        > =============== =============== =============== =============== ===============[/color]


                        Comment

                        Working...