Page 1 of 1

pulling page content from mysql db and executing querys

Posted: Tue Dec 10, 2002 4:25 pm
by fariquzeli
I have my website pulling its content from a mysql database. Now on one of my pages that is being pulled from the database, specifically the index, I would like for more mysql queries to take place within that page to make it dynamically db driven as well.

Is this impossible? Do I have to put my queries on my templatepage.php that calls up the content and then just echo out what I need within the page. I'm running into quite a bit of trouble with it and no information will show up at all if i put all of the php/mysql querys into the db, but it will also not work if I put the queries outside, i just figured I may have been doing it wrong.

any suggestions or anything would help a bunch, thanks!

Posted: Tue Dec 10, 2002 4:29 pm
by qads
well..i have't worked with this much but i some times use eval(); to run php stored in db, give that a try.
here is more iinfo: http://www.php.net/manual/en/function.eval.php

Posted: Tue Dec 10, 2002 4:36 pm
by fariquzeli
I read up on it, i'm not entirely sure I understand

If I want to echo out one of the variables from the query (which was inside the database) i would do something like this?:

$sql = "SELECT * FROM products WHERE id = '5' LIMIT 3";
$query = mysql_query($sql);
$result = mysql_fetch_assoc($query);

eval(echo $result['productName'];)

return();

or would i put the entire bit of codde in that??

Posted: Tue Dec 10, 2002 4:57 pm
by JPlush76
so basically what you want to do is have multiple queries on the same page?

here is a function that I use on all of my scripts:

Code: Select all

<?php
function dbconnect() 
{ 
 global $dbhost, $dbuname, $dbpass, $dbname; 
 mysql_connect($dbhost, $dbuname, $dbpass); 
 @mysql_select_db($dbname) or die ("Unable to select database"); 
} 


//***** QUERY MYSQL RESULTS
function query_db($query) 
{ 
 dbconnect(); 
 return @mysql_query($query); 
} 
?>
that does my database connection...
now when I want to do queries I can do:

Code: Select all

<?php
// GET MOST REQESTED SHIPPING METHOD
$result = query_db("SELECT shipping_method,count(shipping_method) as ship FROM orders GROUP BY shipping_method ORDER BY ship desc LIMIT 1");
$row = mysql_fetch_array($result);
$shipmeth = ($row['shipping_method']);
$shipmeth = shipmethod($shipmeth);

$shipcount = ($row['ship']);

// GET POPULAR STATE
$result = query_db("SELECT cust_state,count(cust_state) as state FROM customers GROUP BY cust_state ORDER BY state desc LIMIT 1");
$row = mysql_fetch_array($result);
$state = ($row['cust_state']);
$statenum = ($row['state']);

?>
even though I have the same sql variable "RESULT" I can reuse it becauseI dont care about that SQL query anymore I just want my results and then I can assign the results to a variable.

Posted: Tue Dec 10, 2002 5:00 pm
by fariquzeli
Oh no no that's not what I mean.

It's alright I just decided to use an include() and included the entire .php file so that it would stay as php

for futur reference you can't use bare php out of a mysql database without using eval or some other function that I'm unaware of how to use.

Posted: Tue Dec 10, 2002 5:01 pm
by JPlush76
ahh gotcha, so you actually had PHP code in the database :oops:

Posted: Tue Dec 10, 2002 5:04 pm
by fariquzeli
yea, and that's a no-no, :)