Results 1 to 10 of 10

Thread: connect db

  1. #1
    Join Date
    Dec 2008
    Posts
    30

    Unanswered: i need help / validate cod real time

    hello
    I need help to create an db conection to this php script :
    the tables 'call' is all ready created in the myphpadmin
    i cant doing this self i am a photograf i need sell pictures
    in my website.
    http://www.mydoman.se/script.php?PAY...E=12345&TAX=10




    <?php
    /*
    * MySQL Databas:
    *
    * CREATE TABLE call (
    * id int(11) auto_increment NOT NULL,
    * paynr varchar(11) NOT NULL,
    * custnr varchar(15) NOT NULL,
    * code varchar(6) NOT NULL,
    * tax varchar(3) NOT NULL,
    * date varchar(10) NOT NULL,
    * primary key (id)
    * ) TYPE=MyISAM;
    *
    * Funtion:
    * save information to db
    *
    *
    */

    error_reporting(0); /* off error raport */

    /* databas-connect */




    // variabler
    $paynr = mysql_real_escape_string($_GET['PAYNR']);
    $custnr = mysql_real_escape_string($_GET['CUSTNR']);
    $code = mysql_real_escape_string($_GET['CODE']);
    $tax = mysql_real_escape_string($_GET['TAX']);
    $date = time();

    // save in the db
    mysql_query("INSERT INTO table_call (paynr,custnr,code,tax,date)
    VALUES ('$paynr','$custnr','$code','$tax','$date')");

    /* databas-closed */
    ?>
    Last edited by maxbox; 12-04-08 at 14:32. Reason: part2

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    have a look at the PHP.NET website
    that gives details fo the MySQL connection


    normally you need to connect to the database
    $cnn=@mysql_connect(HOST, UID1, PWD1);
    in this example the Hostname, userid an password are DEFINED in an include file
    you would need to decide if you wanted to use variables eg
    Code:
    $cnn=@mysql_connect($host, $username, $password);
    or hard code the 3 values.
    your ISP should provide you with those 3 settings

    then you need to define the database to be used, and thats going to be something similar to
    Code:
    $dbr=@mysql_select_db(DBN, $cnn)
    again this script uses DBN as the database name which is DEFINEd elsewhere
    you would need to provide the name of your database

    so say your ISP sets up your db account this way
    the hostname is "photoserver"
    the username is "maxbox"
    the password is "xobxam"
    the database is "maxphotos"

    Code:
    $cnn=@mysql_connect("photoserver", "maxbox", "xobxam");
    $dbr=@mysql_select_db("maxphotos", $cnn);
    note the $cnn variable which is a PHP variable containing a valid MySQL connection
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Mar 2007
    Location
    010101010110100
    Posts
    803
    Yeah, Healdem is right. You need to connect to the database and then select the database you want to work with. Here is a complete connection script that I use.

    PHP Code:
    <?php
    $username 
    "YOUR USERNAME HERE";
    $password "YOUR PASSWORD HERE";
    $hostname "YOUR HOSTNAME HERE";
    $database "YOUR DATABASE HERE";
        
    $conn mysql_connect($hostname$username$password
        or die(
    "Connection failed");

    mysql_select_db($database$conn)
        or die(
    "Select database failed");

    mysql_close($conn);

    ?>

  4. #4
    Join Date
    Dec 2008
    Posts
    30
    hi

    i have try whit this but i my service provider received 500 intenal server error i dont now if my script is correct


    ex. xhttp://www.mydomain.se/script.php?PAYNR=09392002277&CUSTNR=081234567&CODE =12345&TAX=10[/url]

    to my script

    <?php
    /*
    * MySQL Databas:
    *
    * CREATE TABLE call (
    * id int(11) auto_increment NOT NULL,
    * paynr varchar(11) NOT NULL,
    * custnr varchar(15) NOT NULL,
    * code varchar(6) NOT NULL,
    * tax varchar(3) NOT NULL,
    * date varchar(10) NOT NULL,
    * primary key (id)
    * ) TYPE=MyISAM;
    *
    */

    /* db-connect*/
    $dbhost = 'XXXXX.XXXXXXX.com : 3306';
    $dbuser = 'anuncios';
    $dbpass = 'ganaras4';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
    $dbname = 'suecia';
    mysql_select_db($dbname);


    // Escape:a alla variablar
    $paynr = mysql_real_escape_string($_GET['PAYNR']);
    $custnr = mysql_real_escape_string($_GET['CUSTNR']);
    $code = mysql_real_escape_string($_GET['CODE']);
    $tax = mysql_real_escape_string($_GET['TAX']);
    $date = time();

    // save to db
    mysql_query("INSERT INTO jos_call (paynr,custnr,code,tax,date)
    VALUES ('$paynr','$custnr','$code','$tax','$date')");

    /* databas-stängning bortklippt */
    mysql_close($conn);
    ?>

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    the problem is the host
    it should be the name of the server hosting the MySQL instance on "their" network

    it should not be a URL + port number, it may work ok with a URL or even ipaddress. FWIW mySQL virtually always talks to the outside world on port 3306.

    on the sites Ive set up the name of the db server is usually a private internal server which is not visible to the outside world (and its given another name eg dbserver15, mysqldb04, oojamaflip.

    your ISP should be able to provide with the details.... Ask them to provide the following
    the name of the MySQL server
    the name of the userid they have set up for you to access the db
    the password for that userid
    note these will almost certainly not be the same userid and password they provide for you to maintain the applications and db
    the name of the database

    you can get more information on the details of the error using something like..

    Code:
    $cnn=@mysql_connect($hostname,$username,$password) or
     die("Failed to open connection, MySQL whinged in this manner..".mysql_errno().": ".mysql_error()));
    $dbr=@mysql_select_db($databasename, $cnn) or
     die("Failed to open database, MySQL whinged in this manner..".mysql_errno().": ".mysql_error()));
    before you conatct the ISP try the URL name and see if that works.. Ive never done ti, but if they supplied that as the db server name then at least try it before going back to them
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Mar 2007
    Location
    010101010110100
    Posts
    803
    I'd be willing to bet that the mysql_error() func will not produce any output and that the problem is entirely with his host as you suggest.

    One other thing he might try is to change the hostname to "localhost". I had a managed server a while back and localhost was the only way I could use it.

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    you may well be correct..
    however I would expect there to be a message or at the very least an error code, which would indicate what the potential problems are
    when I first started using PHP the or die construct proved extremely useful in diagnosing what was wrong.

    I'm expecting the message to say cant find hostname, dont have permission for requested action....
    without the error message there is no way of truly zooming in on what the problem is.
    I'd rather be riding on the Tiger 800 or the Norton

  8. #8
    Join Date
    Dec 2008
    Posts
    30

    Talking resolved

    hello I have contacted my host and the code of php has corrected:

    this works fine now thank you very much for your time.

    <?php

    $mysql_host="xxxx.xxxx.com";
    $mysql_user="anuncios";
    $mysql_password="ganaras4";
    $mysql_db = "suecia";

    $conn = mysql_connect("$mysql_host", "$mysql_user" , "$mysql_password")
    or die("Could not connect : " . mysql_error());


    mysql_select_db("$mysql_db",$conn)
    or die("Select database failed");


    // Escape:a alla variablar //
    $paynr = mysql_real_escape_string($_GET['PAYNR']);
    $custnr = mysql_real_escape_string($_GET['CUSTNR']);
    $code = mysql_real_escape_string($_GET['CODE']);
    $tax = mysql_real_escape_string($_GET['TAX']);
    $date = time();

    // save to db //
    mysql_query("INSERT INTO jos_call (paynr,custnr,code,tax,date)
    VALUES ('$paynr','$custnr','$code','$tax','$date')");


    mysql_close($conn);
    ?>

  9. #9
    Join Date
    Dec 2008
    Posts
    30

    part2

    hello

    I found this code to validate xxxx in real time from db when the users type something in the text field so i need apply this to validate code in the table jos call of my db suecia but I am not sure I must do it in the red text
    DB suecia tables jos_call
    my script

    <?php
    /*
    $mysql_host="XXXXX.XXXXX.com";
    $mysql_user="anuncios";
    $mysql_password="ganaras4";
    $mysql_db = "suecia";

    $conn = mysql_connect("$mysql_host","$mysql_user","$mysql_ password")
    or die("Could not connect : " . mysql_error());

    mysql_select_db("$mysql_db",$conn)
    or die("Select database failed");

    $?????= mysql_query("Select * from ????? where ????? = '".@$_REQUEST['code']."'",@$conexion);
    if (mysql_num_rows($conexion)==0){



    echo @$_REQUEST['code'].' - code free ';
    }else{
    echo @$_REQUEST['code'].' - code not free';
    }
    mysql_close($conexion);
    */
    ?>













    HERE IS THE COMPLET SCRIPT:

    Comprueba.php

    <?


    /*
    $servidor = "tu servidor";
    $usuario = "nombre_usuario";
    $password = "miclave";
    $conexion = mysql_connect($servidor, $usuario, $password) or die("no se pudo conectar a base de datos".mysql_error());
    $usuarios = mysql_query("Select * from usuarios where nombre_usuario = '".@$_REQUEST['nombre']."'",@$conexion);
    if (mysql_num_rows($conexion)==0){
    echo @$_REQUEST['nombre'].' - Nombre de usuario Libre';
    }else{
    echo @$_REQUEST['nombre'].' - Nombre de usuario ocupado';
    }
    mysql_close($conexion);
    */
    ?>


    Index.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Validate users</title>
    <script>
    function chk_usuario(){
    <!-- INICIO DE CODIGO OFRECIDO POR TUTORES.ORG -->

    var pos_url = 'comprueba.php';
    var nombre = document.getElementById('usuario').value;
    var req = new XMLHttpRequest();
    if (req) {
    req.onreadystatechange = function() {
    if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
    document.getElementById('resultado').innerHTML = req.responseText;
    }
    }
    req.open('GET', pos_url +'?nombre='+nombre,true);
    req.send(null);
    }
    }
    <!-- FIN DE CODIGO OFRECIDO POR TUTORES.ORG -->
    </script>
    </head>
    <body>
    <form id="form1" name="form1" method="post" action="">
    validate users: tutores<br />
    <input name="usuario" type="text" id="usuario" onKeyUp="chk_usuario();"/><br /><div id='resultado'></div>
    <input type="submit" name="Submit" value="Enviar" />
    </form>
    </body>
    </html>

  10. #10
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    new question new thread please...
    it helps you, it help repsondents
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

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