DaVuLf Posted May 16, 2006 Share Posted May 16, 2006 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 Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted May 16, 2006 Share Posted May 16, 2006 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] Quote Link to comment Share on other sites More sharing options...
DaVuLf Posted May 17, 2006 Author Share Posted May 17, 2006 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? Quote Link to comment Share on other sites More sharing options...
Ferenc Posted May 17, 2006 Share Posted May 17, 2006 [!--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 Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted May 17, 2006 Share Posted May 17, 2006 As long as you have disk space available :) Quote Link to comment Share on other sites More sharing options...
DaVuLf Posted May 17, 2006 Author Share Posted May 17, 2006 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 localinfile '/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. Quote Link to comment Share on other sites More sharing options...
Ferenc Posted May 17, 2006 Share Posted May 17, 2006 Past the first two lines of googstock.txt here Quote Link to comment Share on other sites More sharing options...
DaVuLf Posted May 17, 2006 Author Share Posted May 17, 2006 [!--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,4002,GOOG,400.16[/code]That is that. Thanks man. Quote Link to comment Share on other sites More sharing options...
Ferenc Posted May 17, 2006 Share Posted May 17, 2006 make a reader.php file and place this code in itupload to directory googstock.txt resides in , point your browser to reader.phpit will only work if the CSV values are as you posted (3 values)1,GOOG,4002,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 readforeach ($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 createINSERT 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 Quote Link to comment Share on other sites More sharing options...
DaVuLf Posted May 17, 2006 Author Share Posted May 17, 2006 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 :). 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.