php insert sql issue while loop / foreach - php

The following code from my data between tables, but for some reason only one value is inserted to database
<?php
DEFINE("DB_SERVER", "localhost"); //LOCALHOST
DEFINE("DB_USER", "user");
DEFINE("DB_PASS", "pass");
DEFINE("DB_NAME", "table");
$connect = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die("connect issue". ' ' . mysql_error());
mysql_query('SET NAMES utf8');
$db = mysql_select_db(DB_NAME,$connect) or die("connect issue". ' ' . mysql_error());
mysql_query('SET NAMES utf8');
if (!$db){
echo "connect issue";
}
$sql = "SELECT id, column2 FROM tablea";
$result = mysql_query($sql) or die(mysql_error());
$set = date("Y-m-d H:i:s", time());
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$list = $row['column2'];
echo "user_id: $id";
echo "<br/><br/>";
$makes = explode (";", $row['column2']);
$i = 0;
foreach ($makes as $make) {
$sql2 = "SELECT url FROM tableb WHERE id = '$make'";
$result2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_array($result2)) {
echo $row2[0];
echo $row2[1];
echo $row2[2];
echo $row2[3];
echo $row2[4];
echo $row2[5];
echo $row2[6];
echo $row2[7];
$m1 = $row2[0];
$m2 = $row2[1];
$m3 = $row2[2];
$m4 = $row2[3];
$m5 = $row2[4];
$m6 = $row2[5];
$m7 = $row2[6];
$m8 = $row2[7];
echo "<br/>";
}
if (++$i == 8) break;
}
$sql3 = "INSERT INTO tablec (partner_id, make1, make2, make3, make4, make5, make6, make7, make8, saved) VALUES ('$id', '$m1', '$m2', '$m3', '$m4', '$m5', '$m6', '$m7', '$m8', '$set')";
$result3 = mysql_query($sql3) or die(mysql_error());
var_dump($sql3);
var_dump($result3);
if($result3) {
echo "done";
}
else {
echo "fail";
}
echo "<pre>";
var_dump($row);
echo "</pre>";
echo "<hr/>";
}
?>
var_dump sql:
string(186) "INSERT INTO tablec (partner_id, make1, make2, make3, make4, make5, make6, make7, make8, saved) VALUES ('75', 'opel', '', '', '', '', '', '', '', '2014-02-06 18:20:14')"
it works fine, but the only 1 variable inserted the table "$row2[0];"
whats the problem? thank you for your help!

You only ever fetch one field from your tableb:
$sql2 = "SELECT url FROM tableb WHERE id = '$make'";
^^^^---- here
If you want more fields, you need to specify them:
SELECT url, field1, field2, etc...

Related

SQL Update unable to work

