locked
Getting info from a Mysql database using webrequest RRS feed

  • Question

  • Is it possible to get info from a MySQL database and then return that info to my software.

     

    Ok. I have a list of files that are displayed on a listView. When I click on one of those files. It uses webrequest to run a PHP script on my web server to return the description for that file using the file name as a key.

    Thanks,

    Joe

     

     


    http://www.pro-forums.info http://www.df-barracks.com http://www.frontline-nova.com
    Thursday, August 25, 2011 12:15 PM

Answers

  • I understand you want to do that from C# code. But if you point your browser to http://www.toppersbbs.info/getdesc.php?filename=death_raid.zip you'll see that it returns a blank page.

    So it's likely an issue in your PHP script. I noticed that the PHP script uses return (which is usually to define a return value regardless in most languages). I believe you should use perhaps something like echo $row['desx'] instead to write this value to the browser output...

    Make sure to use view source in your browser else it won't be visible as this is not valid as an HTML content (you could add something to your PHP script to explictely tell that this is text/plain document. This way it will display right away in your browser.

    The C# code won't care about that as you read it at a string and use this string wihtout testing the document type return by the browser...

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Marked as answer by Joesoft11a Friday, August 26, 2011 1:46 PM
    Friday, August 26, 2011 1:18 PM
  • Well, have you tried what I suggested ?

    IMO you'll have to replace return $row['desx'] with echo $row['desx']. Once done try again your C# code.

    I tried to tell you that using YOUR BROWSER is a convenient way to TEST your PHP script exactly as you perhaps use Notepad to open a code generated text files to see if its content is correct even if your goal is not to have someone use Notepad to view this file. Not sure If I explain myself so badly, if this is a language issue (English is not my language native either) or if you are just having a hard time to understand...

     

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    • Marked as answer by Joesoft11a Friday, August 26, 2011 2:06 PM
    Friday, August 26, 2011 1:48 PM

