Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Parsing PHP Code In MySQL Results Set?


I have a PHP script that pulls content from an MySQL database and returns a result set.

Is there a way I can put PHP code within a field in the database and have that code parsed as PHP code on returning a results set? If possible, I want just one field of the database parsed for PHP code. The results set is return as a straight result, not within a loop. Just inserting the <? PHP code thus ?> returns that as sources code not parsed.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
PHP Parsing Shell Command Results
I am running a shell command via a php started ssh connection to a mail
server. I need to grab the data the shell command displays and then load
them into variables or an array ... Can anyone give me ideas on how to
do this ...

Note: I posted this on alt.php.sql ... so this is a cross post...

Can PHP Parsing Xml Code ?
I am looking for a programming script to parse a xml file. Can PHP do it. The xml file is from other web site and i need to parse it into my html page.

BB Code Parsing
how would I go about coding a vBulletin style BBcode parsing engine?

eg. to translate [ b] something [ /b] into <b>something</b> etc

I know i need todo regular expression searches on the strings, however im a reg-exp newb and really need some guidance. Alternatively, are there any free premade classes etc I could us?

PHP Not Parsing This Code?
I'm trying to populate an array (with some strange characters). The problem I'm having is the page will ether come up blank, but show the code in the "page source". Or, some or all of the source is sent directly to the browser, like PHP didn't even parse it. I've tried this on a few different servers and get similar, though slightly different results. Here are some examples of what I'm trying to store: Code:

Help My Slow Parsing Code!
Here's my situation...I've been learning to write PHP for awhile and have been trying to rewrite my web site from plain old static pages to PHP versions. I've been learning based on what I want to do, put my links and articles in databases and allow comments and ratings.

I have a test page here:
URL

I've managed to write the PHP code to do just about everything I wanted, but it takes about 3+ seconds for the page to start to load (and it didn't when it was a static page). I know that this is because of my badly formed PHP no doubt. The routines do what I want, but there HAS to be a much more efficient way to get it done.

I'm going to post the code (stipped of font tags and error catching statements). Please advise the easiest way to streamline this code (and or SQL statements) in the best interest of the server processing time (any help would be great!):

<?php
//Start of routine for animation links
$usr = "user";
$pwd = "pw";
$db = "db";
$host = "host";

$cid = mysql_connect($host,$usr,$pwd);
?>

<?php
//We want the Animated Gif category
$category = "1";

$SQL = " SELECT * FROM links_temp";
$SQL = $SQL . " WHERE category = '$category'";
$retid = mysql_db_query($db, $SQL, $cid);

