Jump to content

Insert & Retrieve data into/from MySql using sockets


uppalakishore

Recommended Posts

Suppose i had a Registration Form with multiple fileds(like first name,last name etc).And i want to enter the entered values into MySql using Sockets...

 

I need the example showing inserting data into Mysql using sockets.Bcoz i learned something about php sockets but i don't have the basic idea how to implement them...Even i tried so many examples but i tired of having so many complexities like socket_bind,socket_accept,socket_listen errors...

 

THANKS IN ADVANCE...

Link to comment
Share on other sites

IF you use "localhost" as the address for the MySQL server, you'll be using the Unix sockets to communicate with the server. Could this be what the client/teacher/somebody meant?

 

If not, and it is for a client, then just tell him "No, you don't want to do that. It would be a LOT more expensive, take a LOT more time, and possibly introduce security flaws." If it's for someone else, ask them why the heck they want to re-invent the wheel.

Edited by Christian F.
Link to comment
Share on other sites

Thanq @trq for your responce...

But i need Socket programming in php that will enter the Form Field Values(entered Values) into database...

Means as follows:

 

$val=$_GET['name'];

Then how can i enter '$val' value into database using socket,that is my problem...

 

anyway THANKS...

Link to comment
Share on other sites

Hmm.. I'm guessing that you either have misunderstood your teacher, or he has misunderstood something.

 

There are plenty of other uses which sockets could be used for, which wouldn't only make more sense but also be a whole lot easier to use as learning projects. A simple web server, for instance, is perfect for learning about using sockets in PHP.

For this, using the built in MySQLI or PDO libraries is the far better option, as that is what you'll be using outside of school.

 

As trq stated doing this requires that you read up on the MySQL Client-Server protocol as well, and learn it inside out. That's in addition to the SQL query language syntax, which you use to tell the MySQL server what to do. You also need to read up on how to connect to a socket in PHP, and then figure out how to create the proper structure as defined by the protocol.

Personally, I don't see any benefit of doing this, at least not until you've reached a sufficiently advanced platform of knowledge. Not just with PHP, but also programming, protocols, databases and networking in general. This is the kind of project I might want to do as a learning experience, to put things in perspective.

It's like having your driving instructor insisting that you build your own car, including the engine, so that you can take your driving lessons with it; While it is nice to know how to build a car, it has precious little with actually knowing how to drive one.

 

Ask him how much time he expects you to use on this project, if what I've outlined here is actually what he expects you to do. If he says anything less than 6 months[1], then he clearly doesn't fully appreciate the complexity of the task and the amount of work it would require.

 

[1]Assuming you do other stuff at school, and not just this one project.

Edited by Christian F.
Link to comment
Share on other sites

  • 3 weeks later...

Thank You Christian F...

 

I had simple client and server socket programs, the client sends a message to server, then the server reverses the string(sent by client) and send it to server.

Client.php

[bR]

<?php
$host = "127.0.0.1";
$port = 80;

[color=#ff0000][b]$message ="Hello world";[/b][/color]
echo "<br>\nMessage To server :".$message."\n";
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or
die("Could not create socket\n");
if($socket)echo "<br>Client Socket Created\n";
//
//// connect to server
$result = socket_connect($socket, $host, $port) or
die("Could not connect to server\n");
if($result)echo "<br>Socket Connected to Server : $host on Port : $port\n";
//
//while(1){
// send string to server
echo "<br>Sending Data to the Server...\n";
sleep(1);
socket_write($socket, $message, strlen($message)) or
die("Could not send data to server\n");
//
// get server response
echo "<br>Getting Responce From the Server...\n";
sleep(1);
$result = socket_read ($socket, 1024) or die("Could
//not read server response\n");
echo "Reply From Server :".$result;
//}
// close socket
socket_close($socket);

?>

 

Server.php

[bR]

<?php
// set some variables
$host = "127.0.0.1";
$port = 80;
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or
die("Could not create socket\n");
if($socket)echo "<br>Socket Created\n";
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
if($result)echo "<br>Socket Binded\n";
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not
set up socket listener\n");
if($result)echo "<br>Socket Listening Connections....\n";
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not
accept incoming connection\n");
if($spawn)echo "<br>Socket Accepted Incoming Connections...\n";
//while(1){
// read client input
echo "<br>\nReading Input...\n";
sleep(1);
$input = socket_read($spawn, 2048) or die("Could not
read input\n");

// clean up input string
$input = trim($input);
echo "<br>Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
echo "<br>\nResponce...\n";
sleep(1);
socket_write($spawn, $output, strlen ($output)) or
die("Could not write output\n");
//}
// close sockets
socket_close($spawn);
socket_close($socket);
?>

 

The program is executed, the problem is i want to enter that message into database using server program...I can't get any idea how to do that...

 

Thanks in Advance...

Link to comment
Share on other sites

A-hah! That makes a whole lot more sense.

You're not actually wanting to communicate to the MySQL server via sockets, but you have a socket-server that needs to communicate with a database.

 

That means you will want to use either MySQLI or PDO, in the "server.php" application. Just like any other PHP script.

There's plenty of tutorials out there, many of which are probably linked to in this forum. The manual also contains some nice examples, and is a pretty good place to start. So I recommend following the links in this post.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.