Jump to content

Recommended Posts

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

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 />";

}

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?

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
}

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

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.