Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save the result of a SQL query to a PHP variable

Status
Not open for further replies.

conjokes

IS-IT--Management
Joined
Jan 24, 2004
Messages
4
Location
GB
<br></bog>
</center><br><br><hr>

<?PHP
$conn = pg_Connect (&quot;port=5432 dbname=jack
user=ma103tw&quot;);
if (!$conn) {
exit;
}
$result1 = pg_exec ($conn, &quot;select MAX(offender_no) from offenders;&quot;);

$result2 = pg_exec ($conn, &quot;select * from civil_fines where offender = $result;&quot;);tried to compare offender to the above query result result1, this does not work, i get the error message below:

Warning: pg_exec() query failed: ERROR: parser: parse error at or near &quot;id&quot; in public_html/project/enterFine2.php on line 12

if (!$result1) {exit;}

if (!$result2) {exit;}

echo &quot;<b>The client $client has beem added to the database<ol>&quot;;

pg_Close($conn);
echo &quot;</ul></b><hr>&quot;;
?>

<small><i>

</i></small>
</body></html>

please can anyone help, thanks



 
where is $result coming from ?? you have a $result1 but still you need to get the value

$row = mysql_fetch_array($result1);
$result=$row[&quot;offender_no&quot;];

further on it's easy to use names when using functions
select MAX(offender_no) AS offendernr from offenders;

and the result2 query should look like this

&quot;select * from civil_fines where offender = '$result';&quot;


 
Thanks 4 that, here is a edited version of the code, which does the same thing, I have included the suggestions, however I get the error message &quot;Parse error: parse error in public_html/my_folder/fines.php on line 10/fines.php on line 10:...I am using postgres as supposed to mysql if that makes any differance

<?php
pg_connect(&quot;dbname=sean user=ma102so&quot;) or die(&quot;Couldn't Connect: &quot;.pg_last_error());

$query = &quot;select MAX(offender_no) from offenders;&quot;;



$row = fetch_array($query);

$result = $row&quot;offender_no&quot;;


$query2 = &quot;select * from civil_fines where offender_no = '$result';&quot;;


$query = pg_query($query);

$query2 = pg_query($query2);

$row = pg_query($row);

$result = pg_query($result);

if($query)
echo &quot;inserted successfully!&quot;;
else{
echo &quot;There was an error! &quot;.pg_last_error();
}


?>
 
Code:
 $result=$row[&quot;offender_no&quot;];

oops forgot the code tags
 
thanks anyway m8, u been a great help, but it it still done work, oh well

<?php
pg_connect(&quot;dbname=offenders user=jack&quot;) or die(&quot;Couldn't Connect: &quot;.pg_last_error());

$query = &quot;select MAX(offender_no) from offenders;&quot;;



$row = pg_fetch_array($query);

$result=$row[&quot;offender_no&quot;];


$query2 = &quot;select * from civil_fines where offender_no = '$result';&quot;;


$query = pg_query($query);

$query2 = pg_query($query2);



if($query)
echo &quot;inserted successfully!&quot;;
else{
echo &quot;There was an error! &quot;.pg_last_error();
}


?>

Warning: pg_fetch_array(): supplied argument is not a valid PostgreSQL result resource public_html/test/traning.php on line 8

Warning: pg_query() query failed: ERROR: Attribute 'offender_no' not found in public_html/test/traning.php on line 18
inserted successfully!
 
You can on use pg_fetch_array on the result from a query. You are using it on the query string itself there.
Code:
<?php
pg_connect(&quot;dbname=offenders user=jack&quot;) or die(&quot;Couldn't Connect: &quot;.pg_last_error());

$query = &quot;select MAX(offender_no) from offenders;&quot;;

$dbResult = gr_query($query) or die(&quot;Query 1 failed: &quot; . pg_last_error())

$row = pg_fetch_array($dbResult);

$result=$row[&quot;offender_no&quot;];


$query2 = &quot;select * from civil_fines where offender_no = '$result';&quot;;

$dbResult2 = pg_query($query2);

if($dbResult2)
  echo &quot;inserted successfully!&quot;;
else{
  echo &quot;There was an error! &quot;.pg_last_error();
}

?>
 
tied that m8, its crashes with the results below:

Parse error: parse error in /mnt/moya/home/ugrad/ma102sopublic_html/test/traning.phpon line 8
 
Ok, I noticed 2 typos I made (I really should proof read this stuff before submitting).
On the line:
Code:
$dbResult = gr_query($query) or die(&quot;Query 1 failed: &quot; . pg_last_error())
replace with:
Code:
$dbResult = pg_query($query) or die(&quot;Query 1 failed: &quot; . pg_last_error());
I misspelt pg_query and I forgot the semi-colon at the end of the line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top