...

View Full Version : Php function() with mysql queries not working



Crowds
03-14-2008, 11:14 PM
I am creating a functions page to be included on all pages so I can keep pages tidy of code. Some of these functions are used to connect to and query a database. however these don't seem to work. Is it not possible to have Mysql queries inside of a function ? Or am I missing something.

The functions are as follows...


<?php
function connect() {
mysql_connect($sql_host,$sql_user,$sql_pass) or die(mysql_error());
mysql_select_db($sql_data) or die(mysql_error());
}


function indexFetch() {
$query = "SELECT * FROM CDblg_article_test ORDER BY date ASC";
$latest = mysql_query($query) or die(mysql_error());
$row_i = mysql_fetch_assoc($latest);
}

function indexList() {
include "inc/eng/index.php";

}
?>


I am calling the functions via this page...


<?php

include "inc/CDblg.php";
include "inc/functions.php";

connect();

indexFetch();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo TITLE ; ?></title>
</head>

<body>

<?php indexList(); ?>
</body>
</html>




indexFetch(); works just fine as I have tested the page that calls the functions by exchanging the functions for the actual code contained within them.

I am just trying to keep things tidy as the script requires the ability to have end users design their own layout around it. I looked into things such as smarty tags but from what I could tell smarty tags needs installing on the server with root privileges.

When I test the page it just appears blank and viewing the source shows it to be empty.

Any ideas ?

Crowds

tomws
03-14-2008, 11:34 PM
...
<body>

<?php indexList(); ?>
</body>
...



What's indexList() supposed to return? Should that return be echoed?

Crowds
03-14-2008, 11:43 PM
indexlist() is a function that contains html with a table and while loop to echo out the results from a mysql query. Currently that is the only function on the page that works

tomws
03-14-2008, 11:58 PM
mysql functions do work within other functions.

I'm wondering if the include in indexList() is breaking. Specifically, with respect to the path. Maybe it's trying to include relative to the functions.php path rather than the main path. Do you have access to the logs? If it's the path problem, it should give you an appropriate error there.

Crowds
03-15-2008, 12:02 AM
i don't have access to the logs...

But this is all it contains..


<table width="100%" border="0" cellpadding="0" cellspacing="0" class="indexTable">
<?php do { ?>
<tr>
<td colspan="2"><img src="img/DEC2.gif" alt="<?php $btitle ?>" width="510" height="47"><h2><a href="topic.php?id_top=<?php echo $row_i['id']; ?>"><?php echo $row_i['heading']; ?></a></h2></td>
</tr>
<tr>
<td style="padding-top:8px"><img src="img/bullit.gif" alt="<?php echo $btitle; ?>" width="10" height="10" /> <strong><a href="article.php?id_art=<?php echo $row_i['id_area']; ?>"><?php echo $row_i['heading']; ?></a></strong>&nbsp;</td>
<td><div align="right"><small><strong>Published: <?php echo $row_i['date']; ?></strong></small></div></td>
</tr>
<tr>
<td colspan="2"><br /><?php echo $row_i['text']; ?>
<br /><br />


</td>
</tr>
<?php } while ($row_i = mysql_fetch_assoc($latest)); ?>

</table>

oesxyl
03-15-2008, 12:59 AM
function indexFetch() {
$query = "SELECT * FROM CDblg_article_test ORDER BY date ASC";
$latest = mysql_query($query) or die(mysql_error());
$row_i = mysql_fetch_assoc($latest);
return $row_i;
}
?>


if you want to keep code tidy you must check if the things works for example to check $latest if is valid and do something if is not, write into a log file, mail to the ceo/owner of the company, or anything you think is usefull.

best regards

Crowds
03-15-2008, 02:44 AM
I tried returning it and there was no change. the page remains blank with no output or errors reported ?!

oesxyl
03-15-2008, 03:02 AM
I tried returning it and there was no change. the page remains blank with no output or errors reported ?!

this is probabbly because the query don't return anything. Try this:



function indexFetch() {
$query = "SELECT * FROM CDblg_article_test ORDER BY date ASC";
$latest = mysql_query($query) or die(mysql_error());
$row_i = "this query dont work";
if($latest && mysql_num_rows($latest) > 0){
$row_i = mysql_fetch_assoc($latest);
}
return $row_i;
}


or better:



function indexFetch() {
$query = "SELECT * FROM CDblg_article_test ORDER BY date ASC";
$latest = mysql_query($query);
$row_i = "this query dont work";
if($latest){
$row_i = mysql_fetch_assoc($latest);
}else{
$row_i = mysql_error();
}
return $row_i;
}


best regards

Crowds
03-15-2008, 03:41 AM
Thanks oesxyl. Tried both versions but still no joy.
However, I did try adding the include CDblg.php which contains the database details within the connect() function and it now connects to the database but the indexFetch is failing to retrieve any data.


function connect() {
include "inc/CDblg.php";
mysql_connect($sql_host,$sql_user,$sql_pass) or die('Nope cant connect');
mysql_select_db($sql_data) or die(mysql_error());
}

oesxyl
03-15-2008, 03:58 AM
Thanks oesxyl. Tried both versions but still no joy.
However, I did try adding the include CDblg.php which contains the database details within the connect() function and it now connects to the database but the indexFetch is failing to retrieve any data.


function connect() {
include "inc/CDblg.php";
mysql_connect($sql_host,$sql_user,$sql_pass) or die('Nope cant connect');
mysql_select_db($sql_data) or die(mysql_error());
}


try this, put at the begining of the file before any include:



<?php
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting (E_ALL);

?>


best regards

tomws
03-15-2008, 04:42 AM
Moving the db connection details into the function and getting a result shows part of the problem. It's all coming come to variable scope issues, apparently. I was able to make your code work by stirring up this stew of functions and passing vars in and out down your function waterfall. Here's the relevant portions on the main index.php and the functions.php:


// index
$dbconn = connect();
$arr= indexFetch($dbconn);
...
<?php indexList($arr); ?>
...

// functions
<?php
...
function indexFetch() {
$query = "SELECT * FROM CDblg_article_test ORDER BY date ASC";
$latest = mysql_query($query) or die(mysql_error());
$row_i = mysql_fetch_assoc($latest);
$rv = array($row_i,$latest);
return $rv;
}

function indexList($arr) {
$row_i = $arr[0];
$latest = $arr[1];
include "inc/eng/index.php";
}
?>


Yes - it's sloppy, but it makes it work. Attempting cleaner code is a good thing, but at a point, it becomes unreadable and difficult to maintain. Merging some (if not all) of this would make it easier to read and easier to debug. (Be sure to follow oesxyl's advice above, too.)

edit: By the way, my passing back the result AND the row is stupid. I'm only doing that so I didn't have to edit yet another included file (/inc/eng/index.php). If insisting upon this function stew, it would be best to pass back just the result and extract the row from the next function.

Crowds
03-20-2008, 11:47 AM
Thanks tomws,

Sorry for the late reply. Your solution is working fine ! :)

Thanks again.

Jim

MatuduStrauss
03-20-2008, 11:56 AM
About that listbox generation i found the solution. the problem was that i was trying to use php coding inside a html form and that wont happen unless i editedthe .htaccess file but it comes with a price. the best way to do it is to name the form u are using with a .php extension that worked for me as php doent recognise a code within any file unless its told.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum