This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Goobster22's profile picture

Data Base help

Started by Goobster22, 21 December 2013 - 06:20 PM
Goobster22 #1
Posted 21 December 2013 - 07:20 PM
I am working on a store script and need to be able to access a SQL database. I heard the the grapevine you can do something with the http api but i dont know how to use it. Can you please tell me how to access a database.
Imque #2
Posted 21 December 2013 - 11:16 PM
Sure, databases can be access by 2 simple-ish ways. (not limiting this)

1. I find this to be the best but its personal choice. You can create a webpage that handles all the transfers and pulls itself. A page like this is doing all the database accessing! Your computer in game will simply be running a couple of HTTP functions calling the site. Generally they are GET variables.

Here is an example!

(not protected against SQL injection)

<?php

$query = $_GET['sql_query']; 

mysql_query($query);

MySQLi would be more efficient.

Sorry, the 2nd way actually doesn't exists anymore (from what I am aware)

Maybe another user could clean this up for me. There was a mod that could be added to the game and would allow connect to MySQL databases. I am not sure sorry
Goobster22 #3
Posted 22 December 2013 - 09:04 AM
I hosted the code you put on the post above on my xampp server (by the way you forgot the end php tag)

I tried calling the website with the code below


term.clear()
term.setCursorPos(1,1)
http.get("localhost/index.php")

this is the error i got

Bios:451: Invalid URL

Do i need a website that isn't hosted on my pc?
rewbycraft #4
Posted 22 December 2013 - 03:35 PM
I hosted the code you put on the post above on my xampp server (by the way you forgot the end php tag)

I tried calling the website with the code below


term.clear()
term.setCursorPos(1,1)
http.get("localhost/index.php")

this is the error i got

Bios:451: Invalid URL

Do i need a website that isn't hosted on my pc?



term.clear()
term.setCursorPos(1,1)
http.get("http://localhost/index.php")

You forgot the http part.
Imque #5
Posted 22 December 2013 - 11:39 PM
Ok, with PHP, you don't require the end tag..

** FUTURE USERS :: HAVE A GOOD UNDERSTANDING IN BOTH PHP AND LUA BEFORE ATTEMPTING **

You will need to use:


data = http.get("127.0.0.1/index.php?value=whatever")
data = data.readAll()

print(data) -- prints computercraft


<?php

if ($_REQUEST['value'] == 'whatever') {

  echo 'computercraft';

}

Edited on 22 December 2013 - 10:39 PM
Goobster22 #6
Posted 23 December 2013 - 10:22 AM
Thank you guys sooooooo much. I'm so happy it worked. :D/>

thanks for the amazing advice. :D/>