Log in

View Full Version : Calling info from a database via ID


Blue Moose Aaron
08-07-2004, 08:20 PM
This is a tough one, but i was wondering if you ever ran into a problem using words rather than numbers as IDs in php/mysql before?

Before when I was simply caling files I had no trouble using words (ie smallville.php?action=bios&character=clarkkent), but now that I am calling info from the database it would only work if I called a number (ie smallville.php?action=bios&character=1)

I'm not sure what I can do about this. it isnt a huge deal, I just would like to try to keep my links the same as they were for the sake of bookmarks

If it isnt clear what Im talking about I can explain more.

Here is the code in using to call the info
case 'bios': {
$bio = intval($_GET['bio']);
$row = $DB_site->query_first("SELECT * FROM smallville_cast WHERE id=$bio");
$id = nl2br( $row[id] );
$name = nl2br( $row[name] );
$character_name = nl2br( $row[character_name] );
$image = nl2br( $row[image] );
$birth_date = nl2br( $row[birth_date] );
$birth_place = nl2br( $row[birth_place] );
$info = nl2br( $row[info] );
$character_info = nl2br( $row[character_info] );
eval("\$home[header] = \"".fetch_template('vbindex_header')."\";");
eval("\$nav = \"".fetch_template('vbindex_customblock_1')."\";");
eval("\$next_on_smallville = \"".fetch_template('vbindex_customblock_6')."\";");
eval("\$smallville_episode_select = \"".fetch_template('smallville_episode_select')."\";");
eval("\$smallville_biolink = \"".fetch_template('smallville_bio_links')."\";");
eval( 'print_output( "' . fetch_template( 'smallville_bios' ) . '" );' );
}

Like I said before I originally set up the ID field to hold 'clarkkent' or 'lanalang' but it wouldnt work. However as soon as I changed the ids from clarkkent to 1 it worked.

Modin
08-07-2004, 08:27 PM
I've used names to call many things from a database and never ran into a problem.

Maybe you had an error in your code? Post the code that didn't work for you...

Blue Moose Aaron
08-07-2004, 08:35 PM
thats the code above. I got no error, it would just always send me to the first row of the the table.

nexialys
08-07-2004, 08:38 PM
if didn't create a while {}... so it's taking only the first in the query...

Blue Moose Aaron
08-07-2004, 08:43 PM
Can you put that in novice terms? I barley know what im doing :-\

Blue Moose Aaron
08-08-2004, 04:14 PM
<a href="http://www.thekryptonian.com/smallville.php?action=bios&bio=jonathankent" target="_blank">http://www.thekryptonian.com/smallvi...o=jonathankent</a>

Thats the page. You can see that if you click links in the Smallville Cast box on the right that you always get Clark Kents info from the database no matter what the url calls for.I dont understand why it calls the correct info when the id is a number but not a word. Should I make my ID row something other than varchar?

Colin F
08-08-2004, 06:33 PM
well you can't use intval() if you want it to be a string (like "jonathonkent")
try deleting the first row and changing the second to

$row = $DB_site->query_first("SELECT * FROM smallville_cast WHERE id=$_GET[bio]");

Blue Moose Aaron
08-09-2004, 10:52 AM
I get an SQL error. For some reason its not catching that the word names are the id's of the table.

Database error in vBulletin 3.0.3:

Invalid SQL: SELECT * FROM smallville_cast WHERE id=clarkkent
mysql error: Unknown column 'clarkkent' in 'where clause'

mysql error number: 1054

Date: Monday 09th of August 2004 06:50:47 AM
Script: http://www.thekryptonian.com/test.php?action=bios&bio=clarkkent

Colin F
08-09-2004, 02:41 PM
you need to surround the id with 's
thats: '

making it: $row = $DB_site->query_first("SELECT * FROM smallville_cast WHERE id'=$_GET[bio]'");

Blue Moose Aaron
08-09-2004, 04:39 PM
Thanks so much for your help. I appreciate it!

Dean C
08-09-2004, 04:53 PM
I can't stress this enough but ALWAYS addslashes any data that will be going into your database, where the input should be a string. Intval it if it's a number.

So the row Colin gave you should be:


$row = $DB_site->query_first("SELECT * FROM smallville_cast WHERE id='" . addslashes($_GET[bio]) . "'");