Jump to content

noob question.


tomek142

Recommended Posts

I had a quick PHP crash course for my Programming Language class and have received an assignment to do. The problem that I have is getting data from a database. I have done a little test to see if everything is ok and I have found a problem. In my database I have a single table (called Schema) and two columns, plaintext and cyphertext. Both columns are CHAR type so the plaintext columns has a single character and the cyphertext columns has the corresponding rotate-13 character of the plaintext column. So I have something like this:

 

plaintext      cyphertext

T                  G

a                  n

 

When I do a select query I just wanted to select the 'T' and the 'G' and echo them on the screen. My input file only includes one 'T' and no 'a'. When I run my code I get three 'TG' and three 'an' echoed but was thinking I will get only one 'TG'.

 

I'm sure there is something wrong with my database and not with the php code.

 

<?php
function connect()
{
$link = mysql_connect('127.0.0.1','root','root');

if(!$link){die('Could not connect: ' .mysql_error());}

if(mysql_select_db('Schema') === false)
{die('Cound not select database: ' .mysql_error());}

return $link;
}

$input = file_get_contents($argv[2]);

if($input === false)
{
echo "An error has occurred on file $argv[2]\n";
exit();
}

$link = connect();

for($i = 0;$i < strlen($input);$i++)
{
	$result = mysql_query("SELECT * from Cypher WHERE plaintext= '" .$input[$i]."';");

	if(!$result){die('Invalid query: ' .mysql_error() ."\n");}

	if($row = mysql_fetch_object($result))
	{
		echo $row->plaintext;
		echo $row->cyphertext;
		echo "\n";
	}
}
mysql_close($link);
?>

 

Input file:

Tomek
John

Output

TG
TG
an
an
TG
an

 

 

Link to comment
Share on other sites

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.