I've been trying to make a (very) simple shopping system where somebody can see the current orders, here is the code: <?php //create short variable name $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; ?> <html> <head> <title>Bob's Auto Parts - Customer Orders</title> </head> <body> <h1>Bob's Auto Parts</h1> <h2>Customer Orders</h2> <?php $mysql = mysqli_connect( 'localhost', 'bob', 'bob' ); if(!$mysql) { echo 'Cannot connect to database.'; exit; } // select the appropriate database $selected = mysqli_select_db( $mysql, 'bob' ); if(!$selected) { echo 'Cannot select database.'; exit; } $result = $mysql->query('select * from orders;'); $eresult = settype($result, string); echo $eresult; ?> </body> </html> PHP: when I view the page I get this error message: Catchable fatal error: Object of class mysqli_result could not be converted to string in /var/www/bobs/vieworders.php on line 27 something to do with it not being a string? Any help would be great.
It means you can't convert the result you get (the result object) into a simple string. Instead you have to recourse through the result set try something like this: while ($row = mysql_fetch_array($result)) { echo "{$row["orderid"]}<br>"; } Where orderid is just an assumption of mine for your field's name, just replace it with whatever field name/s you use...
Thanks, but now I get this message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/bobs/vieworders.php on line 27
This should be correct (you can't mix mysql and mysqli functions): while($row = $result->fetch_object()) { echo $row->orderid, '<br />'; } Code (php):
right, my bad, lordofthelake what would you say is the main difference between the two methods? which one is better to use?
Well, there isn't the "best" method... mysql_* functions and mysqli are just two different extensions for doing the same thing. mysql: Procedural style A bit faster (but the difference is negligible) "Traditional" way to interact with MySql Will be dropped in PHP6 mysqli (MySql Improved): Both OO and procedural style supported Present since PHP4, it's the preferred method to use in PHP5 Gives easier access to many new MySql functions (for example, transactions and prepared statements) Will benefit of the new mysqlnd driver, soon.