Results 1 to 11 of 11

Thread: about PHP and mysql

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    148

    Arrow about PHP and mysql

    sup? all

    i don't know anything about php, but i was wondering if anyone have sample example how to read mysql Database, lets say when a user type in a link like this:

    http://www.hostname.com/Serial.php?123456

    123456 is the serial in the mysql database, once the serial match in the database it will show a sample with the serial on it.


    can anyone help me out here i'm been searching for this for awhile

    Thankx

  2. #2
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: about PHP and mysql

    http://www.hostname.com/Serial.php?123456

    hmmm.... this particular construct... 123456... doesn't follow query string contruct.. now if you have this like this

    http://www.hostname.com/Serial.php?serial=123456

    then you can get 123456 by
    $_GET["serial"] in your php code...
    and from there... create your sql statement.
    then pass it to something like
    mysql_query($strQUERY); something like that.. I havn't used mysql routines... I work mostly using pgsql.. by I should say they are somewhat the same

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    148

    Arrow Re: about PHP and mysql

    Quote Originally Posted by oceanebelle
    http://www.hostname.com/Serial.php?123456

    hmmm.... this particular construct... 123456... doesn't follow query string contruct.. now if you have this like this

    http://www.hostname.com/Serial.php?serial=123456

    then you can get 123456 by
    $_GET["serial"] in your php code...
    and from there... create your sql statement.
    then pass it to something like
    mysql_query($strQUERY); something like that.. I havn't used mysql routines... I work mostly using pgsql.. by I should say they are somewhat the same

    yeah that is what i meant , like i said i'm don't know anything about php

    is there a chance you can make a lil example like that with connect to mysql?

    Thankx so much

  4. #4
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: about PHP and mysql

    assuming this code is in serial.php

    PHP Code:
    <?php
    $link 
    mysql_connect('hostname''mysql_user''mysql_password');
    if (!
    $link) {
        die(
    'Could not connect: ' mysql_error());
    }
    echo 
    'Connected successfully';

    var 
    $nSerial $_GET["serial"]; 
    var 
    $strQUERY "SELECT * FROM mydatabase.mytable where serial =";
    var 
    $result;

    if (
    $nSerial != ""){
        
    $strQUERY $strQUERY "'"trim($nSerial) + "'";
        
        
    $result mysql_query($strQUERY$link);
        if (!
    $result) {
            die(
    'Invalid query: ' mysql_error());
        }
        
        if(
    mysql_num_rows($result) > 0){
            echo 
    'serial number is found';
        }
        
        
    // do code here to continue processing whatever you want to process. :D

        
    } else {
        echo 
    'Serial not found';
    }

    mysql_close($link);
    ?>
    I basically copied bits of code from dox... and I have not tested this because I don't have mysql with me... but I'd say this is ONE of the ways to do this.

    Hope this helps

    -oceane

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    148

    Re: about PHP and mysql

    here is what i had to view the database so what do i have to change the code u had?

    PHP Code:
    <?php
    $link 
    mysql_pconnect("localhost""root""123456");
    mysql_select_db("serial") or die("Bad DataBase");

    $query ="SELECT * FROM products";

    $result mysql_query($query) or die ("Bad Send" mysql_error( ));

    while ( 
    $row mysql_fetch_array($result) ) {
     echo 
    "<b>UserName: </b>".$row['Name']."<br>";
     echo 
    "<b>Date: </b>".$row['Date']."<br>";
     echo 
    "<b>E-mail: </b>".$row['eMail']."<br>";
     echo 
    "<b>Serial: </b>".$row['Serial']."<br>";
    }

    mysql_free_result($result);

  6. #6
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: about PHP and mysql

    Quote Originally Posted by lmf
    here is what i had to view the database so what do i have to change the code u had?

    PHP Code:
    <?php
    $link 
    mysql_pconnect("localhost""root""123456");
    mysql_select_db("serial") or die("Bad DataBase");

    $query ="SELECT * FROM products";

    $result mysql_query($query) or die ("Bad Send" mysql_error( ));

    while ( 
    $row mysql_fetch_array($result) ) {
     echo 
    "<b>UserName: </b>".$row['Name']."<br>";
     echo 
    "<b>Date: </b>".$row['Date']."<br>";
     echo 
    "<b>E-mail: </b>".$row['eMail']."<br>";
     echo 
    "<b>Serial: </b>".$row['Serial']."<br>";
    }

    mysql_free_result($result);
    use mysql_fetch_row instead of array... array returns all rows in one go.. and you wanted to loop through the records right?

    i have this in mind
    PHP Code:
    while ( $row mysql_fetch_row($result) ) {
     echo 
    "<b>UserName: </b>".$row[0]."<br>";
     echo 
    "<b>Date: </b>".$row[1]."<br>";
     echo 
    "<b>E-mail: </b>".$row[2]."<br>";
     echo 
    "<b>Serial: </b>".$row[3]."<br>";

    notice I used numbers for the index instead of name...
    just make sure you are accessing the right field in that order.

    Hope this helps

    -oceane

  7. #7
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: about PHP and mysql

    PHP Code:
    <?php
    $link 
    mysql_pconnect("localhost""root""123456");
    mysql_select_db("products") or die("Bad DataBase");

    $query ="SELECT Name, Date, eMail, sSerial FROM serial WHERE sSerial = '" $_GET["serial"]+ "'";

    $result mysql_query($query) or die ("Bad Send" mysql_error( ));

    while ( 
    $row mysql_fetch_row($result) ) {
     echo 
    "<b>UserName: </b>".$row[0]."<br>";
     echo 
    "<b>Date: </b>".$row[1]."<br>";
     echo 
    "<b>E-mail: </b>".$row[2]."<br>";
     echo 
    "<b>Serial: </b>".$row[3]."<br>";
    }

    mysql_free_result($result);
    mysql_close($link);
    so according to what you told me...
    products - database
    serial -table
    sSerial - column

    Hope this one is clear.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    148

    Arrow Re: about PHP and mysql

    Quote Originally Posted by oceanebelle
    PHP Code:
    <?php
    $link 
    mysql_pconnect("localhost""root""123456");
    mysql_select_db("products") or die("Bad DataBase");

    $query ="SELECT Name, Date, eMail, sSerial FROM serial WHERE sSerial = '" $_GET["serial"]+ "'";

    $result mysql_query($query) or die ("Bad Send" mysql_error( ));

    while ( 
    $row mysql_fetch_row($result) ) {
     echo 
    "<b>UserName: </b>".$row[0]."<br>";
     echo 
    "<b>Date: </b>".$row[1]."<br>";
     echo 
    "<b>E-mail: </b>".$row[2]."<br>";
     echo 
    "<b>Serial: </b>".$row[3]."<br>";
    }

    mysql_free_result($result);
    mysql_close($link);
    so according to what you told me...
    products - database
    serial -table
    sSerial - column

    Hope this one is clear.

    I was wondering if $result don't come up with anything, can i post a massage?

    Thx

  9. #9
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Re: about PHP and mysql

    http://au2.php.net/mysql You can find plenty of sample code here as well as function definitions.

    PHP Code:

    $result 
    mysql_query($query) or die ("Bad Send" mysql_error( ));
    if(!
    $result) {
      while ( 
    $row mysql_fetch_row($result) ) {
        echo 
    "<b>UserName: </b>".$row[0]."<br>";
        echo 
    "<b>Date: </b>".$row[1]."<br>";
        echo 
    "<b>E-mail: </b>".$row[2]."<br>";
        echo 
    "<b>Serial: </b>".$row[3]."<br>";
      }
    } else {
      
    //I'm more of a print man than an echo man really
      
    print "No Records Found Noob";

    If a query returns no results, then it returns false.
    http://au2.php.net/mysql_query

    Hopefully that code works.
    Don't Rate my posts.

  10. #10
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: about PHP and mysql

    Quote Originally Posted by Pc_Madness
    http://au2.php.net/mysql You can find plenty of sample code here as well as function definitions.

    PHP Code:

    $result 
    mysql_query($query) or die ("Bad Send" mysql_error( ));
    if(!
    $result) {
      while ( 
    $row mysql_fetch_row($result) ) {
        echo 
    "<b>UserName: </b>".$row[0]."<br>";
        echo 
    "<b>Date: </b>".$row[1]."<br>";
        echo 
    "<b>E-mail: </b>".$row[2]."<br>";
        echo 
    "<b>Serial: </b>".$row[3]."<br>";
      }
    } else {
      
    //I'm more of a print man than an echo man really
      
    print "No Records Found Noob";

    If a query returns no results, then it returns false.
    http://au2.php.net/mysql_query

    Hopefully that code works.
    If thats the case, your snippet will say "No Records Found Noob" when there are, and possibly throw an error when there's none:
    PHP Code:
    if(!$result) { 
    ..should be?:
    PHP Code:
    if($result) { 
    ?
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  11. #11
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Re: about PHP and mysql

    Err, yeah, good point. Getting confused with which part of the if was doing the error part.
    Don't Rate my posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width