I have a SQL update below and there's no error but it doesn't reflect any changes I have made to the database. I also paste here the 2 previous php which will lead to the update php. SQL query for UPDATE shows no error too.
Please let me know if i miss out anything.
Appreciate it very much.
table 1: nmc_cd
table 2: nmc_category
table 3: nmc_publisher
This is the php to display the list of CDs and the key here is the CDID which once selected will be sent to the next php.
include 'database_conn.php'; //make db connection
$sql = "SELECT nmc_cd.CDID, nmc_cd.CDTitle, nmc_cd.CDYear, nmc_cd.CDPrice, nmc_category.catDesc, nmc_cd.catID
FROM nmc_cd
LEFT JOIN nmc_category ON nmc_cd.catID=nmc_category.catID";
$queryresult = mysqli_query($conn, $sql)
or die (mysqli_error($conn));
echo'<table cellpadding="0" cellspacing="0" class="db-table" table align="center">';
echo"<tr><th>Music Title</th><th>Year</th><th>Price</th><th>Category</th></tr>";
while($row = mysqli_fetch_assoc($queryresult)) {
$iCDID = $row['CDID'];
$CDTitle = $row['CDTitle'];
$CDYear = $row['CDYear'];
$CDPrice = $row['CDPrice'];
$CDCat = $row['catDesc'];
$CDCatID = $row['catID'];
echo "<tr><td>";
echo "$CDTitle";
echo "</td><td>";
echo "<div align='center'>$CDYear</div>\n";
echo "</td><td>";
echo "<div align='center'>$CDPrice </div>\n";
echo "</td><td>";
echo "<div align='center'>$CDCat</div>\n";
echo "</TD></tr>";
}
echo "</table>";
mysqli_free_result($queryresult);
mysqli_close($conn);
?>
In this php it display the selected CD and editing is permitted
<?php
header('Content-type: text/html; charset=iso-8859-1'); //set the charset parameter
include 'database_conn.php'; //make db connection
$code = $_GET['itemCode']; //collect form data from user input
$sql = "SELECT nmc_cd.CDID, nmc_cd.CDTitle, nmc_cd.CDYear, nmc_cd.CDPrice, nmc_cd.catID, nmc_cd.pubID, nmc_category.catDesc, nmc_publisher.pubID, nmc_publisher.pubName
FROM nmc_cd
JOIN nmc_category on (nmc_cd.catID = nmc_category.catID)
JOIN nmc_publisher on (nmc_cd.pubID = nmc_publisher.pubID)
WHERE nmc_cd.CDID = '$code'"; //link user input with primary key
$queryresult = mysqli_query($conn, $sql)
or die (mysqli_error($conn));
$row = mysqli_fetch_assoc($queryresult); //function fetches result row
$iCDID = $row['CDID'];
$CDTitle = $row['CDTitle'];
$CDYear = $row['CDYear'];
$CDPrice = $row['CDPrice'];
$CDCat = $row['catDesc'];
$CDPub = $row['pubName'];
$CDpubID = $row['pubID'];
<form method="get" action="UpdateCD.php">
<div align="center">
<div>Title <input type = "text" name = "CDTitle" value = "<?php echo $CDTitle; ?>" /></div></br>
<div>Year <input type = "text" name = "CDYear" value = "<?php echo $CDYear; ?>" /></div></br>
<div>Price <input type = "text" name = "CDPrice" value = "<?php echo $CDPrice; ?>" /></div></br>
Category
<select name="CDCat">
<option value= " ">
<?php
include 'database_conn.php'; //make db connection
if (! ( is_object($conn ) && ( get_class( $conn ) == 'mysqli' ))) {
die("DB connection failure.");
}
$rsCDCat = mysqli_query($conn, "SELECT nmc_category.catDesc FROM nmc_category");
if ( !$rsCDCat ) {
die("No result from DB query."); //probably invalid SQL, table error
}
if ( $rsCDCat->num_rows < 1 ) {
die("No rows returned from DB query."); //query runs but nothing is found in DB to match
}
while($Catcatresult = mysqli_fetch_array($rsCDCat)){
$optioncat = "<option value='{$Catcatresult[0]}'";
if($Catcatresult[0] == $CDCat){
$optioncat .= " selected='selected'";
}
$optioncat .= ">{$Catcatresult[0]}</option>";
echo $optioncat;
}
?>
</select></br></br>
Publisher
<select name="CDPub">
<option value= " ">
<?php
include 'database_conn.php'; //make db connection
if (! ( is_object($conn ) && ( get_class( $conn ) == 'mysqli' ))) {
die("DB connection failure.");
}
$rsCDpub = mysqli_query($conn, "SELECT nmc_publisher.pubName FROM nmc_publisher");
if ( !$rsCDpub ) {
die("No result from DB query."); //probably invalid SQL, table error
}
if ( $rsCDpub->num_rows < 1 ) {
die("No rows returned from DB query."); //query runs but nothing is found in DB to match
}
while($Catpubresult = mysqli_fetch_array($rsCDpub)){
$option = "<option value='{$Catpubresult[0]}'";
if($Catpubresult[0] == $CDPub){
$option .= " selected='selected'";
}
$option .= ">{$Catpubresult[0]}</option>";
echo $option;
}
?></select></br></br>
<div><input type="submit" value="Update"></div>
</form>
In this update php, it gets the values from the previous php and update the table.
<?php
header('Content-type: text/html; charset=iso-8859-1');
include 'database_conn.php'; // make db connection
$pCDTitle = filter_has_var(INPUT_GET, 'CDTitle') ? $_GET['CDTitle']: null; // store all parameter in variable
$pCDPubName = filter_has_var(INPUT_GET, 'CDPub') ? $_GET['CDPub']: null;
$pCDYear = filter_has_var(INPUT_GET, 'CDYear') ? $_GET['CDYear']: null;
$pCDCategory = filter_has_var(INPUT_GET, 'CDCat') ? $_GET['CDCat']: null;
$pCDPrice = filter_has_var(INPUT_GET, 'CDPrice') ? $_GET['CDPrice']: null;
$pCDID = filter_has_var(INPUT_GET, 'CDID') ? $_GET['CDID']: null;
$pCDPubID = filter_has_var(INPUT_GET, 'pubID') ? $_GET['pubID']: null;
$sql = "UPDATE nmc_cd "
. "SET nmc_cd.CDTitle='$pCDTitle',nmc_cd.CDYear='$pCDYear',nmc_cd.CDPrice='$pCDPrice',nmc_cd.catID='$pCDCategory',nmc_cd.pubID='$pCDPubName'"
. "WHERE nmc_cd.CDID='$pCDID'";
if ($conn->query($sql) === TRUE) {
echo "<b><font face='verdana' font sise='3' color='red'>Record updated successfully</font></b>";
$sql = "SELECT * FROM nmc_cd "
. "JOIN nmc_category ON (nmc_cd.catID = nmc_category.catID)"
. "JOIN nmc_publisher ON (nmc_cd.pubID = nmc_publisher.pubID)"
. "WHERE nmc_cd.CDID = '$pCDID'"; //Query Database
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$CDID = $row['CDID'];
$CDTitle = $row['CDTitle'];
$CDYear = $row['CDYear'];
$CDPrice = $row['CDPrice'];
$catDesc = $row['CDCat'];
$pubName = $row['CDPub'];
$location = $row['location'];
echo "<table>";
echo "<tr><th>Title</th><th>Year</th><th>Price</th><th>Catergory</th><th>Publisher</th><th>Location</th></tr>";
echo "<tr><td>".$row["CDTitle"]."</td><td> ".$row["CDYear"]."</td><td> ".$row["CDPrice"]."</td><td> ".$row["CatCD"]."</td><td> ".$row["CatPub"]."</td><td> ".$row["location"]."</td></tr>"; //Display Data Table Data
echo "</table";
mysqli_close($conn);
} else {
echo "<b><font face='verdana' color='red'>Error updating record!!!</font></b>" . $conn->error;
}
?>
$sql = "UPDATE nmc_cd "
. "SET nmc_cd.CDTitle='$pCDTitle',nmc_cd.CDYear='$pCDYear',nmc_cd.CDPrice='$pCDPrice',nmc_cd.catID='$pCDCategory',nmc_cd.pubID='$pCDPubName'"
. "WHERE nmc_cd.CDID='$pCDID'";
Should be:
$sql = "UPDATE nmc_cd "
. "SET nmc_cd.CDTitle='$pCDTitle',nmc_cd.CDYear='$pCDYear',nmc_cd.CDPrice='$pCDPrice',nmc_cd.catID='$pCDCategory',nmc_cd.pubID='$pCDPubName' "
. "WHERE nmc_cd.CDID='$pCDID'";
i.e. You are missing a SPACE character before the WHERE Clause.
(Note the end of 2nd line)
Look in the MySql logs. Make sure bad queries are logged to the file and you should see the syntax error. The other thing you can do is dump out the $sql string and run that in MySQL. See if it runs successfully or not.

