will35010 Posted June 4, 2009 Share Posted June 4, 2009 The query works CLI so I know it's my php that's bad. I'm trying to retrieve a result from the database and echo it. I normally use mysqli_fetch_array, but this is only one field so it would be overkill. What function do I use to display only one result? <?php $vin = "11111111111111112"; require('lib/opendb.php'); $query = mysqli_query($conn, "SELECT mileage FROM cmileage WHERE vin = ".$vin.""); $row = mysqli_fetch_object($query); echo $row; ?> Quote Link to comment Share on other sites More sharing options...
will35010 Posted June 4, 2009 Author Share Posted June 4, 2009 This works. I just thought there might be a better practice. <?php $vin = "11111111111111112"; require('lib/opendb.php'); $query = mysqli_query($conn, "SELECT mileage FROM cmileage WHERE vin = ".$vin.""); $row = mysqli_fetch_array($query); echo $row['mileage']; ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 4, 2009 Share Posted June 4, 2009 Nope, that's fine. Quote Link to comment Share on other sites More sharing options...
will35010 Posted June 4, 2009 Author Share Posted June 4, 2009 Nope, that's fine. Thanks. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 4, 2009 Share Posted June 4, 2009 Well, I would do some things differently: 1. No need to exit the double quote to include a variable. 2. Add error handling to the query call 3. Create the query as a variable so if there is an error you can echo it to the page Note: I would use more extensive error handling to include a debug mode vs. production mode, but this gives you an idea <?php $vin = "11111111111111112"; require('lib/opendb.php'); $query = "SELECT mileage FROM cmileage WHERE vin = $vin"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_array($query) or die ("Query: $query<br />Error: ".mysql_error()); echo $row['mileage']; ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 4, 2009 Share Posted June 4, 2009 This is a matter of personal opinion but I don't find or die() a good way to debug anything. If $vin is a number, it shouldn't need to be in quotes. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 4, 2009 Share Posted June 4, 2009 This is a matter of personal opinion but I don't find or die() a good way to debug anything. I'm not trying to start an argument, but I did state that the error handling in that example was just to give the OP an idea on how it would be approached. As I stated, I would utilze more comprehensive error handling with a debug mode so that a user would see a friendle error message or, while in debug mode, would give the developer more detailed information as to any errors that occur. In either case I would not use "on die()" - but it is a valid method of doing a quick test to see why a query is failing (or to confirm it is not failing). If $vin is a number, it shouldn't need to be in quotes. You really need to pay attention to what is written. In the original post the author was using double quotes to define the query and exited the quotes to append the $vin then appended another empty string using double-quotes. My statement (and particularly the code I provided) show that $vin can just be included within the double quotes that define the query. Makes a heck of a lot more sense than appending an empty string! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.