Click here to Skip to main content
16,017,650 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to insert, update and read data using PHP and C#, is there a library to do this, if not does anyone have any information to help me along, with some experimenting I got this to work:

Example 1: I managed to get a sum working using PHP and C#

PHP Code:
PHP
<?php echo (int)$_GET["a"] + (int)$_GET["b"]; ?>

C# Code:
C#
using (var client = new WebClient())
          {
              var a = 50;
              var b = 100;
              var result = client.DownloadString(string.Format("http://antivurusdownload.co.uk/test/csharptest.php?a={0}&b={1}", a, b));
              Console.WriteLine(result);
              Console.ReadKey();
          }


Now I tried to apply this to MySQL using this code to no success

PHP Code:
PHP
<?php

$username="testing123";
$password="helloworld";
$joindate="21/10/2013 - 21:00";
$active=1;

$con=mysqli_connect("localhost","root","password","database");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"INSERT INTO user (username, password, joindate, active) VALUES ($username, $password, $joindate, $active)");

mysqli_close($con);
?>


Can anyone help!? Thanks!
Posted
Updated 16-Nov-16 0:14am
Comments
Kornfeld Eliyahu Peter 21-Oct-13 15:57pm    
The PHP you provided seems to be right - only thing I can see that maybe some of your values need quotes around them.
Do you got an error? What and when?
You want the same in c#?
Kieran Crown 21-Oct-13 15:59pm    
I didn't get an error, it's just really confusing me, is there no known library for this function?
Kornfeld Eliyahu Peter 21-Oct-13 16:39pm    
Now I'm confused. What function? Connect and CRUD a database?
Kieran Crown 21-Oct-13 16:45pm    
Sorry, something that will allow me to insert, update,delete and read data from a table
Kornfeld Eliyahu Peter 21-Oct-13 17:20pm    
Why mysqli_query isn't good for you?
(by the way - CRUD means create(insert), read, update and delete...)

C# CRUD sample.. The most simplest example

http://www.srikanthtechnologies.com/blog/dotnet/adonetcrud1.aspx[^]
 
Share this answer
 
First you have to install this: http://dev.mysql.com/downloads/connector/net/[^]

And here a sample doing the same as your PHP does...

C#
using MySql.Data.MySqlClient;

// open connection
MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();

conn.ConnectionString = "server=localhost;uid=root;pwd=password;database=database;";

try
{
  conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
  // show error
}

string sql = string.Format("INSERT INTO user (username, password, joindate, active) VALUES ({0}, {1}, {2}, {3})", username, password, joindate, active);

MySqlCommand cmd = new MySqlCommand(sql, conn);

try
{
  cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
  // show error
}

conn.Close();
 
Share this answer
 
Comments
Kieran Crown 22-Oct-13 14:28pm    
I can't use this as I need the SQL code to run on the server
Kornfeld Eliyahu Peter 23-Oct-13 1:14am    
Reading you question, our previous conversation and you comment here - I'm totally confused.
Can you rephrase your question?
Try Json.NET --- It is a good library for C# interacting with PHP and gives JSON Output in the Console or Form. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900