mysql_num_rows returning zero

<?php
session_start();
$con=mysql_connect("localhost","root","samy");
mysql_select_db("project");
if($con)
{
echo "Connected Successfully ";
}
else
{
echo "Error" . $sql . "<br>" . mysql_connect_error();
}
$name=$_SESSION['name'];
echo $name;
$sql1 = mysql_query("select cust_id from registered_user where name ='.$name.' ");
$r = mysql_num_rows($sql1);
echo $r;
$row1 = mysql_fetch_array($sql1);
$cid = $row1['cust_id'];
echo $cid;
?>
Since num_rows is returning zero therefore $cid is also not printing.
Don't know what's the error;
You should remove the dot(.).
$sql1 = mysql_query("select cust_id from registered_user where name ='$name'");
^ ^
Also suggest to add error reporting like this
$sql1 = mysql_query("select cust_id from registered_user where name ='$name'")
or die(mysql_error());

trouble getting mysql array to duplicate code to execute per each row

im trying to query the database and apply code to each result and insert a record per result to a databse. This code works but only does it for the last result row. Ive tried foreach but cannot get it to work most likley because i dont understand how it should work. Please help and thank you
<?php
if(isset($_POST['bill1'])){
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
require ('dbconnect.php');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
$invoicedate= $_POST['invoicedate'];
$invoiceduedate= $_POST['invoiceduedate'];
echo 'post is good';
require ('dbconnect.php');
echo 'db connect';
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM customer WHERE terms = 1");
while($row = mysqli_fetch_array($result)) {
echo 'array good';
$item = $row['inputItem'];
$itemdescription = $row['description1'];
$itemprice = $row['itemprice1'];
$id = $row['id'];
$paidstatus = 5;
$totaldue = $itemprice;
require ('dbconnect.php');
echo $item;
echo $itemdescription;
echo $itemprice;
echo $id;
echo $paidstatus;
echo $invoicedate;
echo $invoiceduedate;
$resulttt = mysqli_query($con,"SELECT invoice_number FROM invoices ORDER BY invoice_number DESC LIMIT 1");
while($row = mysqli_fetch_array($resulttt)){
$addone = "1";
$invoicenewnumber = $addone + $row [invoice_number];
}
echo $invoicenewnumber;
//echo $row [invoice_number];
$invoice_number = $invoicenewnumber;
require ('dbconnect.php');
$put = mysqli_query($con,"INSERT INTO invoices (item, description, item_total, id, paidstatus, duedate, invoicedate, invoice_number, total_due)VALUES('$item', '$itemdescription', '$itemprice', '$id', '$paidstatus', '$invoiceduedate', '$invoicedate', '$invoice_number', '$totaldue')");
if (!mysqli_query($con,$put))
die('Error: ' . mysqli_error($con));
echo 'succsess';
}
?>
<?php
$resulttt = mysqli_query($con,"SELECT invoice_number FROM invoices ORDER BY invoice_number DESC LIMIT 1");
while($row = mysqli_fetch_array($resulttt))
{
$addone = "1";
$invoicenewnumber = $addone + $row [invoice_number];
$invoice_number = $invoicenewnumber;
$put = mysqli_query($con,"INSERT INTO invoices (item, description, item_total, id, paidstatus, duedate, invoicedate, invoice_number, total_due)VALUES('$item', '$itemdescription', '$itemprice', '$id', '$paidstatus', '$invoiceduedate', '$invoicedate', '$invoice_number', '$totaldue')");
}
?>
Try this insert the second query in the while loop.
And read the comment where ever you find them in the Answer
Note:You need the connection only one time in whole page.
<?php
if(isset($_POST['bill1']))
{mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
require ('dbconnect.php'); You need the connection only once
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}}
$invoicedate= $_POST['invoicedate'];
$invoiceduedate= $_POST['invoiceduedate'];
echo 'post is good';
//require ('dbconnect.php'); Dont need here
// echo 'db connect'; Dont need here
//if (mysqli_connect_errno()) {
// echo "Failed to connect to MySQL: " . mysqli_connect_error();
// }
$result = mysqli_query($con,"SELECT * FROM customer WHERE terms = 1");
while($row = mysqli_fetch_array($result)) {
echo 'array good';
$item = $row['inputItem'];
$itemdescription = $row['description1'];
$itemprice = $row['itemprice1'];
$id = $row['id'];
$paidstatus = 5;
$totaldue = $itemprice;
require ('dbconnect.php');
echo $item;
echo $itemdescription;
echo $itemprice;
echo $id;
echo $paidstatus;
echo $invoicedate;
echo $invoiceduedate;
$resulttt = mysqli_query($con,"SELECT invoice_number FROM invoices ORDER BY invoice_number DESC LIMIT 1");
while($row = mysqli_fetch_array($resulttt)){
$addone=$addone+1;//If you want to add one to the invoice number you have to use this.
$invoicenewnumber = $addone + $row ['invoice_number'];//}//remove the closing while loop from here,
//Added quete to the Invoice_number
echo $invoicenewnumber;
//echo $row [invoice_number];
$invoice_number = $invoicenewnumber;
require ('dbconnect.php');
$put = mysqli_query($con,"INSERT INTO invoices (item, description, item_total, id, paidstatus, duedate, invoicedate, invoice_number, total_due)VALUES('$item', '$itemdescription', '$itemprice', '$id', '$paidstatus', '$invoiceduedate', '$invoicedate', '$invoice_number', '$totaldue')");
}
if (!mysqli_query($con,$put))
die('Error: ' . mysqli_error($con));
echo 'succsess';
}
?>

