Jump to content

Recommended Posts

I have the following code that works fine:

 

<?php
include 'dbconnect.php';
$query = "SELECT * FROM users"; 
$result = mysql_query($query) or die(mysql_error());
$stng = mysql_fetch_array($result) or die(mysql_error());
echo $stng['name'];
?>

 

This will output a name just fine. But, if I put the query into a function like this:

 

<?php
include 'dbconnect.php';
function pb_stng_selectall()
{
$query = "SELECT * FROM users"; 
$result = mysql_query($query) or die(mysql_error());
$stng = mysql_fetch_array($result) or die(mysql_error());
}
?>

<?php 
pb_stng_selectall();
echo $stng['name'];
?>

 

I will get a "Undefined variable" error instead of the name. Why is this?

Try this:

 

<?php
include 'dbconnect.php';
function pb_stng_selectall()
{
$query = "SELECT * FROM users"; 
$result = mysql_query($query) or die(mysql_error());
return = mysql_fetch_array($result) or die(mysql_error());
}
?>

<?php 

echo pb_stng_selectall();
?>

 

Variables within functions are only accessible via the scope of the function.  Values must be returned to allow external access.

Correction to p2grace's code. The function will return an array and you cannot echo an array:

 

<?php
include 'dbconnect.php';
function pb_stng_selectall()
{
  $query = "SELECT * FROM users"; 
  $result = mysql_query($query) or die(mysql_error());
  $stng = mysql_fetch_array($result) or die(mysql_error());
  return $stng;
}
?>

<?php 

$user = pb_stng_selectall();
echo $user['name'];
?>

However, the functionality makes no sense. Why are you querying the entire table but only using the first record? You're not even using a sort to ensure you get a specific record. What are you wanting the function to return? It would make more sense to me if you were passing an ID to the function to get a specific record or at least returning a multidimensional array of all the records returned. A lot of inefficiency in the the above format.

<?php

include 'dbconnect.php';

function pb_stng_selectall($users)

{

$query = "SELECT * FROM users='$users'";

$result = mysql_query($query) or die(mysql_error());

$stng = mysql_fetch_array($result) or die(mysql_error());

 

    echo = $stng['users'];

}

?>

 

<?php

pb_stng_selectall();

 

?>

I am writing a bunch of crud functions into one page that I will need to use across the site. I want to put them all in one page so I will not be writing the same queries over and over. That random name I'm trying to echo is just for testing purposes.

 

I tried using this:

$user = pb_stng_selectall();
echo $user['name'];

 

and I also put a "hello world" inside the function but I just get a blank page with no errors. (I also turned error reporting on)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.