//Throw the links in an array - html table loop
print ("<center><table cellpadding=2 cellspacing=0 width=100% border=1>
");
while ($row = mysql_fetch_array($retid)) {
$siteurl = $row["siteurl"];
$sitename = $row["sitename"];
$description = $row["description"];

//cocatenate together anigif_ with each item id for unique rating value
$id = "anigif_";
$id = $id . $row["id"];

print ("<tr><td width=100% valign=top>");

//Lets start the form here for the voting to elimate a line break later
print ("<form method="POST" action="/test/rating4.php" onsubmit="return genwin()" target="newwin">
");
print ("<A HREF='$siteurl' target=_blank>$sitename</A>");

//While in the while loop lets start another db connection to get item ratings
$usr3 = "user";
$pwd3 = "pw";
$db3 = "db";
$host3 = "host";

$cid3 = mysql_connect($host3,$usr3,$pwd3);

$SQL3 = " SELECT * FROM ratings_temp WHERE item = "$id"";

$result3 = mysql_db_query($db3,"$SQL3",$cid3);
$num_rows = mysql_num_rows( $result3 );
if ($num_rows==0) { echo ("No Votes yet"); }
else {
print " $num_rows Votes, ";
$SQL4 = "SELECT AVG(value) AS avgRating FROM ratings_temp WHERE item = "$id"";
$result4 = mysql_db_query($db3,"$SQL4",$cid3);

print "Rating: " . mysql_result($result4,0) . "<br>";
}
mysql_close($cid3);
//end of the db connection for ratings

print ("$description<br>
");

print ("Vote for this Site!
");
print (" <select name="rating" size="1">
");
print (" <option value="5" selected>5 - Superb
");
print (" <option value="4">4 - Tasty
");
print (" <option value="3">3 - Average
");
print (" <option value="2">2 - Bland
");
print (" <option value="1">1 - Rotten
");
print (" </select>
");
print (" <input type="hidden" name="item_id" value="$id">
");
print (" <input type="submit" value="Vote">
");
print ("</form>
");
print ("</td></tr>
");
}
//end of the array subroutine making table rows

print("</table></center>");
//end of the table after the array ends
?>

Parsing String With Embedded PHP Code...
On my webserver right now, I have a folder of webpages with embedded PHP coding. Because the dynamic content changes very infrequently, I've written a script which generates a "static cache" of HTML pages from the PHP pages. It works by automatically looping through all PHP pages in the folder. For each, it reads the contents of the PHP file into a variable, then creates a new HTML page with the same name and writes the contents of the variable to it. After the process is complete, the cache of HTML files gets uploaded to the website for public viewing.

However, if the script reads the contents of the PHP pages through the local filesystem, the temporary variable (and thus the resulting static .html page) will have PHP code within it, which is obviously no good.

Therefore, at the moment, the script reads the contents of the PHP pages by opening an http connection to where they reside on the public webserver... this makes the contents of the temporary variable equivalent to the HTML that would be outputted when any user accessed the PHP page through the WWW, which is exactly what I want.

However... my problem is that the http connections are much slower than reading through the local filesystem, and moreover, I don't necessarily want the PHP pages to be publicly accessible through a webserver, which is currently required for the script to work. Is there any way that I can take a PHP string that has embedded PHP code and tell PHP to "parse the PHP out of it"?

PHP Code Parsing From Database Only Works Selectively
I have a script for my templating system which grabs PHP code from a mySQL database, and parses it. Here it is: PHP Code:

Search Results ($num_rows) Does Not Match $search Results (PHP,MySQL)?
For example, when I do a database search for a term I get 2 results but the $num_rows says there are 3 results.

I suspect it's because there are similar keywords within different search fields of the SQL statement. But I am stumped as to how I can change this. Any Suggestions? the code:

.csv/MySQL Parsing Script
I have a several thousand row spreadsheet which contains a column for a full name (i.e. William R. Jennings, Jr. PE). What I'm trying to do is truncate the data in that column to only leave the first name (i.e. William) for all rows. The table is also set up in a MySQL database.

Parsing PHP From A MySQL Database
i am creating a CMS and i have a setup so you can add sections and they get stored in a db then you can call them with index.php/section. however this doesn't work right if you have PHP in the content of the section. the PHP code shows up instead of getting parsed.
PHP Code:

Parsing Xml And Using Php Inserting In Mysql
How to parse this xml and generate php code  to insert to mysql. In this example xml there are 3 articles, one with 2 photos and the others with just one. A Php5 solution requiered. Code:

Parsing Output From MySQL Database Through Php
Hi All,

I want to put php pages in a MySQL database, and then get the output parsed by php. Getting the pages in MySQL is no problem, but the output is only displayed, not parsed.

Parsing Mysql Data Into Template
is there any easy way to parse the result getting from mysql database into a template i have seen fasttemplates and phlib but it still difficult to understand.

Parsing Date String From Php Into Mysql?
Three select fields within the html code having the following attributes:

<select name="day">
<option>1
<option>up to 31
</select>

<select name="month">
<option>january
<option>up to december
</select>

<select name="year">
<option>1996
<option>up to 2032
</select>

Now I want to combine these fields with php and give that parsed date into a field in mySQL that has the following structure:

fullydate date NOT NULL default &#390000;-00-00',

AS you can see the name of this field is 'fullydate' and it´s set to NOT NULL. Now my question: How can I insert the values of the select fields into the DB using the correct format? And later on, how can I read it out again that it will be splitted into the three parts again? Like if I want to edit that date using php.

thx for any help.

Stream From FTP Directly To MySQL While Parsing CSV
I have some files that sit on a FTP server. These files contain data
stored in a tab-separated format. I need to download these files and
insert/update them in a MySQL database. My current basic strategy is to
do the following:

1) Login to the ftp server using the FTP library in PHP
2) Create a variable that acts like a file handle using Stream_Var in PEAR.
3) Use ftp_fget() to read a remote file into this variable (this is so I
don't have to write it to disk).
4) Parse that data now stored in memory using fgetcsv() (again treating
that variable as a file handle using Stream_Var). This produces an array.
4) Insert/Update the data in the array using DB in PEAR.

This all seems to work and it means I don't have to write anything to
disk. Everything is handled in memory so not temp files are needed. The
downside is that some of these files are very large so the program can
consume large amounts of memory. I want to see what I can do to reduce
this memory usage.

In a perfect world I don't need to keep the entire file in memory. As
soon as a single line is read via FTP I should be able to pass that line
off to the CSV parsing code and the MySQL insert/update should be able
to take place as each line is parsed by the CSV library. I.E. I should
have more than a buffer worth of data in memory at a time. A buffer
would need to be able to store at least a entire line but my memory
requirements would drop significantly.

My problem is that I can't seem to be able to figure out how to do this
with the current PHP libraries. It seems that most functions in PHP are
not designed around the idea of piping streams of information together.

The other restriction I have is that I am limited to just PHP 4.3. Any
ideas or is holding the entire file in memory the best way (other than
writing my own libraries).

Parsing XML In MYSQL Using Php, Date Issue
I am trying to parse an XML file into a MYSQL database and retreve it later on sorted by date. So far I have: Code:

MySQL Results Not Working So Hot
Ok. Here I am again....probably posing a question with a solution so easy that, after looking at it for an hour, I can't figure out. Ok. here's the deal. please review the code below. Looks simple enough, right?

If/then Within MySQL Results List
I want to list job opportunities for a facility. If there are jobs in the database, I can list them without a problem. But if there are no jobs, I'd like to echo a line something like: "No jobs right now, come back later." But I can't get that line to echo. PHP Code:

Mysql Writing Results From 51 To 100
Can someone write me a mysql_query code which will get me results from 51 to 100 from $database?

Don't suggest me working with indexes, because i'm also deleting some rows (for example #1 is filled #2 is not, #3 is...)

Show Next Results Of MySQL DB
How do I get it so that php reads in the first 10 results out of a MySQL database. Then when Next or something is hit it displays the next 10 results and so on.

MySQL Results To Return Only One Value
For example, I have a column that has three different values, 1, 2, & 3. In the database, there may be over 500+ entries with one of these values assigned to it. How can I get MySQL to return just 1, 2, & 3 only once rather than several hundred times.

Hyperlinked MySQL Results W/PHP
I was wondering how results to db querys have attached links to each individual result? I have a good grasp of basic PHP & MySQL, but don't understand how that works. For example, if you have a db of products, all with individual id #, how do you get one product from the query a <ahref>? Could someone explain?

Compiling MySQL Results
Does anyone know how to compare multiple results from a MySQL query? For example, I would like to query the database and collect all the results into a variable (perhaps $totalresults). Then, I would like to use a single statement to compare each of the results within $totalresults to the statement's conditions.

Mysql: How Do I Get The Rowcount Of The Results?
I want to execute some alternate code if the a query produces zero results, but I can't find any info on the web about how to get the number of rows in a mysql result.

Getting Results From MySQL Database In PHP
I am trying to write a php page which connects to a MySQL
Database which is supposed to get the results from a table within a
database and display the results in a table.

Below is the code that I am using:

Limit Mysql Results
I have a script which has a mysql query which returns 20 results - I want to add some validation so if my variable ($var) is empty, it only allows the user to select the first 5 results my code is: -

$sql = mysql_query("SELECT * FROM greetingcards_cards WHERE category='$cat' LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){   
        $image = $row['url'];
       $cardid = $row['cardid'];
        echo "<br><img src="$image" /><br><a href="?function=sendmessage&cardid=$cardid">Pick this card</a><br>";}

I want it so that if the variable is empty, instead of displaying "Pick this card" it displays - "You must register to pick this card" and have a different hyperlink Is this type of validation possible? if so how is it done?

PHP MySQL Order Results
I'm using:

  $sql = "SELECT * from $myTable ORDER BY `points` DESC LIMIT 5";
in a whileloop to display the top five scorers but would like to put their position next to the results (eg, 1, 2, 3, 4 and 5)

Is there an easy way to do this?

Convert MySQL Results To XML
I just got Dreamweaver CS3, and I love the built in SPRY framework. It allows you to create web applications that use a table listing, and details listing very quickly. Unfortunately the only data the SPRY framework can accept is XML. Is there a built in function to convert from a $row result from MySQL to XML? Code:

Mysql Results - Ordering Them.
i need an mysql query to rank the results, like SELECT field FROM table and to order them greatest to least by the value of a different field. 

Mysql Results Pagination
I have a php code to split the results of a sql query over pages taht works great... But I need to add some feature, I need to add that if there are more than * pages it shows me "..." instead of the hole numbers of pages.. Code:

Sum All Of My MySQL Integer Results?
I have a bunch of rows, and for each row, the column "rating" has a number from 1-5. I need to add all of these together, so like if there were 3 rows with 5 for rating, it would return 15. If it's possible to do it with an SQL statement, I'll like it to average them.

Splitting MySQL Results
So I have a database, and I want to split the results into 2 columns. I don't want to alternate them, but have the first half in the left column, and the second half in the right column. How can I handle this so that it does it automatically no matter what number of entries (and so that if it's an odd number, the one overflow is on the left).

MySQL Multiple Tables / Results
I would like to perform multiple queries to a MySQL database and have all
the results in 1 result set.

Populating Array With Mysql Results
I'm returning a result set of one field in a table, and want to populate an array with the results. I can't for the life of me figure out a simple way to do this without using mysql_fetch_array() to cycle through the results, append that to a var, then explode that into a var and pass that. PHP Code:

MySql - Outputting Results In Groups
I want to output mysql data by groups (states). However each time php/mysql comes to a new state in the results I want it to print the state name before putting the results. Does php have some sort of memory function to recognize when the state has changed?

Split Mysql Results Into Two Columns
Trying to develop php catalog, but can't split the results into two columns, PHPcode:        

Mysql Results Output Problem
I trying to set up some subdomains and I'm at the early stages of just testing the the thing . I have set up a small test DB, with the following fields, company, id, url.

I have also set up a test page to see if the output corresponds with the subdomain name. If the domain exists the result with echo "domain exists" if not it will output "does not exist". But the page just says "does not exist" all the time even though the domains are set up.here's the code:

Exploding Results Of Mysql Query
I have a table with times and dates listed like this:

0000-00-00 00:00:00
2003-09-20 20:26:22
etc
etc

What I want is to query the DB for the field (matchdate) and then get it to explode the ' ' to give jsut the date for me and then put it into a dropdown in a loop. this may sound easy but i too sleepy to work it out and searching aint good.

Store Mysql Results Into An Array
i just started using php. i've used it before as a maintainer. fun stuff. well now i'm the writer. i want to abstract all the DB commands away from myself and all other developers. so i'm writing a function to query the php database from all users of the database and store them into an array as a return value so that i can seperate my database code from my html creation code.

1. is there a way to put all my database functions into a php file and include that file into all my other php pages? similiarly to a #include in C++ or a import package name in Java?

2. my code to run down the fetched results and store them into an array doesn't seem to be working the way i want it to. if i print out the results from the mysql returned fetch the data is fine. however the array seems to print out "Array[elementnumber]" instead of the value. Code:

Mysql Results <-- Previous Row, And Next Row, Showing Next Ok
I query a database and return a result on the column "reference".
There might be 7 listings. Each row is displayed in a table, with
links through to a detail page. I am working on having a "previous"
record and a "next" record link on the detail page.

This code below works, for the "next" record, by searching the values
in the array $myarray for the variable $ref. It then returns the key
value and the key value, as a number, is deducted by one and plus'd by
one to give me the previous row and the next row.

Returning the value of the next row works a treat, but for some reason
the previous row, deduct 1, while it echoes a value to screen, doesn't
work.

This is the code:

(query database and then:)
//$ref is a variable carried into the page

$num_rows = mysql_num_rows($result2);

$i=0;
while ($i < $num_rows) {
$next=mysql_result($result2,$i,"reference");

++$i;

$myarray = array("$i"=>"$next");
foreach($myarray as $key=>$value) {
if ($value == "$ref"){

$b=($key);
$c=($key+1);
$a=($key-1);

}}

if ($a == "0") {
echo "No Preceding Record";

} else {
if ($key == "$a") echo "<a
href=../detail/index.php?name=$value>Previous Record</a>";
}

if ($key == "$b") echo "This is the current Record";

if ($num_rows < $c) {
echo "Records End";
} else {
if ($key == "$c") echo "<a href=../detail/index.php?name=$value>Next
Record</a>";
}
}

} else {

JOIN Table Results W/MySQL
I have a query with a simple JOIN.

For example:

SELECT * FROM Companies
INNER JOIN CompanyTypes ON Companies.CompanyID = CompanyTypes.CompanyID

The query returns all fields from both Companies AND CompanyTypes. I would
like all fields from Companies only.

Counting Results From A Mysql Database
I want to know how many results throw a query in order to know if they have reached a limit.I.E.

$sql = mysql_query ("select * from classifieds where user='$username'");
while ($row = mysql_fetch_array($sql)){}

I want to check lets say that only 10 classifieds per  'username' can be created. If ten or more classifieds are in the database for that 'username' then show error message.

Alternating Row Colors With MySQL Results
Is there any way i could make a image for the alternating row colors instead of a strait bold color, for instance:

$color1 = "#CCFFCC";
$color2 = "#BFD8BC";

Would there be anyway to link an image in there? I have tried it this way:

$color1 = "<img src='..style1.jpg'>";
$color2 = "<img src='..style2.jpg'>";

How Can I Arrange Results Alphabetically In MySQL
I am using this code to retrieve names from the databse

$q = "SELECT * FROM
names
ORDER BY date DESC
LIMIT 5;";

How can i arrange names alphabetically

MySQL Query Not Returning All Results
I have a user table in the DB, for example:

+---------+----------------+-------------------+
|     uid     |     username     |       firstname       |
|----------+----------------+------------------+
|       1     |       cobby        |       Andrew          |
|       2     |       admini        |     test-account    |
+---------+-----------------+------------------+

If, in phpMyAdmin, I query:
   SELECT `username` FROM `users`;

It will return results correctly (cobby and admini, respectively).
But if I make a simple PHP script, such as:

<?php

mysql_connect('localhost', 'cobby', 'dbpass');
mysql_select_db('testdb');

$query  = mysql_query('SELECT `username` FROM `users`');
print_r(mysql_fetch_assoc($query));

?>

It only returns:
Array ( [username] => admini )

Why does it only return the second result? I have a feeling I'm doing something really silly, but I looked over this test script and its got me stumped.

Mysql Dump Results In Blankfile
I am trying to get my script working to back up a mysql database. Everything works fine only the output results in a blank file. Code:

Merging Results From Two Mysql Databases
I have made two websites on seperate servers both with a database of their own.
They are identical but with different results in the databases. I want to make a page on one of the sites that will merge the information from both databases.

e.g.
site 1 has a list in date order
site 2 has a list in date order

I want site 1 to have the list merged together to form one big list but still be in date order. Also I want to be able to have the results of site 2 in a different font colour to site 1.

i.e.
Site 1 results

10th jan - something
15th jan - something else
19th jan - more

Site 2 results

11th jan - even more
13th jan - some more
16th jan - more stuff

Merged page

10th jan - something
11th jan - even more
13th jan - some more
15th jan - something else
16th jan - more stuff
19th jan - more

can anyone help with this?

Using Arrays With Mysql Query Results
im currently in the process of making a piece of code that will firstly take all the data from a table in mysql and then assign it to an array. This data will consist of several different rows of data each with different infomation for each field in the table.

i want to be able to use an array in a loop so that depending on which number of the loop is on will change the infomation that is assigned to a certain array. Code:

Displaying MySQL Results As URLs
I'm attempting to turn the results of a query into urls, yet I keep getting this error, "Parse error: syntax error, unexpected '<' in staff.php on line 190." I've coded and recoded this, yet for some reason I cannot get this error to go away. I know it's probably simple, but what am I missing? Code:


Copyright © 2005-08 www.BigResource.com, All rights reserved