Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > connect db

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-01-08, 14:25
maxbox maxbox is offline
Registered User
 
Join Date: Dec 2008
Posts: 30
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... =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
Reply With Quote
  #2 (permalink)  
Old 12-01-08, 16:46
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 5,444
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
__________________
Warning
May! contain traces of NUT. people with NUT allergies should not pay attention to any of the above
Reply With Quote
  #3 (permalink)  
Old 12-02-08, 00:13
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
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);

?>
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #4 (permalink)  
Old 12-02-08, 08:58
maxbox maxbox is offline
Registered User
 
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);
?>
Reply With Quote
  #5 (permalink)  
Old 12-02-08, 09:45
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 5,444
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
__________________
Warning
May! contain traces of NUT. people with NUT allergies should not pay attention to any of the above
Reply With Quote
  #6 (permalink)  
Old 12-02-08, 10:12
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
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.
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #7 (permalink)  
Old 12-02-08, 10:24
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 5,444
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.
__________________
Warning
May! contain traces of NUT. people with NUT allergies should not pay attention to any of the above
Reply With Quote
  #8 (permalink)  
Old 12-02-08, 17:29
maxbox maxbox is offline
Registered User
 
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);
?>
Reply With Quote
  #9 (permalink)  
Old 12-04-08, 13:19
maxbox maxbox is offline
Registered User
 
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>
Reply With Quote
  #10 (permalink)  
Old 12-04-08, 15:41
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 5,444
new question new thread please...
it helps you, it help repsondents
__________________
Warning
May! contain traces of NUT. people with NUT allergies should not pay attention to any of the above
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On