Question:

How do I pass variables from mysql_fetch_array to another PHP program?

by  |  earlier

0 LIKES UnLike

I'm not a programmer, but I do understand a little bit of PHP. My problem is that I'm taking two program snippets that work well, but when I try to pass variables from the first one to the second, something breaks and nothing comes out.

The first part of the program selects a random record from a MYSQL database.

// Create the connection and select the DB

$link = mysql_connect("localhost","root","");

if ($link) {

mysql_selectdb("test",$link);

// Select records from the DB

$query = "SELECT * FROM names ORDER BY Rand() LIMIT 1";

$result = mysql_query($query);

// Display records from the table

echo "<table border='1'>";

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

The second part of the program uses XML RPC to post remotely to a WordPress blog.

$wp = new eWordpressApi("http://www.mysite.com/xml...

$title = "Testing";

$text = "Lorem Ipsum";

$category = array(Pants,Stuff); // Assuming you have categories with IDs: 1,2

$postID = $wp->newPost($title,$text,$category);

if(is_array($postID)) echo "Die, d**n bug!";

I've been trying to pass the results from mysql_fetch_array to the second program so that $title = row[0] and $text = row[1], row[2], but when I execute the script all it does it print the second program as text instead of passing the variable and executing the program.

I've been beating my head against the wall for a week on this problem. What am I doing wrong.

The complete program:

<?php

// Create the connection and select the DB

$link = mysql_connect("localhost","root","");

if ($link) {

mysql_selectdb("test",$link);

// Select records from the DB

$query = "SELECT * FROM names ORDER BY Rand() LIMIT 1";

$result = mysql_query($query);

// Display records from the table

echo "<table border='1'>";

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

$wp = new eWordpressApi("http://www.mysite.com/xml...

$title = "Testing";

$text = "Lorem Ipsum";

$category = array(Pants,Stuff); // Assuming you have categories with IDs: 1,2

$postID = $wp->newPost($title,$text,$category);

if(is_array($postID)) echo "Die, d**n bug!";

}

?>

 Tags:

   Report

1 ANSWERS


  1. use the include_once( ) function in PHP so you could have the variables from the previous page in the second page.

    Example, your page1.php queries to the database and stores it mysql_fetch_array[] variable, to be able to use mysql_fetch_array[] in the next page(page2.php), use

    include_once(page2.php);

    hope this helps

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.