tags:

views:

160

answers:

4

I'm making a website using php and I'm having some trouble with an SQL query.

$dataArray = array();
$result = mysql_query("SELECT * FROM test WHERE web_id='$websiteID'")
    or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
    $k = $row['kfoo'];
    $v = $row['vbar'];
    $dataArray[$k] = $v;
}

That's the code as it should be, however that causes the page to go blank (i.e. simply display all white). I've double checked and web_id is definitely the correct name and the correct case.

If I change web_id in the query to be, for instance, 'foo' (basically anything other than web_id), then the page will display, but with the error message "Unknown column 'foo' in 'where clause'".

Does anybody have any idea what could be causing this? Here's the code where I create the table test:

$dataDB = "CREATE TABLE test
(
    data_id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(data_id),
    web_id INT,
    kfoo TEXT,
    vbar TEXT
)";

mysql_query($dataDB,$con);

Update

Based on some of the answers here, I removed the quotes from around $websiteID and displayed errors using

error_reporting(E_ALL);
ini_set('display_errors', 1);

This displayed a lot of errors, including a syntax error earlier on that I've now fixed on. However, many errors remain and they don't make much sense to me. Here's the full code for my method:

function getOutputSQL($websiteID,$userInput) {
 $result = mysql_query("SELECT * FROM websites WHERE websiteID=$websiteID") 
    or die(mysql_error());

  while ($row = mysql_fetch_array($result)){
    $url = $row['url'];
    $exp = $row['exp'];
    $output= $row['textPrint'];
    $endUrlStart = $row['outUrlStart'];
    $endUrlEnd = $row['outURLEnd'];

    $image = $row['image'];
    $print = $row['print'];
    $post = $row['post'];

    $dataSource = $row['dataSource'];
    $dataSourceName = $row['dataSourceName'];     

  }

  // construct array of data names and values
  $dataArray = array();
  $result = mysql_query("SELECT * FROM test WHERE web_id=$websiteID") 
    or die(mysql_error());

  while ($row = mysql_fetch_array($result)) {
    $k = $row['kfoo'];
    $v = $row['vbar'];
    $dataArray[$k] = $v;
  }

  // construct array of expressions and replacements
  $result = mysql_query("SELECT * FROM regex WHERE web_id=$websiteID");
  $expArray = array();

  while ($row = mysql_fetch_array($result)) {
    $e = $row['ex'];
    $r = $row['re'];
    $expArray[$e] = $r; 
  }

  return getOutput($userInput,$url,$exp,$output,$endUrlStart,
     $endUrlEnd,$dataSource,$dataSourceName,$post,$image,$print,
     $expArray,$dataArray); 
}

The errors I'm getting are all like this -

Notice: Undefined variable: output in /home/daniel/web/resultsTest.php on line 113"

That repeats several times for url, exp, output, endUrlStart, endUrlEnd, dataSource, dataSourceName, post, image and print Line 113 is the large return line.

The thing is, as far as I can tell these variables are defined, and I know the table isn't empty, because I can display it on another page.

Solved

Sorted. The problem was actually in another part of my code - I was calling getOutputSQL incorrectly, but I've fixed it now!

+2  A: 

In general a blank page in php indicates disabled error notifications. At first you should enable them via .htaccess or php.ini.

BTW: As far as I know integers shouldn't be quoted in SQL.

Node
Quoting integers won't hurt.
Tomalak
+2  A: 

I expect that your actual code actually does out put anything, If this is all the code you have it makes sence nothing is outputted since you don't when there is no error (no echo's or prints are templates or anything)

Also make sure you set both error_reporting and display_errors are set. You can do this from the top of your script using

error_reporting(E_ALL);
ini_set('display_errors', 1);

That way uou'll see all errors.

EDIT
PHP has multiple types of errors, including fatal errors, warnings and notices. Notices tell you when your using undefined variables or deprecated functions, you can pretty much ignore them. You can change the setting so you won't see them using:

error_reporting(E_ALL ^ E_NOTICE);

Then you will only see real errors (warnings and fatal errors).

More Edit
Then those variables are truly unset, try something like this before you call getOutput():

echo "<pre>";
var_dump($userInput);
var_dump($url);
//etc.
echo "</pre>";

and see if the variables are really set.

Pim Jager
This exposed loads of errors, none of which make much sense to me. I've updated the question with the new problems.
Daniel
If I suppress Notices the way you suggest I simply get a blank page.
Daniel
The variables were set, but I've found the problem, it was to do with how I was calling getOutputSQL.
Daniel
+1  A: 

Try this instead:

$dataArray = array();
if(is_numeric($websiteID){
        $result = mysql_query("SELECT * FROM test WHERE web_id=$websiteID") or die(mysql_error());

        while ($row = mysql_fetch_array($result)) {
                $k = $row['kfoo'];
                $v = $row['vbar'];
                $dataArray[$k] = $v;
        }
}

Notice I removed the '' around the websiteID variable. This means mysql won't treat it as a string but as an int, which is what you specified in the mysql table creation. I also check to see if the websiteID is a number. If it is not, then there is no point in doing the sql query, as you will get zero results. This protects against sql injections.

Marius
A: 

Variables you define inside loops are destroyed at the end of the loop. That means the output variable you create in the first while loop does not exist after the loop. You should define them outside the while loop and then set the values inside the loop:

function getOutputSQL($websiteID,$userInput) {
 $result = mysql_query("SELECT * FROM websites WHERE websiteID=$websiteID") 
    or die(mysql_error());
  $url = "";
  $exp = "";
  $output = "";
  //... etc for all the other variables you need in the function at the end.
  while ($row = mysql_fetch_array($result)){
    $url = $row['url'];
    $exp = $row['exp'];
    $output= $row['textPrint'];
    $endUrlStart = $row['outUrlStart'];
    $endUrlEnd = $row['outURLEnd'];

    $image = $row['image'];
    $print = $row['print'];
    $post = $row['post'];

    $dataSource = $row['dataSource'];
    $dataSourceName = $row['dataSourceName'];                   

  }
  // the rest of your function...
Marius
I tried this and the page came up as blank again. Something strange is going on.
Daniel