trying to get php to query mysql dbase

  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

my friends and i are working on a site and im trying to write code to make it dynamic... the guy who is providing us with a host hasnt set up our page dbase yet, so im using lycos to debug... i know the code is a bit messy, but im just tring to get it to display results atm...

[edit]

ok, forget all that, here is my MySQL thing:
Code: [ Select ]
$result[] = mysql_query("SELECT pageName, pageTitle, thePage FROM " . $whichSection . " WHERE pageName = '" . $pageName . "'", $DBLink);

the pageName is 'alpha' and whichSection is 'evangelion'

im trying to get it to return a result array of [alpha, beta, gamma], and those values are what is in the dbase...

[/edit]
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

Calendae, I don't quite understand your question.

What's your problem? How can help you?
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

i have a 'template page' that is the same throughout the entire site, the only thing that changes on the different pages is the page title, and the content in this one cell in the html table i have set up for the layout of the page.

i want to use a mysql dbase with 3 coloms (pageName, pageTitle, thePage) to store the different info in it.

currently, in the database there is an entry, following the order above of (alpha, beta, gamma)

the page name is obtained from the request URL, which is no problem... the URL is http://members.lycos.co.uk/fkoc/index.php?page=alpha

the code goes and gets the alpha (i checked) and sets $pageName to alpha for the mysql query

i am trying to get my php to query the dbase and return those values in an array called $result, which i can then use to echo the results for pageTitile, and thePage into my html code.

and i cant get the mysql query to work, i was wondering what i was doing wrong
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

Code: [ Select ]

// query mysql

$result = mysql_query("SELECT pageName, pageTitle, thePage FROM " . $whichSection . " WHERE pageName = '" . $pageName . "'", $DBLink);


// Fetch first row in my array

$array = mysql_fetch_array($result);
  1. // query mysql
  2. $result = mysql_query("SELECT pageName, pageTitle, thePage FROM " . $whichSection . " WHERE pageName = '" . $pageName . "'", $DBLink);
  3. // Fetch first row in my array
  4. $array = mysql_fetch_array($result);


Note: you have to use mysql_fetch_array() for each possible row the query may return, even if the query is know to return just one row.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

