dannerz Posted July 2, 2013 Share Posted July 2, 2013 (edited) if ($buy == "1") { echo "you bought 1 archer"; $data = mysql_query(SELECT Vals FORM vals2 WHERE ValsN = 'archers'); echo $data; } if ($buy == "2") { echo "you bought 5 archers"; } This is the code I am using.^ To manage the data bases I use http://localhost/phpmyadmin/ There is a table already that I made in phpadmin. http://s43.photobucket.com/user/fruitpooper/media/screenshot_zpsd9a1eeef.jpg.html I took a screen shot of it. The screen shot is above, as I can't paste it. Parse error: syntax error, unexpected T_STRING in D:\xampp\htdocs\MP_game\shop1b.php on line 21 This is the error message I get. All I want to do is read and change numbers. I had to edit this post too. Edited July 2, 2013 by dannerz Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted July 2, 2013 Solution Share Posted July 2, 2013 $data = mysql_query("SELECT Vals FORM vals2 WHERE ValsN = 'archers');echo $data; You should read the manual re mysql_fetch_row or mysql_fetch_assoc Quote Link to comment Share on other sites More sharing options...
dannerz Posted July 2, 2013 Author Share Posted July 2, 2013 (edited) Which manual?I got the "SELECT Vals FORM vals2 WHERE ValsN = 'archers' " from SQLZOO. Even when I read the manual, the example seems to involve an interface that I do not have. Is there some other thing other than phpadmin that is used to manage a database in sql ? I was hoping someone could correct the code. I just changed it so that there is a " right before and after the query. Now there is no error message but I was hoping to get it to return the integer 0. Edited July 2, 2013 by dannerz Quote Link to comment Share on other sites More sharing options...
dannerz Posted July 2, 2013 Author Share Posted July 2, 2013 http://php.net/manual/en/function.mysql-fetch-assoc.php I found this after your suggestion. In the first example, part of it says: <?php $conn = mysql_connect("localhost", "mysql_user", "mysql_password"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("mydbname")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql = "SELECT id as userid, fullname, userstatus FROM sometable WHERE userstatus = 1"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } // While a row of data exists, put that row in $row as an associative array // Note: If you're expecting just one row, no need to use a loop // Note: If you put extract($row); inside the following loop, you'll // then create $userid, $fullname, and $userstatus while ($row = mysql_fetch_assoc($result)) { echo $row["userid"]; echo $row["fullname"]; echo $row["userstatus"]; } mysql_free_result($result); ?> where it says: $sql = "SELECT id as userid, fullname, userstatus FROM sometable WHERE userstatus = 1"; I don't know what this means. What is id as userid, fullname, userstatus, sometable. ? This is why I the manuals are not enough for me on their own. They make referrences to things that a person is already supposedto know, but I don't know it. Quote Link to comment Share on other sites More sharing options...
trq Posted July 2, 2013 Share Posted July 2, 2013 This is why I the manuals are not enough for me on their own. How do you think the rest of us learnt this stuff? PHP's manuals are some of the best around. What is id as userid, fullname, userstatus, sometable. It is an example query, replace it with your own and forget about it. The part you really need to look at is the usage of mysql_fetch_assoc. Note that you need to pass the resource returned from mysql_query() to mysql_fetch_assoc() in order to be able to actually use the results. Quote Link to comment Share on other sites More sharing options...
dannerz Posted July 2, 2013 Author Share Posted July 2, 2013 The manuals are amazing, but I lack the understanding to fully use them at this time. What i meant about "id as userid, fullname, userstatus, sometable" was that I don't know how each of those are supposedto be filled out, and what they are normally supposedto be like. The example query, i am not sure what to replace the examples with, you see. Once I understand it better I wont be asking about it and using up precious time, but right now I'm an absolute beginner, so that is why i made this thread. Quote Link to comment Share on other sites More sharing options...
trq Posted July 2, 2013 Share Posted July 2, 2013 You already have a query: $data = mysql_query(SELECT Vals FORM vals2 WHERE ValsN = 'archers'); Your actual error is cause by the fact that your syntax is incorrect. Queries are strings. $data = mysql_query("SELECT Vals FORM vals2 WHERE ValsN = 'archers'"); If your question is about that fact that you don't understand how to create a query, your in the wrong board. That is a MySQL question. Quote Link to comment Share on other sites More sharing options...
dannerz Posted July 3, 2013 Author Share Posted July 3, 2013 (edited) I noticed that and fixed it but i can't edit my first post it seems. What is the query returning anyways? When I try to echo it i get nothing. Edited July 3, 2013 by dannerz Quote Link to comment Share on other sites More sharing options...
trq Posted July 3, 2013 Share Posted July 3, 2013 mysql_query returns a result resource on success or false on failure. You cannot echo a resource. Quote Link to comment Share on other sites More sharing options...
dannerz Posted July 3, 2013 Author Share Posted July 3, 2013 mysql_query returns a result resource on success or false on failure. You cannot echo a resource. it is returning a false. the error said it was a boolean value. this is as far as i got: $data = mysql_query("SELECT `test`.`vals2` FROM `vals2` WHERE `ValsN` = 'archers'"); if ($data == false) exit("This has failed"); $msg = mysql_fetch_assoc($data); echo $msg; echo $data; I'm sure I got the query wrong. In my screen shot i showed which part of the database I wanted to access. Quote Link to comment Share on other sites More sharing options...
dannerz Posted July 6, 2013 Author Share Posted July 6, 2013 (edited) ok, I just made a little bit of progress. $con = mysql_connect("localhost", 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } if (!mysql_select_db("test")) { echo "Failure" . mysql_error(); exit; } $buy = $_POST["unit"]; if ($buy == "1") { echo "you bought 1 archer"; // FROM not FORM. $data = mysql_query("SELECT Vals FROM vals2 WHERE ValsN = 'archers'"); if ($data == false) exit("This has failed"); $msg = mysql_fetch_assoc($data); echo "<br />"; echo $msg; echo "<br />"; echo $data; } ^That is my code. It echos something that says "Array Resource id #4". I'm wondering what do I do next to turn the resource into an integer or a string? Edited July 6, 2013 by dannerz Quote Link to comment Share on other sites More sharing options...
dannerz Posted July 6, 2013 Author Share Posted July 6, 2013 I just cut and pasted something out of the manual. It seems that it works now. Quote Link to comment Share on other sites More sharing options...
NavedKhan Posted July 20, 2013 Share Posted July 20, 2013 i want to login by hard code username and password only and retreive mydata named db and table name is users.....so now when admin login by hardcode username and password next page shows the complete data of the users table Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.