mysqli_real_escape does not look correct while inserting the value

I am inserting value into table with mysqli_real_escape in this way:
$noun = mysqli_real_escape_string($con,$noun);
$adjective = mysqli_real_escape_string($con,$adjective );
//$update1 = "UPDATE review_words SET adjective = CONCAT(adjective, ',', '$adjective'), noun = CONCAT(noun, ',', '$noun') ";
$update1 = "UPDATE review_words SET adjective = CONCAT(IFNULL(adjective, ''), ',', '$adjective'), noun = CONCAT(IFNULL(noun, ''), ',', '$noun') "; if (!mysqli_query($con,$update1))
{
// die('Error: ' . mysqli_error($con));
// echo "error";
}
I dont get error here. But when I select the data from that for processing it gives error :
restaurant : 16 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where word = 'restaurant'' at line 1
Here is where I get error:
$select1 = mysqli_query($con,"SELECT * from review_words");
while ($row = #mysqli_fetch_array($select1))
{
$noun = $row['noun'];
echo "Nons are : $noun <br><br>";
$adjective = $row['adjective'];
echo "Nons are : $adjective <br>";
}
I have used mysql_real_escape, even this is showing this sort of error. Do i need to use anything else?
Here is full code:
foreach($response->businesses as $business)
{
echo "<img border=0 src='".$business->image_url."'><br/>";
echo "Local provider : ".$business->name."<br/>";
$rtext = $business->snippet_text;
echo "Review : ".$business->snippet_text."<br/>";
if( $item = 'Italian_restaurants' or $item = 'Mexican_restaurants')
{
$keywords = MakeExternalReq($business->snippet_text);
echo "<strong>Important keyword : </strong>".$keywords."<br/>";
$tagger = new PosTagger('lexicon.txt');
$tags = $tagger->tag($rtext);
$noun = printTagN($tags);
$adjective = printTagA($tags);
$noun = implode(", ",$noun);
$adjective = implode(", ",$adjective);
echo "Noun : $noun <br>";
echo "Adjectives : $adjective <br>";
//var_dump($var);
}
echo "Rate : ".$business->rating."<br/>";
echo "Phone : ".$business->phone."<br/>";
echo "Address : ".$business->location->display_address[0]."<br/>";
echo "Category : ".$business->categories[0][0];
echo "<hr>";
$brand = 'Yelp';
$local_provider = $business->name;
$review = $business->snippet_text;
$id = md5($review);
$rate = $business->rating;
$image = $business->image_url;
$phone = $business->phone;
$address = $business->location->display_address[0];
$category = $business->categories[0][0];
$con = mysqli_connect('127.0.0.1', 'root', 'root', 'root');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return;
}
$insertQuery1 = "INSERT INTO review_details(`id`,`brand`,`local_provider`,`review`,`rate`,`important_words`,`adjective`,`noun`,`image`,`phone`,`address`,`category`) VALUES ('".$id."','".$brand."','".$local_provider."','".$text."','".$rate."','".$keywords."','".$adjective."','".$noun."','".$image."','".$phone."','".$address."','".$category."')";
if (!mysqli_query($con,$insertQuery1))
{
// die('Error: ' . mysqli_error($con));
// echo "error";
}
$noun = mysqli_real_escape_string($con,$noun);
$adjective = mysqli_real_escape_string($con,$adjective );
//$update1 = "UPDATE review_words SET adjective = CONCAT(adjective, ',', '$adjective'), noun = CONCAT(noun, ',', '$noun') ";
$update1 = "UPDATE review_words SET adjective = CONCAT(IFNULL(adjective, ''), ',', '$adjective'), noun = CONCAT(IFNULL(noun, ''), ',', '$noun') "; if (!mysqli_query($con,$update1))
{
// die('Error: ' . mysqli_error($con));
// echo "error";
}
}
}
function get_word_count()
{
echo "<br> Entered into word count <br>";
$con = mysqli_connect('127.0.0.1', 'root', 'root', 'root');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return;
}
$select1 = mysqli_query($con,"SELECT * from review_words");
while ($row = #mysqli_fetch_array($select1))
{
$noun = $row['noun'];
echo "Nons are : $noun <br><br>";
$adjective = $row['adjective'];
echo "Nons are : $adjective <br>";
}
$noun_count = array_count_values(str_word_count($noun, 1));
$adjective_count = array_count_values(str_word_count($adjective, 1));
//echo $noun_count;
arsort($noun_count);
//print_r($noun_count);
arsort($adjective_count);
//print_r($adjective_count);
//echo $noun_count;
foreach($noun_count as $key=>$value)
{
echo "$key : $value ";
$insertQuery2 = "INSERT INTO review_word_count (`word`,`count`,`type`) VALUES ('".$key."','".$value."','noun') ON DUPLICATE KEY UPDATE count = '".$value."' where word = '".$key."'";
if (!mysqli_query($con,$insertQuery2))
{
die('Error: ' . mysqli_error($con));
// echo "error";
}
}
foreach($adjective_count as $key=>$value)
{
$insertQuery3 = "INSERT INTO review_word_count (`word`,`count`,`type`) VALUES ('".$key."','".$value."','adjective') ON DUPLICATE KEY UPDATE count = '".$value."' where word = '".$key."'";
if (!mysqli_query($con,$insertQuery3))
{
die('Error: ' . mysqli_error($con));
// echo "error";
}
}
echo "<br> End into word count <br>";
}
remove this
where word = '".$key."'
from your queries of insert.
you dont have to make where clause here while you inserted that key, so the query will look for that inserted key automatically.
also
echo "$key : $value ";
should be
echo $key. " : ".$value ;

