ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Client : Fedora10, server CentOS 5.2. Server has Apache, MySQL, PHP and PHPadmin installed.
I can use PHPadmin smoothly. So, PHP and mySQL both function properly.
I have a site (in PHP) where a visitor can select a language from a list taken from the server. The page is called default.php and it includes fnts.inc.php - a functions module.
In the include, there are two functions and some declares (constants and variables), one function opens the path to the database (with success - no errors) and the other one reads (or, it should) the list of available languages, once done, it returns the results as an array for default.php for further processing by means of filling up the combo.
Some snippets:
---------------------------------------------------
the include file
<?php
// variables
$lnkDBlink; // link to the database - global scope
// logic
function do_PrepSession()
{
// var & settings prepper
// some initting goes here - no problems...
}
function do_OpenBase()
{
// open the database
try
{
$lnkDBlink = mysql_connect(conDBhost, conDBlogin, conDBpwrd);
if(!$lnkDBlink)
{
die("no connect," . mysql_error());
}
mysql_select_db(conMainBase, $lnkDBlink);
}
catch(Exception $e)
{
// better to use a "DIE"???
echo($e->getMessage());
}
}
function get_lingolist()
{
// gets the list of languages
$strSQL = "select ID, descr, active from tblLanguages where active=1 order by ID";
$resSQLres = mysql_query($strSQL);
if(!$resSQLres)
{
die("No data fetched : " . mysql_error());
}
return $resSQLres;
}
</tr>
</table>
</body>
</html>
---------------| end of snippet(s) |----------------
Both files are in the same folder with read/execute rights to "others" and rwx to root and owner...the same rights apply to the images folder and its content.
Struct
./
defult.php
fnts.inc.php
/images
[content of /images]
the user (db_login) was defined in mySQL (I use the GUI for that) and has select and update rights on the database. Also, this user can approach the server from anywhere...
I fed the SQL to the database using PHPadmin, and I did get results - in this case two languages...
I tried a login with that user (PHPadmin) and I could log in (as that user)
I tried a function-return and I could read the returned value in default.php.
Where do I miss the point?
Any hints, tips, chunks of advice are very welcome...
I seem to have forgotten the question/problem (sorry) : I dont get any data. The code does not execute within the while loop, so I conclude that the data array is empty. I tried a call to mysql_num_rows (in the include) to see how many rows I got (should be 2) but got nothing, There is data in the base however...
If the number of row returned are zero then add some diagnostics to check that
The database connection is made
That you are able to select the database
That the SELECT works
Already did that. Here's a list:
-Deliberate error by using a bad/non-existing password - script died on an "access denied"
-Deliberate select of a wrong/non-existing base - script died too
-forced a select on a non-existing table - script died
conclusion - access works, select of the database works, select on the table works. Copied&pasted the SQL into the SQL frame of phpAdmin and did get data (2 rows)
I just hope I dont get arrested for having killed my script that many times...
I can't see what's wrong, although mysql_select_db doesn't throw an exception but returns false on failure.
In get_lingolist() I'd suggest that you try a call to mysql_num_rows() try:
Ok, first off, you're both right with your suggestions. I looked at the array INside the include, and it HAD stuff, but that got lost as - apparently - a function cannot return a resource...
I had to lace the code with all sorts of debug stuff, like count($array); and so on.
function get_lingolist()
{
// gets the list of languages
$intCounter = 0; // a counter
$arrList = array(); // the array to be returned
$strSQL = "select ID, descr, active from tblLanguages where active=1 order by ID";
$resSQLres = mysql_query($strSQL);
if(!$resSQLres)
{
die("No data fetched : " . mysql_error());
}
while($line = mysql_fetch_row($resSQLres))
{
$arrList[$intCounter] = $line[1];
$intCounter = $intCounter + 1;
}
return $arrList;; // return the ARRAY - not the resource...tnx jlinkels
}
Ok, so the array gets read out, all I had to do was itterate tru the array with a for loop, in the default.php
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.