2

I want to fetch multiple row and store in one variable.Here is my function for fetch data.

I want to fetch all row which type is service. and display all data in php.

Get value function

public function getvalue() {
    $db = connectionstart();
    $sql = ("SELECT * FROM user_posts WHERE type='service' LIMIT 3");
    $result = mysql_evaluate($db, $sql);

    connectionclose($db);
    return $result;
}

mysql_evaluate function

function mysql_evaluate($db, $sql) {
    $result = mysql_query($sql, $db) or die(mysql_error());
    if (mysql_num_rows($result) == 0)
        return $default_value;
    else
        return mysql_result($result, 0);
}
3
  • 4
    Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. Commented Feb 10, 2016 at 7:07
  • how to use mysqli or pdo Commented Feb 10, 2016 at 7:13
  • I've also included the links in my comment. Click on them to read about these database extensions. Commented Feb 10, 2016 at 7:18

3 Answers 3

1

Write your function as below:-

public function getvalue() {
    $db = connectionstart();
     // No need of bracket here
    $sql = "SELECT * FROM user_posts WHERE type='service' LIMIT 3";
    $result = mysql_query($sql);
    // declare an array
    $data = [];
    if($result){
        // loop
        while($row = mysql_fetch_array($result)) {
                 // store data in an array
                 $data[] = $row; 

         }    
    }

    return $data;
}

Hope it will help you :)

Sign up to request clarification or add additional context in comments.

Comments

0

You should try with array store technique

//  First Store data in $data
$data = array();
$result = mysql_evaluate($db, $sql, $defaultvalue);
foreach ($result  as $row) {
    $data [] = $row;
}
// Your all data will be store in $data variable
print_r($data);

2 Comments

Invalid argument supplied for foreach() give this typ of error @Php Developer
@KevinPPatel check this print_r($result) and let me know which kind array are you retrieving..
0

Try this article on w3schools.com, especially the part with $row=$result->fetch_assoc()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.