I've the following script that is supposed to take a query
from a textarea and display the results in a table. This is using the Pear DB abstration layer, DB.php. The function numCol is in the file DB.php which is called at the beginning of the script. I'm receiving an 'undefined function' error at line 45, which is the line where the function numCols is called.
If I comment out this line, I get the same error from the call to numRows. If I comment out the call to numRows, I get the error when the function tableInfo is called.
All of the function calls that result in the undefined error message are called in the same way:
$cols = $res->numCols();
$rows = $res->numRows();
$table = $res->tableInfo();
which leads me to the idea that $res is not being initialized correctly. Which would further indicate that the query,
$res = $cnx->query($query);
is not working.
However, when I enter a deliberately bad query, e.g.,
select no_col_by_this_namel from table,
I get the correct query-error message back: that the column doesn't exist. And when I enter a correct query,
e.g., select * from table,
I don't get a query-error message at all, I just get the undefined function error.
I'm at a loss.
The code is below. I appreciate any help a lot.
Thanks
B
<?php
require_once 'DB.php';
If($_GET['submit']){
echo $_GET['query'].'<br>';
$query=stripslashes($_GET['query']);
echo $query.'<br>';
$user = '';
$pass = '';
$host = 'localhost';
$db_name = '***';
// Data Source Name: This is the universal connection string
$dsn = "mysql://$user:$pass@$host/$db_name";
// Connect to the database
$cnx = DB::connect($dsn); //fill "stuff" with your own DB info.
if (DB::isError($cnx)) {
echo 'No connection<br>';
die ($cnx->getMessage());
}
// Query the database
$res = $cnx->query($query);
if (DB::isError($res)) {
echo 'Invalid query<br>';
die ($res->getMessage());
}
// Determine the number of rows and columns in the result set
//line 45 is below:
$cols = $res->numCols();
$rows = $res->numRows();
// Print column headings
$table = $res->tableInfo();
echo("<table><tr><td> ");
for ($i = 0; $i < $cols; $i++)
{
echo("<td>".$table[$i]['name']."</td>");
}
echo("</tr>");
// Print query results
while ($row = $res->fetchRow())
{
echo("<tr>");
for ($i = 0; $i < $cols; $i++)
{
echo("<td>$row[$i]</td>");
}
echo("</tr>");
}
echo("</table>");
// disconnect from the database
$db->disconnect();
}
?>
<html>
<head>
</head>
<body>
<form action="<?php echo $PHP_SELF ?>" method="get">
<textarea name="query" rows="7" cols="50"></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>