LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2009, 05:53 PM   #1
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
Question 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
 
Old 03-20-2009, 08:21 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
Old 03-21-2009, 02:21 AM   #3
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
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
 
Old 03-21-2009, 02:52 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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
 
Old 03-21-2009, 03:14 AM   #5
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
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...

Thor
 
Old 03-21-2009, 07:50 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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);
 
Old 03-21-2009, 07:51 AM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 03-21-2009, 01:52 PM   #8
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
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!!!!

Thor
 
  


Reply

Tags
lookup, mysql, php, select



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Create a Web-based interface for MySQL databases in a flash with phpMyEdit LXer Syndicated Linux News 0 03-09-2007 08:16 AM
LXer: Implement MySQL-based transactions with a new set of PHP ... LXer Syndicated Linux News 0 06-21-2006 10:33 PM
Security risks of php based mysql queries TigerOC Linux - Security 5 04-10-2005 07:30 AM
mysql and php issue czarherr Programming 4 04-07-2005 05:36 PM
web-based email client (pls suggest) carboncopy Linux - Software 2 09-07-2004 01:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration