0

plz help me to resolve this problem..

Notice: Undefined variable: con in C:\xampp\htdocs\ecommerce\function\fuction.php on line 10

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\ecommerce\function\fuction.php on line 10

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\ecommerce\function\fuction.php on line 11

<?php
$host="localhost";
$user_id="root";
$password="";
$con = mysqli_connect($host,$user_id,$password) OR die("not connected.... ");
$db = mysqli_select_db($con,'ecommerce') OR die ("not connected...");
?>




<?php
function getbrands()
{
$fetch_brand="SELECT * FROM brands";


        $run=mysqli_query($con,$fetch_brand);
    while($row=mysqli_fetch_array($run));

         $brands_id=$row[0];
         $brands_title=$row[1];

         echo "<h4 align='center' style='margin:0px'>

         <a href='ecommerce.php?brands=$brands_id'>$brands_title</a></h4><hr>";


    }



?>

1 Answers1

0

Try this:

<?php
function getbrands()
{
    global $con;
    $fetch_brand="SELECT * FROM brands";


        $run=mysqli_query($con,$fetch_brand);
    while($row=mysqli_fetch_array($run));

         $brands_id=$row[0];
         $brands_title=$row[1];

         echo "<h4 align='center' style='margin:0px'>

         <a href='ecommerce.php?brands=$brands_id'>$brands_title</a></h4><hr>";


    }
?>
  • thanksss bro.....it works bt there is fetching problem ....there is no data fetching from the database – mukesh kumar Apr 22 '16 at 10:49
  • 2
    @mukeshkumar For using any variable declared outside of a function, you need to make it global inside your function. And mark the solution as answer If it solved your problem. –  Apr 22 '16 at 10:50
  • ok i marked....bt wht abt fetchng from database... – mukesh kumar Apr 22 '16 at 10:59
  • @mukeshkumar try dumping $row in while loop like this `var_dump($row)` and post output in the comment. –  Apr 22 '16 at 11:01
  • Fatal error: Can't use function return value in write context in C:\xampp\htdocs\ecommerce\function\fuction.php on line 13 – mukesh kumar Apr 22 '16 at 11:05
  • after adding var_dump($row ) this error occured – mukesh kumar Apr 22 '16 at 11:06
  • Where did you add the `var_dump($row);` ? –  Apr 22 '16 at 11:09
  • while(var_dump($row)=mysqli_fetch_array($run)); – mukesh kumar Apr 22 '16 at 11:11
  • @mukeshkumar Inside while loop means, inside the block of while loop, `while($row = mysqli_fetch_array($run)){ var_dump($row); }` –  Apr 22 '16 at 11:12
  • rray(6) { [0]=> string(1) "1" ["id"]=> string(1) "1" [1]=> string(1) "1" ["brands_id"]=> string(1) "1" [2]=> string(3) "hcl" ["brands_title"]=> string(3) "hcl" } array(6) { [0]=> string(1) "2" ["id"]=> string(1) "2" [1]=> string(1) "3" ["brands_id"]=> string(1) "3" [2]=> string(4) "sony" ["brands_title"]=> string(4) "sony" } array(6) { [0]=> string(1) "3" ["id"]=> string(1) "3" [1]=> string(1) "2" ["brands_id"]=> string(1) "2" [2]=> string(3) "hcl" ["brands_title"]=> string(3) "hcl" } – mukesh kumar Apr 22 '16 at 11:18
  • now it can b show like this inside the div where i want to fetch.. – mukesh kumar Apr 22 '16 at 11:19
  • change this line `echo "

    $brands_title


    ";` with `echo "

    ".$brands_title."


    ";`
    –  Apr 22 '16 at 11:22
  • bro nothing changed...same error – mukesh kumar Apr 22 '16 at 11:26
  • What is the error now? –  Apr 22 '16 at 11:26
  • same error... rray(6) { [0]=> string(1) "1" ["id"]=> string(1) "1" [1]=> string(1) "1" ["brands_id"]=> string(1) "1" [2]=> string(3) "hcl" ["brands_title"]=> string(3) "hcl" } array(6) { [0]=> string(1) "2" ["id"]=> string(1) "2" [1]=> string(1) "3" ["brands_id"]=> string(1) "3" [2]=> string(4) "sony" ["brands_title"]=> string(4) "sony" } array(6) { [0]=> string(1) "3" ["id"]=> string(1) "3" [1]=> string(1) "2" ["brands_id"]=> string(1) "2" [2]=> string(3) "hcl" ["brands_title"]=> string(3) "hcl" } – mukesh kumar Apr 22 '16 at 11:29
  • It's not the error, just remove the `var_dump($row);` –  Apr 22 '16 at 11:30
  • it removed all this....bt i want to fetch data from database – mukesh kumar Apr 22 '16 at 11:33
  • nd nothing is fetching. – mukesh kumar Apr 22 '16 at 11:33
  • @mukeshkumar Are you new in php? This is the data `array(6) { [0]=> string(1) "1" ["id"]=> string(1) "1" [1]=> string(1) "1" ["brands_id"]=> string(1) "1" [2]=> string(3) "hcl" ["brands_title"]=> string(3) "hcl" } array(6) { [0]=> string(1) "2" ["id"]=> string(1) "2" [1]=> string(1) "3" ["brands_id"]=> string(1) "3" [2]=> string(4) "sony" ["brands_title"]=> string(4) "sony" } array(6) { [0]=> string(1) "3" ["id"]=> string(1) "3" [1]=> string(1) "2" ["brands_id"]=> string(1) "2" [2]=> string(3) "hcl" ["brands_title"]=> string(3) "hcl" }` which is being fetched fromt the database. –  Apr 22 '16 at 11:35
  • yes i m beginner in php.... – mukesh kumar Apr 22 '16 at 11:36
  • @mukeshkumar Please post your full source code in your question. –  Apr 22 '16 at 11:37
  • ya i know this is fetching from database...bt i only want brand_title is to b fetched nt like the whole information abt that.. – mukesh kumar Apr 22 '16 at 11:38
  • change this `SELECT * FROM brands` with `SELECT brand_title FROM brands` and access it like this `echo $row['brand_title']` –  Apr 22 '16 at 11:39
  • @mukeshkumar give me access to your source code, If you don't have any problem with that and I will fix the problem. –  Apr 22 '16 at 11:46
  • $brands_title
    "; } ?>
    – mukesh kumar Apr 22 '16 at 11:48
  • @mukeshkumar Please provide me the source code files somehow. –  Apr 22 '16 at 11:50
  • It's developer.rohitkhatri[at]gmail[dot]com –  Apr 22 '16 at 11:52
  • done...i sent it...check it out – mukesh kumar Apr 22 '16 at 11:56
  • @mukeshkumar I have replied with fixed source code zip file, please extract the files and use them instead, let me know if you find any trouble. –  Apr 22 '16 at 12:06
  • bro this all file in encypted form...after extracting all files are crashed – mukesh kumar Apr 22 '16 at 12:11
  • sorry i got d files....wait i'll check it works or not – mukesh kumar Apr 22 '16 at 12:13
  • Warning: mysqli_fetch_all() expects at least 1 parameter, 0 given in C:\xampp\htdocs\ecommerce\function\fuction.php on line 9 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ecommerce\ecommerce.php on line 32 – mukesh kumar Apr 22 '16 at 12:18
  • now this warning is shown – mukesh kumar Apr 22 '16 at 12:18
  • @mukeshkumar Please replace `$brands = mysqli_fetch_all();` with `$brands = mysqli_fetch_all($result,MYSQLI_ASSOC);` –  Apr 22 '16 at 12:19
  • leave it bro...i'll find another way.... thankssssss bro for helping me...m very grateful that u tried ...again thnkxx a lot – mukesh kumar Apr 22 '16 at 12:26
  • @mukeshkumar If you have teamviewer, I can assist you by remote access, If you don't have any problem with that. –  Apr 22 '16 at 12:27
  • sorry bro i dnt have – mukesh kumar Apr 22 '16 at 12:30
  • its ok bro....i'll find another way.. – mukesh kumar Apr 22 '16 at 12:31