Dazuti Posted December 14, 2009 Share Posted December 14, 2009 I have the following code that prints the data to the screen, but I need to manipulate the variable that is the result, but I'm not sure how the information is being stored. If I just echo the variable name $result I get the following error Resource id #3 Please can someone come to my assistance. The aim of the exercise was trying to teach myself how to manipulate data from databases. Sadly I didn't get far. <?php mysql_connect("localhost","root","xxxx") or die("Cannot connect : " . mysql_error()); mysql_select_db("photorace"); $result = mysql_query("SELECT Tokens FROM `tokens` WHERE CompNumber='2'"); echo ("<table width=90% bgcolor=#a6d5dd border=2 align=center>"); while($row = mysql_fetch_array($result, MYSQL_BOTH)) { echo("<tr>"); echo ("<td align=center>$row[0]</td>"); echo ("</tr>"); } echo ("</table>"); mysql_free_result($result); ?> I will get the hang of it eventually I am sure. Cheers K Quote Link to comment https://forums.phpfreaks.com/topic/185146-simple-request-to-get-information-from-a-database/ Share on other sites More sharing options...
gevensen Posted December 14, 2009 Share Posted December 14, 2009 1st all in your query i do it like this for clarity, and if you use `` you have to use it on all not partially hope this sends you in the right direction $query="SELECT `Tokens` FROM `tokens` WHERE `CompNumber` ='2' "; $result = mysql_query($query); if($result) { while($row=mysql_fetch_array($result)) { echo $row[0]."<br />"; } echo "all done!<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/185146-simple-request-to-get-information-from-a-database/#findComment-977333 Share on other sites More sharing options...
Dazuti Posted December 14, 2009 Author Share Posted December 14, 2009 Cheers for that, it has certainly tidied up my code, though if I wanted to take that information that I have got from that field and manipulate it how would I do that. For instance. it's used to judge how many tokens someone has. If they buy 10 more tokens I need to manipulate the data to add 10 more to the total. Is $result the variable I need to do this? Quote Link to comment https://forums.phpfreaks.com/topic/185146-simple-request-to-get-information-from-a-database/#findComment-977336 Share on other sites More sharing options...
gevensen Posted December 14, 2009 Share Posted December 14, 2009 Cheers for that, it has certainly tidied up my code, though if I wanted to take that information that I have got from that field and manipulate it how would I do that. For instance. it's used to judge how many tokens someone has. If they buy 10 more tokens I need to manipulate the data to add 10 more to the total. Is $result the variable I need to do this? if you want the records returned use for example $num=mysql_num_rows($result); that gives you the number of rows returned i f($num>10){ //whatever } or even if(mysql_num_rows($result)>10){ //whatever } Quote Link to comment https://forums.phpfreaks.com/topic/185146-simple-request-to-get-information-from-a-database/#findComment-977357 Share on other sites More sharing options...
mikesta707 Posted December 14, 2009 Share Posted December 14, 2009 the following line while($row=mysql_fetch_array($result)) basically stores 1 row from the result set into that variable (as an array). There are many mysql_fetch_XXXX functions, and I suggest looking at the manual for information about that. Now how to manipulate this data depends on how much data/what data/ and how you want to manipulate it. For example, if you want to echo the data out, you would do something like you did in your example. If you wanted to store the result set for info later, you can do $data = array();//this is what will hold our data while($row = mysql_fetch_array($result)){ $data[] = $row;//this will put the contents of $row into the array $data } print_r($data);//this will print the structure of the $data array with $data, you could then do whatever you wanted Quote Link to comment https://forums.phpfreaks.com/topic/185146-simple-request-to-get-information-from-a-database/#findComment-977362 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.