thanx Leo, i didnt have that, but its still not working :(

this is my first time doing anything with php/mysql so bare with me please :oops: ...

here is the entire revised page with the fetch in it:

Code: [ Select ]
<?php

echo("<br><br><br><br><br><br><br><br>");

$whichSection = 'evangelion';
$pageName = $_GET['page'];

echo($pageName);
echo($whichSection);

$DBLink = mysql_connect('localhost', 'fkoc_uk_db', '');
if (!$DBLink) {
  die('Could not connect: ' . mysql_error());
}
else {
    echo('connection established');
}

$sql[] = mysql_fetch_row("SELECT pageName, pageTitle, thePage FROM " . $whichSection . " WHERE pageName = '" . $pageName . "'", $DBLink);
$result = mysql_fetch_array(@sql);
echo($result);

$pageTitle = $result[1];
echo($pageTitle);
$thePage = $result[2];
echo($thePage);

mysql_close($DBlink);
?>
  1. <?php
  2. echo("<br><br><br><br><br><br><br><br>");
  3. $whichSection = 'evangelion';
  4. $pageName = $_GET['page'];
  5. echo($pageName);
  6. echo($whichSection);
  7. $DBLink = mysql_connect('localhost', 'fkoc_uk_db', '');
  8. if (!$DBLink) {
  9.   die('Could not connect: ' . mysql_error());
  10. }
  11. else {
  12.     echo('connection established');
  13. }
  14. $sql[] = mysql_fetch_row("SELECT pageName, pageTitle, thePage FROM " . $whichSection . " WHERE pageName = '" . $pageName . "'", $DBLink);
  15. $result = mysql_fetch_array(@sql);
  16. echo($result);
  17. $pageTitle = $result[1];
  18. echo($pageTitle);
  19. $thePage = $result[2];
  20. echo($thePage);
  21. mysql_close($DBlink);
  22. ?>


all the echos are in there so i can see the results...
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

Hey, it's PHP not PERL!!!

@ does not designs a variable.

Look:

Code: [ Select ]
echo("<br><br><br><br><br><br><br><br>");

$whichSection = 'evangelion';
$pageName = $_GET['page'];

echo($pageName);
echo($whichSection);

$DBLink = mysql_connect('localhost', 'fkoc_uk_db', '');
if (!$DBLink) {
  die('Could not connect: ' . mysql_error());
}
else {
  echo('connection established');
}


// query mysql
$result = mysql_query("SELECT pageName, pageTitle, thePage FROM " . $whichSection . " WHERE pageName = '" . $pageName . "'", $DBLink);


// Fetch first row in my array
$array = mysql_fetch_row($result);

$pageTitle = $array[1];
echo($pageTitle);

$thePage = $array[2];
echo($thePage);
  1. echo("<br><br><br><br><br><br><br><br>");
  2. $whichSection = 'evangelion';
  3. $pageName = $_GET['page'];
  4. echo($pageName);
  5. echo($whichSection);
  6. $DBLink = mysql_connect('localhost', 'fkoc_uk_db', '');
  7. if (!$DBLink) {
  8.   die('Could not connect: ' . mysql_error());
  9. }
  10. else {
  11.   echo('connection established');
  12. }
  13. // query mysql
  14. $result = mysql_query("SELECT pageName, pageTitle, thePage FROM " . $whichSection . " WHERE pageName = '" . $pageName . "'", $DBLink);
  15. // Fetch first row in my array
  16. $array = mysql_fetch_row($result);
  17. $pageTitle = $array[1];
  18. echo($pageTitle);
  19. $thePage = $array[2];
  20. echo($thePage);
  • CazpianXI
  • Proficient
  • Proficient
  • User avatar
  • Posts: 285

Post 3+ Months Ago

Note: the way you are presently doing it will only return the first row from your MySQL table. (It will work only if you just have one row in the table) Maybe that's your problem.

Try this:

Code: [ Select ]
$result = mysql_query("SELECT pageName, pageTitle, thePage FROM " . $whichSection . " WHERE pageName = '" . $pageName . "'", $DBLink);

$sql_result = array();
$a=0;
while($array = mysql_fetch_array($result)) {
   $b=0;
   foreach($array as $temp) {
     $sql_result[$a][$b] = $temp;
     $b++;
   }
   $a++;
}
  1. $result = mysql_query("SELECT pageName, pageTitle, thePage FROM " . $whichSection . " WHERE pageName = '" . $pageName . "'", $DBLink);
  2. $sql_result = array();
  3. $a=0;
  4. while($array = mysql_fetch_array($result)) {
  5.    $b=0;
  6.    foreach($array as $temp) {
  7.      $sql_result[$a][$b] = $temp;
  8.      $b++;
  9.    }
  10.    $a++;
  11. }


This will store all the rows and collums into the multidimensional array $sql_result.

Hope this helps.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

then how would i get it to pick out the 'alpha' row?
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

Just call mysql_fetch_row() again. If you want to get all rows in the query, use a for() from 0 to mysql_num_rows()
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

ok, back to the drawing board... it worked on the lycos server, and now that ive moved it over to the 'real' server its not working...

on the plus side, i am getting an error this time...

heres the code:
Code: [ Select ]
$sql = mysql_query("SELECT pageName, pageTitle, thePage FROM " . $whichSection . " WHERE pageName = '" . $pageName . "'", $DBLink);
$result = mysql_fetch_array($sql); //<--the error is on this line
mysql_close($DBLink);
  1. $sql = mysql_query("SELECT pageName, pageTitle, thePage FROM " . $whichSection . " WHERE pageName = '" . $pageName . "'", $DBLink);
  2. $result = mysql_fetch_array($sql); //<--the error is on this line
  3. mysql_close($DBLink);


heres the error:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource

i have no idea what the problem is...
  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

The query is not working, may be a table name changed, or a colum name.
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

What is the code you have that is where it actually contains mysql_fetch_row, because that is what the error is.
  • Cae
  • Expert
  • Expert
  • User avatar
  • Posts: 734

Post 3+ Months Ago

thanx, that was it, working again, ty

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: No registered users and 87 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.