-1

Is there a way i can make the following code a single variable or function

$result = mysql_query("SELECT * FROM a0pg3_mikes WHERE pkey=".$pkeyurl);
while ($custrow = mysql_fetch_array($result)) {
}
?>

I want to put this in my functions.php file and call the whole lot with something like...

<?php echo $custrow{'custname'};?>

I have been finding that the echo must be between the "while {}" If i use the echo on a seperate php like the above, it doesn't work. I am very new to this level of coding and beginning to push myself.

Many Thanks.

Mike Wood
  • 154
  • 8
  • 2
    **Warning:** you're using [a **deprecated** database API](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) yourself from. – Marcel Korpel Dec 06 '13 at 23:31
  • So, do you want to fetch a single value? Or loop over a list of results? Have you tried writing a function for it? (And the second example isn't the valid function invocation syntax then.) – mario Dec 06 '13 at 23:33
  • I would really have liked to set each item in the $custrow array as a variable for individual use later.... :S – Mike Wood Dec 06 '13 at 23:46
  • @Marcel Korpel I am VERY new and struggling along. could you give me an example of the correct way to achieve the query? What i have is the best lord google found - That worked! :S – Mike Wood Dec 06 '13 at 23:49
  • Just follow the links in my comment, especially http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Marcel Korpel Dec 06 '13 at 23:50

1 Answers1

1

Like this?

<?php
$result = mysql_query("SELECT * FROM a0pg3_mikes WHERE pkey=" . $pkeyurl );
while ($custrow = mysql_fetch_array($result)) {
    echo $custRow['custName'];
}
?>
Dai
  • 141,631
  • 28
  • 261
  • 374