Jump to content

[SOLVED] mysqli query not working


will35010

Recommended Posts

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;

?>

Link to comment
Share on other sites

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'];

?>

Link to comment
Share on other sites

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'];

?>

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.