All replies

  • Which part is it that you need help with? Getting the data from your PHP script of getting the information from MySQL?

     

    Thursday, August 25, 2011 1:23 PM
  • Hi,

    Yes, your C# app will just send a query to the web server that will do the job and your C# app gets the HTML page as a response. The page can be not that suitable depending on what you write to it especially if it was first intended for someone to read (plus if you change the layout you could break your app that won"t be able to parse the HTML page the same way).

    Depending on how far you want to go a first step could be to create a PHP Web service that you would then call from your C# app. Try http://php.net/manual/en/refs.webservice.php (or at least a page that is dedicated to this prupose and not for human consomption).

    Knowing what you have tried and where you stuck could perhaps help. Have you tried to just grab some random web page for now ordo you have an issue PHP side...

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Thursday, August 25, 2011 2:33 PM
  • Ok, Let me try and give you guys more details if I can explain this so you can under stand

    1) In a listView a file is displayed.

    2) when I click on that file.

    3) It access the webrequest and returns the desction for that file by running a PHP script.

    4) and then displays that files description in a listbox

    I'm not trying to access or get and display a html page or any thing like that.

    So when you select a file on the listView. The index changes. When that happens I need it to access or run a PHP script that will run and return the description of that file using the file name as a key name. The name to search for to get the description for that file.

    and then it gets displayed in a textbox.

    I'll have to post on the support forum for the forums section that I'm using that has the download section, So I can get the info I need to make the PHP script to do what I want it to do. Unless some one here knows how to do that. Any way I run PHPBB 3.0.9 with Portal 5.0 XL Premod that has a download section. My software accesses thoses folders created in the download section and displays the files for download.

     

     


    Pro-Forums
    Delta Force Barracks
    On the Frontlines
    Thursday, August 25, 2011 7:00 PM
  • I am still not clear what your question is. Do you need help using the WebRequest class?

    This forum will not be able to help you with the PHP/MySql development, there are other forums for that.

     

    Friday, August 26, 2011 7:32 AM
  • What will happen is :

    - your client application will send a web request to the server
    - the PHP script will generate some output (wether or not you call this a web page) that the webrequest will receive as a response. It could be just a single line text output
    - your client app will then get this as a string that will be used how you wish

    I believe both Andreas and I understand what you are trying to do but we don't know on which particular point you have a problem. Even if you can't get the script up and running right now you could start by just grabbing the content of a simple one line txt file stored somewhere on a web server. Later you'll point to a PHP string that writes the needed info rather than to the txt file but it really won't change much.

    If the issue is client side have you tried http://msdn.microsoft.com/en-us/library/fhd1f0sw(v=vs.80).aspx ? (WebClient.DownloadString). You'll pass also the id on the url (that is you'll point to http://yourserver/script.php?id=9 and the script outputs "Product description" which is returned by DownloadString or a more complex output if you want to get multiple informations about the file).

    If the issue is on the PHP side it's likely better to use a PHP forum...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Link added
    Friday, August 26, 2011 8:59 AM
  • I am still not clear what your question is. Do you need help using the WebRequest class?

    This forum will not be able to help you with the PHP/MySql development, there are other forums for that.

     


    Ok, one more time, in a short and simple.

    All I need to do is get my C# program to run a PHP script to access a MySQL database and to return the description of a file.

    It can't get any simpler then that.

    The idea is that when you click on the file name on the listView. It gets that description for that file by doing a webrequest.


    Pro-Forums
    Delta Force Barracks
    On the Frontlines
    Friday, August 26, 2011 9:02 AM
  • You are clear what you want to do, but not what you need help with. Here is how to use WebRequest.

    string fileName = "somefile.dat";
    string url = @"http://www.yoursite.invalid/script.pgp?filename={0}";
     
    WebRequest request = HttpWebRequest.Create(String.Format(url, HttpUtility.UrlEncode(fileName)));
    WebResponse response = request.GetResponse();
    			
    string received = null;
     
    using (Stream stream = response.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
    	received = reader.ReadToEnd();
    }
     
    // Received data is received variable as string
    
    Friday, August 26, 2011 9:17 AM
  • You are clear what you want to do, but not what you need help with. Here is how to use WebRequest.

    string fileName = "somefile.dat";
    string url = @"http://www.yoursite.invalid/script.pgp?filename={0}";
     
    WebRequest request = HttpWebRequest.Create(String.Format(url, HttpUtility.UrlEncode(fileName)));
    WebResponse response = request.GetResponse();
    			
    string received = null;
     
    using (Stream stream = response.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
    	received = reader.ReadToEnd();
    }
     
    // Received data is received variable as string
    

    Oe maybe some thing like this. From what I can see from above.

     

    string fileName = "dfx2_map.zip"; // File to search for
    string url = @"http://www.yoursite.com/getdesc.php?filename=" + filename;
     
    WebRequest request = HttpWebRequest.Create(url);
    WebResponse response = request.GetResponse();
    			
    string received = null;
     
    using (Stream stream = response.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
    	received = reader.ReadToEnd();
    }
    
    

    Or in my case, it look like this

    txtFileDesc.Text = reader.readtoend();

     


    Pro-Forums
    Delta Force Barracks
    On the Frontlines

    Friday, August 26, 2011 9:34 AM
  • Or when you don't need the low level control provided by HttpWebRequest you can use the higher level WebClient class and for example its DownloadString method (http://msdn.microsoft.com/en-us/library/fhd1f0sw(v=vs.80).aspx).

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Friday, August 26, 2011 11:18 AM
  • Well I been playing with it and Well on msdn.com, I thought there was a PHP forum. I just went threw the list and didn't see a thing. I set it up as a sample and it works. It's just that nothing is getting returned. I need a forum that some one would know PHP and C#. any ideas.

     

     

     


    Pro-Forums
    Delta Force Barracks
    On the Frontlines
    Friday, August 26, 2011 11:31 AM
  • Not necessarily. You could point your browser to the PHP script and just see if it returns the good data (use "view source").

    With the same C# code you could see if you can grab whatever page or file you'll find somewhere on the web.

    I mean that your C# client app doesn't care what is the server side technology and the PHP script doesn't care who requires its output. The point here is to find out if this is the PHP script that doesn't provide the good data or the C# side that doesn't read correctly the data.

    Is the script yours ? Sometimes the script check the browser type that is used. When the query is not send from a browser it sees that the browser type is not defined and sends some error message instead. You could workaround this by adding a user agent header.

    See http://stackoverflow.com/questions/642860/faking-browser-request-in-asp-net-c that shows how to pretend to be a browser (assuming this is the issue, but the first step would be likely to test separately both sides to see which one causes the issue).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Friday, August 26, 2011 12:20 PM
  • Well I'm not using a browser, Just in c#. I need to get the data from the mysql database and return the value to the program, NOT THE BROWSER

    and Yes the PHP script in Mine.

    Here, Look for your self

     

    <?php
    
    
    $filename = $_GET['filename']; 
    
    
    $con = mysql_connect("localhost","user","password");
    
    mysql_select_db("mapnet", $con);
    
    $result = mysql_query("SELECT * FROM files WHERE filename='$filename'");
    
    $row = mysql_fetch_array($result))
     
     return $row['desx'];
    
    ?>
    

     


    and Here's C# Code.

    string fileName = "death_raid.zip"; // File to search for
       string url = @"http://www.toppersbbs.info/getdesc.php?filename=" + fileName;
    
       WebRequest request = HttpWebRequest.Create(url);
       WebResponse response = request.GetResponse();
    
       string received = null;
    
       using (Stream stream = response.GetResponseStream())
       using (StreamReader reader = new StreamReader(stream))
       {
        received = reader.ReadToEnd();
    
        if (received.Length == 0)
         txtFileDesc.Text = "No Description is avaliable.";
        else
         txtFileDesc.Text = received;
       }
    
    
    



    Pro-Forums
    Delta Force Barracks
    On the Frontlines

    • Edited by Joesoft11a Friday, August 26, 2011 12:29 PM updated it
    Friday, August 26, 2011 12:28 PM
  • I recommend you Url encode the filename, it may and may not cause any issue but it will help wonderng why it doesn't work for a certain file once non-url allowed characters occur in a filename.

    A great tool to investigate what is sent between a webclient and server is fiddler tool, its free.

    http://www.fiddler2.com

     

    Friday, August 26, 2011 1:11 PM
  • I understand you want to do that from C# code. But if you point your browser to http://www.toppersbbs.info/getdesc.php?filename=death_raid.zip you'll see that it returns a blank page.

    So it's likely an issue in your PHP script. I noticed that the PHP script uses return (which is usually to define a return value regardless in most languages). I believe you should use perhaps something like echo $row['desx'] instead to write this value to the browser output...

    Make sure to use view source in your browser else it won't be visible as this is not valid as an HTML content (you could add something to your PHP script to explictely tell that this is text/plain document. This way it will display right away in your browser.

    The C# code won't care about that as you read it at a string and use this string wihtout testing the document type return by the browser...

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Marked as answer by Joesoft11a Friday, August 26, 2011 1:46 PM
    Friday, August 26, 2011 1:18 PM
  • Hello Joe, if you want to access to a MySql server from C# my question is: how don't you use MySql connector for .NET? In that way you could query the database directly without any PHP script to help you in that stuff:

     

    http://www.mysql.com/downloads/connector/net/

     

    Hope this helps,

    Miguel.

    Friday, August 26, 2011 1:35 PM
  • Now I under stand. The word echo threw me off. Thank you. I didn't know that if the php didn't echo the results that the C# code won't detect it. and return the value. Thanks guys for all your help

    If you can would you please help on on this last problem

    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/231af3ea-ba75-476c-a08b-15f30d5ba338/

     


    Pro-Forums
    Delta Force Barracks
    On the Frontlines

    • Edited by Joesoft11a Friday, August 26, 2011 1:54 PM Updated it
    Friday, August 26, 2011 1:47 PM
  • Well, have you tried what I suggested ?

    IMO you'll have to replace return $row['desx'] with echo $row['desx']. Once done try again your C# code.

    I tried to tell you that using YOUR BROWSER is a convenient way to TEST your PHP script exactly as you perhaps use Notepad to open a code generated text files to see if its content is correct even if your goal is not to have someone use Notepad to view this file. Not sure If I explain myself so badly, if this is a language issue (English is not my language native either) or if you are just having a hard time to understand...

     

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    • Marked as answer by Joesoft11a Friday, August 26, 2011 2:06 PM
    Friday, August 26, 2011 1:48 PM
  • Hello Joe, if you want to access to a MySql server from C# my question is: how don't you use MySql connector for .NET? In that way you could query the database directly without any PHP script to help you in that stuff:

     

    http://www.mysql.com/downloads/connector/net/

     

    Hope this helps,

    Miguel.

    Thanks for the info, The problem is that I don't have access to the server. So I couldn't even use it even if I wanted too.

     


    Pro-Forums
    Delta Force Barracks
    On the Frontlines
    Friday, August 26, 2011 2:04 PM
  • Well, have you tried what I suggested ?

    IMO you'll have to replace return $row['desx'] with echo $row['desx']. Once done try again your C# code.

    I tried to tell you that using YOUR BROWSER is a convenient way to TEST your PHP script exactly as you perhaps use Notepad to open a code generated text files to see if its content is correct even if your goal is not to have someone use Notepad to view this file. Not sure If I explain myself so badly, if this is a language issue (English is not my language native either) or if you are just having a hard time to understand...

     

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    I'm sorry, It's just I didn't know that it's the reason why it wasn't working. and Yes I tried that and it works. Thank you. Please try and help with that last problem.

    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/231af3ea-ba75-476c-a08b-15f30d5ba338/

     


    Pro-Forums
    Delta Force Barracks
    On the Frontlines
    Friday, August 26, 2011 2:06 PM