Getting two scripts to work together

I'm trying to use the following scripts so that the result of the first one determines the output of the second one.
<?
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbname', $db);
$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
if ( $row['pool'] % 2 )
{
echo "<h4>Result 1</h4>";
echo "<br />";
}
else
{
echo "<h4>Result 2</h4>";
echo "<br />";
}
?>
<?php
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbnamesameasother', $db);
$query2 = "SELECT * FROM comments";
$result2 = mysql_query($query2);
while ($row2 = mysql_fetch_assoc($result2))
if ( $row2['commentid'] % 2 ==0 )
{
echo $row2['name'];
echo "<br />";
}
else
{
echo $row2['name'];
}
?>
So basically if the first script chooses Result 1 I only want to echo the names which associate with that result. The names are associated by commentid where an odd commentid would be result 2 and an even commentid would be result 1. Is there any way to do this without using a union statement?
It recommend that you to create a function and then call it.something like this :
<?php
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbname', $db);
$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
if ( $row['pool'] % 2 )
{
echo "<h4>Result 1</h4>";
$names = get_names(1);
foreach ($names as $name) {
echo $name . "<br/>";
}
}
else
{
echo "<h4>Result 2</h4>";
$names = get_names(0);
foreach ($names as $name) {
echo $name . "<br/>";
}
}
Function get_names($pool_result)
{
$name_array = array();
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbnamesameasother', $db);
$query = "SELECT * FROM comments WHERE commentid % 2 = $pool_result";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
array_push($name_array , $row['name']);
}
return $name_array;
}
?>
}

Categories