Jump to content

Quick Question (Query)


DaVuLf

Recommended Posts

I'm pretty sure this is a relatively simple question, yet I cannot grasp why I have not been able to get this to work.

Here is the code:

[code]
$getvalue = "SELECT value FROM $stock WHERE id = '$time'";
$value = mysql_query($getvalue);
$balance=$quantity*$value;
echo $balance;
echo $value;
[/code]

The output of this code is the following:
[code]
20Resource id #2
[/code]

This would lead me to believe that the value $balance is returned as '20' and the value $value is returned as 'Resource id #2'. Now, I'm not sure why this is happening as I'm sure the table that is being accessed is fine.

The query works as follows:
$stock = GOOG
I have a table named 'GOOG' which has an 'id' field that is auto incremented by the number of rows. I have two rows, and a value within each. So, the query should go to the field 'value' and pick up the number within that field that corresponds to the value $time. I'm unsure of what 'Resource id #2' means, so I cannot even begin to determine the problem.

Thanks for the help,
DaVuLF
Link to comment
Share on other sites

The function mysql_query only executes the query and returns a "resource id" of the record set. You need to get the records out using mysql_fetch_*** or mysql_result.
[code]$getvalue = "SELECT value FROM $stock WHERE id = '$time'";
$result = mysql_query($getvalue);
$value = mysql_result($result,0,0);
$balance=$quantity*$value;
echo $balance;
echo $value;[/code]
Link to comment
Share on other sites

Thanks for the reply Ryan. I did something slightly different, but I came up with the same ends. This is what I did:

[code]
$value_result = "SELECT * FROM $stock WHERE id = '$time'";
$value_get = mysql_query($value_result) or die(mysql_error());;
$value_find = mysql_fetch_assoc($value_get) or die(mysql_error());;
$value = $value_find[value];
$balance=$quantity*$value;
[/code]

I had another quick question: How many tables can a database have?
Link to comment
Share on other sites

[!--quoteo(post=374488:date=May 16 2006, 06:02 PM:name=DaVuLf)--][div class=\'quotetop\']QUOTE(DaVuLf @ May 16 2006, 06:02 PM) [snapback]374488[/snapback][/div][div class=\'quotemain\'][!--quotec--]

I had another quick question: How many tables can a database have?
[/quote]
As many as you need
Link to comment
Share on other sites

Nice. I have another question now. I'm using phpMyAdmin, and I'm trying to upload a text file to a table (instead of entering hundreds of rows manually). This is the query:

[code]
load data local
infile '/home/content/b/r/i/briancomeau/html/wdt/googstock.txt'
into table GOOG fields terminated by ',';
[/code]

The path was found using phpinfo(), and the table is called 'GOOG' (uppercase). I can't see why this wouldn't work. In the textfile (comma delimited) there are 3 different entries per row, a number, the word 'GOOG' and a value. In the table, there is an ID field (auto_increment) a name field, and a value field (double).

I'm not sure why this method won't upload. Any help would be appreciated.

Thanks again for everything guys.
Link to comment
Share on other sites

[!--quoteo(post=374544:date=May 16 2006, 11:22 PM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ May 16 2006, 11:22 PM) [snapback]374544[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Past the first two lines of googstock.txt here
[/quote]


[code]
1,GOOG,400
2,GOOG,400.16
[/code]

That is that. Thanks man.
Link to comment
Share on other sites

make a reader.php file and place this code in it
upload to directory googstock.txt resides in , point your browser to reader.php

it will only work if the CSV values are as you posted (3 values)
1,GOOG,400
2,GOOG,400.16

[code]<?php
// fill out db info and remove the #'s to make connection
// preview if you are happy, uncomment the query
// enjoy!
#$db = mysql_connect("localhost", "user_name","password") or die("cannot connect to the database");
#$dbi = mysql_select_db("data_base_name", $db);
#if (!$dbi) {
#     echo "DUMMY YOU BROKE IT??";
#        exit;
#}


set_time_limit(0); //prevent time outs if the file is large


$lines = file('googstock.txt'); //path to text to read
foreach ($lines as $line_num => $line) {
    $parts = explode(',', $line);
    $parts[2] = trim(str_replace('\n', '', $parts[2])); // remove \n from the string and clean white spaces
    $sql = "INSERT INTO GOOG VALUES('".$parts[0]."', '".$parts[1]."', '".$parts[2]."')";
    echo $sql."<BR>"; // to preview the insert code
    // to add to db uncomment the line below
    // mysql_query($sql) or die(mysql_error());
}

?>[/code]

this will create
INSERT INTO GOOG VALUES('1', 'GOOG', '400')
INSERT INTO GOOG VALUES('2', 'GOOG', '400.16')....

until the end of the file, you will have to un comment the query to have it execute
Link to comment
Share on other sites

Thanks alot man, I'll have to use that for some other stuff.

What I ended up doing was installing a newer version of phpMyAdmin which supports an import function. I then uploaded the CSV file directly from my computer and it made the table as I wanted.

Fortunately, I think your way is easier (especially if I make a HTML form frontend for it) for uploading other files.

I appreciate the help :).
Link to comment
Share on other sites

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.