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!