Page 1 of 1

MYSQL table retrieval

Posted: Sat Jul 30, 2005 2:26 pm
by Vercz
[color=\"green\"]well ive been working on this for a while, and im not getting anywhere with it..i think its unfair on ada to make her wait any longer, so im throwing it open to the rest of the community...

note: im not, by a long shot..very good with mysql yet /blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />

i have a database, \"cards\" ..which has a four column table in it (ID, image, title, desc)

i am trying to pick a random number (1-156) and the print out the information in the corresponding row onto a php page

i think thats explain clearly enough /blum.gif\' class=\'bbc_emoticon\' alt=\':P\' /> here\'s the code (if it is commented out, it simply means that its there for when i get the scrips drawing two cards)

[code]<?php
$carda = rand(1,156);
//$cardb = rand(1,156);

$link1 = mysql_query("SELECT * FROM `image` WHERE `cardid` = \'$carda\'") or die (mysql_error());
//$link2 = mysql_query("SELECT * FROM `image` WHERE `cardid` = \'$cardb\'") or die (mysql_error());
$name1 = mysql_query("SELECT * FROM `title` WHERE `cardid` = \'$carda\'") or die (mysql_error());
//$name2 = mysql_query("SELECT * FROM `title` WHERE `cardid` = \'$cardb\'") or die (mysql_error());
$desc1 = mysql_query("SELECT * FROM `desc` WHERE `cardid` = \'$carda\'") or die (mysql_error());
//$desc2 = mysql_query("SELECT * FROM `desc` WHERE `cardid` = \'$cardb\'") or die (mysql_error());
$cardid = $row["cardid"];


echo "<center><table width=\'80%\' border=\'0\' cellpadding=\'5\'>";
echo "<tr>";
echo "<td><img src=\' . $link1 . \' alt=\' . $name1 . \'></td>";
echo "$name1";
echo "<td><center>$card2n</center></td>";
echo "</tr>";
echo "</table>";
echo "<table width=\'60%\' border=\'0\'>";
echo "<tr>";
echo "<td><center><b>Interpretation</b>:";
echo "$desc1 </center></td>";
//echo " $card2d";
echo "</tr>";
echo "</table>";
echo "</center>";

?>[/code]

any/all help is appreciated /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />[/color]

MYSQL table retrieval

Posted: Sun Jul 31, 2005 10:48 am
by ner0
[quote]four column table in it (ID, image, title, desc)[/quote]
I assume you mean (cardid,image,title,desc) lol.. otherwise change `cardid` to `ID` in your queries?... anyway I figure that\'s not the problem sooo..

the sql query statements look a little strange, try something like this:

[code]$query = mysql_query("SELECT * FROM `TABLENAME` WHERE `cardid`=\'$carda\'");
$data = mysql_fetch_array($query);[/code]

and just to show what I mean.. here\'s what the whole code should look like:

[code]<?php
$carda = rand(1,156);

$query = mysql_query("SELECT * FROM `TABLENAME` WHERE `cardid`=\'$carda\'");
$data = mysql_fetch_array($query);

echo "<center><table width=\'80%\' border=\'0\' cellpadding=\'5\'>";
echo "<tr>";
echo "<td><img src=\' . $data[\'image\'] . \' alt=\' . $data[\'title\'] . \'></td>";
echo $data[\'title\'];
echo "<td><center>$card2n</center></td>";
echo "</tr>";
echo "</table>";
echo "<table width=\'60%\' border=\'0\'>";
echo "<tr>";
echo "<td><center><b>Interpretation</b>:";
echo $data[\'desc\'] . "</center></td>";
//echo " $card2d";
echo "</tr>";
echo "</table>";
echo "</center>";

?>[/code]

that -should- work... I\'m not sure what the $card2n is, I assumed its been defined earlier in the script?

replace `TABLENAME` with the name of your table... and remember its \"SELECT fields FROM table... not SELECT data FROM fields...

hope this helps /good.gif\' class=\'bbc_emoticon\' alt=\'(Y)\' />

MYSQL table retrieval

Posted: Sun Jul 31, 2005 11:27 am
by Vercz
[color=\"green\"]hmmmm....

[i]EDIT: deletes all that stuffs...it now workies ^.^[/i]

THANKS RADARRRRRRR!!!!!!!!! /biggrin.gif\' class=\'bbc_emoticon\' alt=\':D\' />


check it out folks, let me know what you think

[url=\"http://funstuff.killanet.net/tarot\"]http://funstuff.killanet.net/tarot[/url]

[/color]

MYSQL table retrieval

Posted: Sun Jul 31, 2005 12:24 pm
by Vercz
[color=\"green\"]..and for those who care about the code, its below /smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />
obviously, for teh security reasons, i have edited the db_ name, username and password[/color]

[code]<?php
$db_host = \'localhost\';
$db_name = \'db_name\';
$db_username = \'username\';
$db_password = \'password\';
$connect = mysql_connect($db_host, $db_username, $db_password);
$databaseSelect = mysql_select_db($db_name);

$carda = rand(1,156);

$query = mysql_query("SELECT * FROM `cards` WHERE `ID`=\'$carda\'");
$data = mysql_fetch_array($query);

echo "<center><table width=\'80%\' border=\'0\' cellpadding=\'5\'>";
echo "<tr>";
echo "<td><center><img src=\'$data[image]\' alt=\'$data[title]\'></center></td>";
echo $data[\'title\'];
echo "</tr>";
echo "</table>";
echo "<table width=\'60%\' border=\'0\'>";
echo "<tr>";
echo "<td><center><b>Interpretation</b>:";
echo $data[\'desc\'] . "</center></td>";
echo "</tr>";
echo "</table>";
echo "</center>";
?>[/code]

MYSQL table retrieval

Posted: Mon Aug 01, 2005 7:34 pm
by ner0
Glad to see it working, good job /good.gif\' class=\'bbc_emoticon\' alt=\'(Y)\' />