LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-29-2004, 04:42 AM   #1
carlosruiz
Member
 
Registered: Jul 2003
Location: Japan
Distribution: Mandrake
Posts: 53

Rep: Reputation: 15
Php Help Required


Hello everybody, i am a newbie in php programming having a weird problem, i am trying to change the way a scripts gets its data, it currently uses a txt file, and i want to change it to mysql.

so far i have been able to locate the area where the data is being requested to the file which is:

Code:
function drs() {
if(file_exists($link_db)) {
  $file = file($link_db);
  foreach($file as $line) {
  if(ereg("^[^;#\r\n]", $line)) {
  $this->_links[] = split("\t+", trim($line));
  }
and i have code the following to replace it

Code:
$host = 'localhost';

$user = 'user';
$pass = 'pass';
$db = 'database';

$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
$query = "SELECT * FROM nw_structure";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
while ($this->_links[] = mysql_fetch_array($result, MYSQL_NUM));
mysql_free_result($result);
mysql_close($connection);

this piece of code returns exactly the same string as the first, and it works properly as long as i run it separately (not in the script function) any ideas????
it seems the problem is the loop if i comment the loop line, the piece of code runs inside the script, but of course i requiere the loop in order to get all the values.

ane help will be appreciated

Last edited by carlosruiz; 08-29-2004 at 04:45 AM.
 
Old 08-29-2004, 04:55 AM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Try seperating the condition for continuing the while loop and the statement to be executed when looping, if possible.
 
Old 08-29-2004, 05:22 AM   #3
carlosruiz
Member
 
Registered: Jul 2003
Location: Japan
Distribution: Mandrake
Posts: 53

Original Poster
Rep: Reputation: 15
Hello Proud, thank you very much for your reply, unfortunatly i don't understand what i have to do, could you please show me how the code should look like???? thanks a lot

Last edited by carlosruiz; 08-29-2004 at 05:27 AM.
 
Old 08-29-2004, 05:29 AM   #4
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Now I'm new to PHP and on a second look I think you dont need the looping part of that while line, as the fetch array method returns an array, not individual results from it, so you can just define
PHP Code:
$this->_links[] = mysql_fetch_array($resultMYSQL_NUM); 
and then the links array should contain all the returned results.

Edit: crash course on PHP MySQL documentation...

Try:
PHP Code:
for($i 0$row mysql_fetch_array($resultMYSQL_NUM); $row mysql_fetch_array($resultMYSQL_NUM); $i++ ) {
$this->_links[$i] = $row;


Last edited by Proud; 08-29-2004 at 05:36 AM.
 
Old 08-29-2004, 05:53 AM   #5
carlosruiz
Member
 
Registered: Jul 2003
Location: Japan
Distribution: Mandrake
Posts: 53

Original Poster
Rep: Reputation: 15
PROUD, thank you very much for taking the time to help me, i just tryed your suggestions, and the first one didnt work since it doesnt return the whole values, it does just one, BUT THE SECOND ONE WORKED PERFECT IN MY SCRIPT, GOD BLESS YOU. THANKS A LOT!!!!!!!!!!!!!!!!!!!

Last edited by carlosruiz; 08-29-2004 at 06:01 AM.
 
Old 08-29-2004, 06:08 AM   #6
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Quote:
mysql_fetch_array -- Fetch a result row as an associative array, a numeric array, or both.

Description

array mysql_fetch_array ( resource result [, int result_type])


Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.
Ok, your result rows are arrays, and there's hopefully more than one result row, so you probably want the first and maybe item from each row ($row[0]), for as many rows as the query result contains. So it's kinda a two dimentional array, but you only know if there's another row full of items (table column entries) once you call that fetch array method, so the assignment and testing for the next row must be together in the loop conditions.
Final version:
PHP Code:
for($i 0$row mysql_fetch_array($resultMYSQL_NUM); $row mysql_fetch_array($resultMYSQL_NUM); $i++ ) {
$this->_links[$i] = $row[0];
}

OR

$i 0;
while (
$row mysql_fetch_array($resultMYSQL_NUM)) {
this->_links[$i] = $row[0];
$i++;
}
unset(
$i); 
If you wanted each row of the result to be in your links array, use $row instead of $row[0].

Edit: Glad it worked, and this above text presumes wrong that you wanted $row[0] instead of $row. You can see from the second solution with the while statement that it wasnt your loop condition that was originally wrong, but your assignment of the result to the links array. You were overwritting it instead of setting the next entry to the next row.

Last edited by Proud; 08-29-2004 at 06:12 AM.
 
Old 08-29-2004, 06:14 AM   #7
carlosruiz
Member
 
Registered: Jul 2003
Location: Japan
Distribution: Mandrake
Posts: 53

Original Poster
Rep: Reputation: 15
Thanks a lot PROUD, the explanation is leading me to get more documentation on multi dimetional arrays, my code is now working as it should THANK YOU VERY MUCH....

Last edited by carlosruiz; 08-29-2004 at 06:16 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
what is the best distro for web developers...mysql, php, and apache required sketchydave Linux - Distributions 4 11-24-2005 03:24 PM
PHP: What is required for GD & ImageTTFBBox stefanlasiewski Programming 1 08-23-2005 02:33 PM
php apache or php cgi - php learner rblampain Linux - Security 3 12-17-2004 11:10 PM
php configuration erro libxml2 version 2.5.10 or higher required raninix Linux - Newbie 2 10-28-2004 10:59 AM
Some help required. Dsavage Linux - Newbie 1 07-23-2004 08:50 PM

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

All times are GMT -5. The time now is 06:16 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