• 0

[PHP/MySQL] Retrieving Data from a Single Row (Error: unknown column)


Question

I have a table with two columns: symbol and company_name. I retrieve the symbol from the url (e.g., .../showco.php?co=RSKIA). Then I want to go to the entry where the symbol matches that in the url (in this case, "RSKIA") and display the name of the company that has that symbol.

$co = $_GET['co'];

	$q = "SELECT * FROM CompanyList WHERE symbol=$co";
	$r = @mysqli_query ($dbc, $q);

	if (mysqli_num_rows($r) == 1) {

		$row = mysqli_fetch_array ($r);

		$company_name = $row[1];

		echo "<h1>" . $company_name . "</h1>";	

	}

I get the following error:

Unknown column 'RSKIA' in 'where clause'

Query: SELECT * FROM CompanyList WHERE symbol=RSKIA

I think this is a pretty basic problem. What am I doing wrong?

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Change this:


$q = "SELECT * FROM CompanyList WHERE symbol=$co";
[/CODE]

To this:

[CODE]
$q = "SELECT * FROM CompanyList WHERE symbol='$co'";
[/CODE]

Link to comment
Share on other sites

  • 0

Considering the parameter is coming from user input (in this case, freeform URL), you might want to read up on SQL injection and ways to prevent them in PHP.

Link to comment
Share on other sites

This topic is now closed to further replies.