Forum Moderators: coopster

Message Too Old, No Replies

empty output, can't understand why!

         

dbarasuk

1:03 am on Feb 23, 2008 (gmt 0)

10+ Year Member



Hi,
The next script is designed to show in a webpage the columns from a table named "item" from the SQL query "SHOW COLUMNS FROM item"

I expect to see the columns of that table in the webpage but I can't. Instead I get an empty page.

Can you please tell me where is the error by analyzing the code?

Thanks in advance:

<?php
// Define MySQL connection
$connection = Mysql_connect('localhost', '', '');
// select database
mysql_select_db('cookbook', $connection);
// function to extract column names
function get_column_names_with_show($connection, $tbl_name)
{
// query definition
$query = "SHOW COLUMNS FROM item ";
// execute query and keep result_id in $result
$result = mysql_query($query, $connection);
If(!$result)
{
return (FALSE);
}
// array to contain retrieved column names
$names = array();
// Loop to return an array of names
while( list($name, $type, $null, $key, $default, $extra) = mysql_fetch_row($result))
{

$names[]=$name; // append name to array
}
mysql_free_result($result);
return($names);
$names = get_colum_names_with_show($connection, 'item') ;
}


?>

[edited by: eelixduppy at 1:04 am (utc) on Feb. 23, 2008]
[edit reason] removed credentials [/edit]

phparion

6:34 pm on Feb 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



where are you calling the function? within the function definition :)

take this out of the function body

$names = get_colum_names_with_show($connection, 'item') ;

phparion

6:34 pm on Feb 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



also in your query you are hard coding the table name then what is the merit to send the table name as variable?

dbarasuk

7:20 pm on Feb 24, 2008 (gmt 0)

10+ Year Member



Hi,
I changed the while loop to look like:

while( $row = mysql_fetch_assoc($result))
{
$col = $row['field'];
$names[]=$col; // append name to array
}

mysql_free_result($result);
return($names);

to retrieve only the first column of the result in which i am interested and then append the successive values to the $names array.

As you see i also took out the line mentionned above, but still getting an empty page.

Note that I tested if the sql really is ok before i put it in the php code.
Any idea?

phparion

8:01 am on Feb 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



blank page also appears when your code has some syntax error and your error_reporting is switched off... check your log to see what error is thrown and fix that... most of the times a missing ; or . or an unnecessary { }