Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pauld1988 · Oct 17, 2015 at 12:11 PM · mysqlphp

How do i search a mysql database in unity 5

Hi All,

how do i search for a Row using mysql using a public string im new to mysql and php

---------------- php script-----------------------------

         $servername = "localhost";
         $username = "root";
         $password = "";
         $dbName   = "online_pw_build";
         
          $CurrentUser = "currentuser";
         
         
         $conn = new mysqli($servername, $username, $password, $dbName);
         
         
         if(!$conn)  {
             
             die("connection failed". mysqli_connect_error());
         }
         
         $sql = "SELECT Onwer FROM db_toons";
 
 $check = mysql_query("SELECT * FROM `db_toons` WHERE `Onwer` LIKE 'currentuser'");
 
 {
     die ("Username does not exist \n");
 }
 
 {
     $CurrentUser = (currentuser);
     while($row = mysql_fetch_assoc($check))
     {
         if (currentuser == $row['Onwer'])
              echo "ID:".$row['ID'] . "|Onwer:".$row['Onwer']. "|Toonone:".$row['Toonone']. ";";
         
     }
 }
 
 ?>

-------------C# script----------------

 using UnityEngine;
 using System.Collections;
 
 public class CurToon : MonoBehaviour {
     
     public string  currentuser;
 
     IEnumerator Start(){
         WWW CurToon = new WWW("http://localhost/online_pw_build/CurToon.php");
         yield return CurToon;
         string CurToonrString = CurToon.text;
         print (CurToonrString);
 }

what i want to happen a user login to a database ID Onwer toonone toontwo this part is done

after they login i want to search for the Onwer they logged in with then return that row back to unity

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thornekey · Oct 17, 2015 at 12:40 PM 0
Share

the way i did it was by echoing out all the information within the PHP file and putting a certain key between each one. For example CurToon.text would be equal to "usernameExample:passwordExample:idExample", then I split the string up and added each split string into an array. so I know infoArray[0] is the username, [1] is the password, etc.. let me know if you need a coded example.

avatar image pauld1988 thornekey · Oct 17, 2015 at 12:56 PM 0
Share

Hi a code example would be great :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by thornekey · Oct 18, 2015 at 10:49 AM

ok so instead of doing echo use the die funciton. this will stop the page at this message. also use the trim function as sometimes you get a whitespace at the beginning. if not just remove it. Putting the '--' acts a form of key for us to know when to split the string. You can use anything!

  die(ltrim($row["ID"] . "--" . $row["Owner"] . "--" . $row["Toonone"]));
 
          

Next, in your script in unity change your IEnumerator to this:

 void Start() {
             WWWForm form = new WWWForm();
                     // add as many of these as you need. you will use the $_POST variable funciton in your php file.
                     // so in your PHP file wyou will have $owner = $_POST['the field name']; and it will equal whatever the "variable name" 's value is.
             form.AddField("the field name", variable name);
             string link = "link to your PHP file";
             Debug.Log(link);
             WWW curToon = new WWW(link, form);
             StartCoroutine(GetInfo(curToon ));
         }
     }


 IEnumerator GetInfo(WWW curToon) {

         string infoString;
 
         yield return curToon;
         infoString = curToonw.text.ToString().TrimStart();
 
         if (infoString == "0") {
             Debug.Log ("no player info..");
         } else {
         
             Debug.Log(infoString);
             string[] infoArray = infoString.Split(new[] { "--" }, System.StringSplitOptions.None);
 
     
         string playerID = infoArray [0];
         string playerName = infoArray [1];
         string playerToonone = infoArray [2];

 
         Debug.Log ("id: " + playerID);
         Debug.Log ("owner: " + playerName);
         Debug.Log ("Toonone: " + playerToonone );

 
         }
     }

There is probably an easier way to do it. I did it this way though and it works just fine.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thornekey · Oct 18, 2015 at 01:10 PM 0
Share

let me know if that worked for you :)

avatar image pauld1988 thornekey · Oct 18, 2015 at 02:33 PM 0
Share

:) yes it does work

but its returning the first line of the table and not the currentuser

avatar image thornekey pauld1988 · Oct 19, 2015 at 01:57 AM 0
Share

What do you mean the first line? Show me your output.

Show more comments
avatar image thornekey · Oct 20, 2015 at 02:19 AM 0
Share

@pauld1988 try if ($CurrentUser == $row['Onwer']) { on line 29 on your PHP file.

avatar image pauld1988 thornekey · Oct 20, 2015 at 09:39 PM 0
Share

Undefined variable: Currentuser in C:\xampp\htdocs\online_pw_build\CurToon.php on line 29

avatar image thornekey thornekey · Oct 21, 2015 at 01:02 AM 0
Share

@pauld1988 sorry, your original code said $CurrentUser not $Currentuser. Variables are case sensitive.

avatar image pauld1988 thornekey · Oct 21, 2015 at 12:11 PM 0
Share

thank you :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity PHP login always returning true 3 Answers

My code to php -> sql in PC good, in Android not work. plese fast 1 Answer

Is it possible to send a table name through a wwwform and use it in a php query? 0 Answers

Is it possible to send and recieve data from/to a MySql server? 4 Answers

unity3d connect to php or coldfusion for database information 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges