Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 50
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,501
    Thanks
    72
    Thanked 105 Times in 104 Posts

    PHP Notice: Array

    I am getting this error

    [14-Jun-2014 12:27:42 America/Denver] PHP Notice: Array to string conversion in /home1/newrodga/public_html/courtyard/blacksmith.php on line 14
    I've never seen this error before. For some reason it's because of $char, which I know is a string, but no idea how to convert it to something that works. It's setup the way I want it. Unless I have to do something like $new = $char; but no idea. Here is the coding.

    PHP Code:
    $fullrow mysqli_query($link"SELECT * FROM rod_CharName WHERE username='$activerow' AND active='1'");
    $char mysqli_fetch_array($fullrow); 
    PHP Code:
    <?php
    include ('../header_common.php');

    include (
    '../includes/header.php');
    include (
    '../includes/content.php');

          
    ?>
          <div id="maintitle"><h2><?php echo "".$char['currenttown'].""?> Blacksmith</h2>
          <?php

          $blacksmithQ
    mysqli_query($link"SELECT * FROM rod_blacksmith ORDER BY id");

           echo 
    "Take a look at my equipment below. Be careful and make sure you can use what you buy.<br /><br />";
           
    mysqli_query($link"UPDATE rod_CharName SET gold-5 WHERE username='$char'");
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,439
    Thanks
    62
    Thanked 537 Times in 524 Posts
    $char = array();

    Be warned, this will destroy any string that is held within $char
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,501
    Thanks
    72
    Thanked 105 Times in 104 Posts
    What do you mean that it will be destroyed?
    It was originally ".$char['username']." but that doesn't work as it's canceled out by $char already calling on the username.
    So I need a way of just pulling $char.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    You can use the constant MYSQLI_ASSOC:

    Code:
    $result = mysqli_fetch_array( $query, MYSQLI_ASSOC );
    
    echo $result['currentdown'];

  • #5
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,501
    Thanks
    72
    Thanked 105 Times in 104 Posts
    That is causing this: Unless I am using it wrong.

    [14-Jun-2014 17:09:58 America/Denver] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, array given in /home1/newrodga/public_html/courtyard/blacksmith.php on line 14
    [14-Jun-2014 17:09:58 America/Denver] PHP Notice: Array to string conversion in /home1/newrodga/public_html/courtyard/blacksmith.php on line 15
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,439
    Thanks
    62
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by myfayt View Post
    That is causing this: Unless I am using it wrong.
    [14-Jun-2014 17:09:58 America/Denver] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,
    We've had this conversation before.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,501
    Thanks
    72
    Thanked 105 Times in 104 Posts
    Yes we did, but I didn't change the parameter. It's still $id and $link (from previous forum post)
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,439
    Thanks
    62
    Thanked 537 Times in 524 Posts
    So you've not done this?:

    $result = mysqli_fetch_array( $query, MYSQLI_ASSOC );

    If you have, there is a missing parameter that bl4ckc0d3r has left out.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #9
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,501
    Thanks
    72
    Thanked 105 Times in 104 Posts
    Yes I did exactly that. If it's a missing parameter that is what we need to investigate.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #10
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    mysqli_fetch_array()

    The procedural style is shown as this:
    PHP Code:
    <?php
    $link 
    mysqli_connect("localhost""my_user""my_password""world");

    /* check connection */
    if (mysqli_connect_errno()) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();
    }

    $query "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
    $result mysqli_query($link$query);

    /* numeric array */
    $row mysqli_fetch_array($resultMYSQLI_NUM);
    printf ("%s (%s)\n"$row[0], $row[1]);

    /* associative array */
    $row mysqli_fetch_array($resultMYSQLI_ASSOC);
    printf ("%s (%s)\n"$row["Name"], $row["CountryCode"]);

    /* associative and numeric array */
    $row mysqli_fetch_array($resultMYSQLI_BOTH);
    printf ("%s (%s)\n"$row[0], $row["CountryCode"]);

    /* free result set */
    mysqli_free_result($result);

    /* close connection */
    mysqli_close($link);
    ?>
    Check the query with phpMyAdmin or another GUI interface if you have one. Also, run var_export() on the $fullrow to see what it's holding.

  • #11
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,131
    Thanks
    2
    Thanked 328 Times in 320 Posts
    Going back to the first post in this thread, the only possible code in that post that could produce the first stated error is if $activerow (being put into the sql query statement) is an array. Which line in the first posted code is line 14 of blacksmith.php?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #12
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,439
    Thanks
    62
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by myfayt View Post
    Yes I did exactly that. If it's a missing parameter that is what we need to investigate.
    No investigation needed, you just need to put your result variable in there as the first parameter. As I've told you previously, the error message tells you everything.

    I did tell you previously that you need to try and understand whats going on and then try to adapt peoples code samples to make them work for you. Clearly this hasn't quite sunk in because you've still not realised that you needed to adapt that piece of code rather than just copy and paste it.

    ALWAYS go to php.net/<function_name> to find out about a function and the parameters it requires. <function_name> should be changed for the actual name of your function - eg mail() would be http://www.php.net/mail
    Last edited by tangoforce; 06-15-2014 at 11:51 AM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #13
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,501
    Thanks
    72
    Thanked 105 Times in 104 Posts
    This is the results of var_export($fullrow); Now $fullrow works fine when using a SELECT query, just not UPDATE OR INSERT INTO.

    PHP Code:
    mysqli_result::__set_state(array( 'current_field' => NULL'field_count' => NULL'lengths' => NULL'num_rows' => NULL'type' => NULL, )) 
    @CFM, this is the entire blacksmith.php coding

    PHP Code:
    <?php
    include ('../header_common.php');

    include (
    '../includes/header.php');
    include (
    '../includes/content.php');

          
    ?>
          <div id="maintitle"><h2><?php echo "".$char['currenttown'].""?> Blacksmith</h2>
          <?php

          $blacksmithQ
    mysqli_query($link"SELECT * FROM rod_blacksmith ORDER BY id");

           echo 
    "Take a look at my equipment below. Be careful and make sure you can use what you buy.<br /><br />";
           
    mysqli_query($link"UPDATE rod_CharName SET gold-5 WHERE username='$char'");
           echo 
    '<table width="75%"><tr><td style="background: blue; text-align: center;"><h3>ITEM NAME</h3></td><td style="background: blue; text-align: center;"><b>TYPE</b></td><td style="background: blue; text-align: center;"><b>COST</b></td><td style="background: blue; text-align: center;"><b>CAN EQUIP</b></td><td style="background: blue; text-align: center;"><b>MINIMUM LEVEL</b></td></tr>';
            while (
    $blacksmithrow mysqli_fetch_array($blacksmithQ)) {
           if (
    $blacksmithrow['type'] == '1') {
               
    $type 'Weapon';
           }
           elseif (
    $blacksmithrow['type'] == '2') {
             
    $type 'Armor';
           }
           else {
             
    $type 'Shield';
           }
           if (
    $char['gold'] < $blacksmithrow['cost']) {
             
    $cost "<b><font color=red>".$blacksmithrow['cost']."</b></font>";
           }
           else {
             
    $cost "<b><font color=green>".$blacksmithrow['cost']."</b></font>";
           }
            echo 
    "<tr><td style='background: black;'><h3><a href=\"blacksmith.php?rod=buy:".$blacksmithrow['id']."\">".$blacksmithrow["name"]."</a></h3></td><td style='background: black; text-align: center;'><b>$type</b></td><td style='background: black; text-align: center;'>$cost</td><td style='background: black; text-align: center;'><b><font color=green>Yes</font></b></td><td style='background: black; text-align: center;'>Lv ".$blacksmithrow['minlevel']."</td></tr>";

          }
           echo 
    '</table>';


           
    ?>
            </div>
           <?php
           
           
    if (isset($_GET['rod']))
    {
        
    $rod explode(':'$_GET['rod']);

        switch(
    $rod[0])
        {

            case 
    'buy':
                
    buy($rod[1], $link);
            break;
            
                }

    }
           
           function 
    buy($id$link) {
                  global 
    $link$char;
              
    $getitem mysqli_query($link"SELECT * FROM rod_blacksmith WHERE id='$id' LIMIT 1");
        
    $itemrow mysqli_fetch_array($getitem);

        echo 
    "".$char['charname']." purchased the <b>".$itemrow['name']."</b> for <b>".$itemrow['cost']."</b>";
        
    mysqli_query($link"UPDATE rod_CharName SET gold-".$itemrow['cost']." WHERE username='".$char['username']."'");
            
    mysqli_query($link"INSERT INTO rod_myequipment SET username='".$char['username']."' id='".$itemrow['id']."' name='".$itemrow['name']."' type='".$itemrow['type']."' power='".$itemrow['power']."' minlevel='".$itemrow['minlevel']."' class='".$itemrow['class']."'");
     }

    include (
    '../includes/contentend.php');
    include (
    '../includes/footer.php');
      
    ?>
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #14
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,131
    Thanks
    2
    Thanked 328 Times in 320 Posts
    So, the following is line 14 of blacksmith.php -
    PHP Code:
    mysqli_query($link"UPDATE rod_CharName SET gold-5 WHERE username='$char'"); 
    There's at least four things wrong with it -

    1) Where it is at in the program flow makes no sense (it looks like it is left over from some long ago version of the code or due to a copy/paste failure.) If it did run without any php or mysql errors and update that table, you are using it unconditionally in your code, so it would update that table every time the code on that page runs. Functionally (pun intended), that query is part of the logic in your buy() function. What is it even doing there on line 14 of that file?

    2) The SET gold-5 term in it makes no sense and will produce a mysql query error. The correct syntax would be SET some_column = an_expression and the query implies you want to subtract 5 from that column's current value (or even better yet subtract an amount given by a php variable), so, it would be SET gold = gold - 5 (using a hard coded value) or SET gold = gold - $some_php_variable (using a value from a php variable.)

    3) You are not testing if your database queries work or are failing due to errors, so you won't know when or why they are failing.

    4) Since $char is an array of character information, there's no way that WHERE username='$char' would work. Which element of the $char array contains the username? <-- That's a clue in the form a question (Jeopardy theme music playing) because this is your code (the blacksmith.php file only contains about 60 lines of actual code/markup) and you should be aware of what each variable in it means, what each line of code in it does, and what each line of code contributes to what the page is trying to accomplish.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #15
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,439
    Thanks
    62
    Thanked 537 Times in 524 Posts
    @CFMaBiSmAd You've just lost myfayt for the next year lol.

    Step by step...
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •