Jump to content

SQL Query/echo not working


timmymwd

Recommended Posts

Hey all,

 

I'm trying to run (what I thought) was a simple operation and it isn't working.  I haven't coded anything in about 2 years, and I'm self taught at that so I know I've got quite a few gaps in my knowledge base.

 

Here's what is running right now:

 

<?php

$con = mysql_connect("localhost","adminaccount","adminpassword");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mornin14_idiot2009", $con);

$id = $_GET['id'];

$query = "SELECT * FROM idiot WHERE index = '15'"; //I want this to say WHERE index = $id, but I'm testing this with just a #
$result = mysql_query($query);

$numrows = mysql_num_rows($result);


for ($x=0; $x<$numrows; $x++){
$result_row = mysql_fetch_row($result);
echo $result_row[0];
echo "<br />";
echo $result_row[1];
echo "<br />";
echo $result_row[3];
echo "<br />";
echo $result_row[5];
echo "<br />";

}


?>

 

Now it should only be returning one row since I'm checking for a specific index #.

 

When I just do a basic select * from idiot and omit the where clause, it works fine and dumps the whole table in the for loop.  But if I try a specific where, it falls apart.  Any help would be greatly appreciated.  Thanks!

Link to comment
https://forums.phpfreaks.com/topic/186316-sql-queryecho-not-working/
Share on other sites

What exactly do you mean by "falls apart"? It could be a number of things. Are you getting a PHP error? blank page? have you tried echoing the num_rows variable to see that it has what you expect? Taking these steps will help solve the problem faster.

 

But your problem is probably with the column "index". index is a reserved word so you can't use it as column/table names. You either have to change the column name (which is usually suggested, because using reserved words as column/table names can make your queries confusing) or surround the word "index" in your query with backticks like so

$query = "SELECT * FROM idiot WHERE `index` = '15'";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.