LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Web based PHP-mySQL issue - pls help (https://www.linuxquestions.org/questions/programming-9/web-based-php-mysql-issue-pls-help-713244/)

ButterflyMelissa 03-20-2009 05:53 PM

Web based PHP-mySQL issue - pls help
 
Hi,

Ok, I tried, and did not find the answer...

Set up locally

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

// constants
define("conSITEACTIVE", TRUE);
define("conDBlogin", "db_login");
define("conDBpwrd", "db_pass");
define("conDBhost", "localhost");
define("conMainBase", "MainBase");

// 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;
}

?>

-------------------------------------------------
default.php

<title>Select you language, please</title>
<?php
include("fnts.inc.php");

$resMyLingoList; // the list of languages

function preps()
{
// main prepper
do_PrepSession();
do_OpenBase();
$resMyLingoList = get_lingolist();
}
?>
</head>
<body bgcolor="#000000" text="#efdfdf">
<?php
// prep the page here
preps();
?>
<link rel="stylesheet" href="sitestyle.css">
<center>
<table width="550" " height="100%" border="0">
<tr height="30" valign="top">
<td bordercolor="#000000" bgcolor="#316384" style="border: 0px solid;" align="right" valign="top" background="images/toplogo2.jpg">

<font color="#EDEBEB"><h2>Big Shopper - Welcome!</h2></font>
</td>
</tr>
<tr height="20">
<td bordercolor="#000000" bgcolor="#D00000" style="border: 0px solid;" valign="top" background="images/back01.jpg">

</td>
</tr>
<tr>
<td bordercolor="#666666" bgcolor="#050505" style="border: 1px solid;" valign="top">
<br>
<?php
if(!conSITEACTIVE)
{
?>
<center>
<h1>
We're still working...sorry."
</h1>
<br>
<img src="images/undercon.jpg">
</center>
<?php
}
else
{
?>
<br><br><br>
<center>
Please select your language...<hr>
<table width="200" cellpadding="0" cellspacing="0" border="0">
<tr valign="top">
<td width="100"><img src="images/lingo.jpg"></td>
<td width="100">
Lingolist<br>
<select name="lingoselect">
<?php
echo("<option>-Select-</option>");
while($dtarow = mysql_fetch_array($resMyLingoList, MYSQL_NUM))
{
echo("<option>$dtarow[1]</option>");
}
?>
</select>
</td>

</tr>
</table>
</center>
<?php
}
?>
<center>
<br><br>

<br>
</center>

</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...

Tnx in advance!

Thor

ghostdog74 03-20-2009 08:21 PM

after i read all that, i don't see you mention your problem.. sorry if i miss them. i would be better to place your code in code tags.

ButterflyMelissa 03-21-2009 02:21 AM

Hmm, yea'...

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...

Thor

graemef 03-21-2009 02:52 AM

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

ButterflyMelissa 03-21-2009 03:14 AM

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... :D

Thor

graemef 03-21-2009 07:50 AM

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:

echo mysql_num_rows(resSQLres);

jlinkels 03-21-2009 07:51 AM

In your code you are calling the function get_lingolist.

Instead of doing that, try to make the SQL query there where you call the function, do the fetcharray and print out the result.

The reason for this test is that I am not sure that you can return a resource (that is what mysql_query returns) as function result.

Alternatively you can do the fetcharray IN the function get_lingolist and return the array instead. Print the array from the function for debugging.

Please enclose you PHP code in PHP code tags next time (right most icon on the toolbar on top of the edit window)

jlinkels

ButterflyMelissa 03-21-2009 01:52 PM

Hello, and WOW!!!

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.

The array (once in default.php) LOST its data, you have to use the glbal keyword for that, see http://be.php.net/manual/en/language...bles.scope.php for more info on that...

So, I rebuilt the code. In the include

Quote:

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

Quote:

<select name="lingoselect">
<?php
echo("<option>-Select-</option>");
for($i=0;$i<count($myList);$i++)
{
echo("<option>" . $myList[$i] . "</option>");
}
?>
</select>
And that's it.

Oooof! NOW, my weekend can begin (saturday - quarter to eight in the evening )

Big tnx guys!!!! :D

Thor


All times are GMT -5. The time now is 04:55 AM.