Jump to content

returning sql values?


dannerz
Go to solution Solved by Barand,

Recommended Posts

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 by dannerz
Link to comment
Share on other sites

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 by dannerz
Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by dannerz
Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.