mysqli connection not cooperating

All,

I’ve been programming PHP for about a year now, but for some reason, I can’t connect to one of my databases.

[PHP]
$con = mysqli_connect(“localhost”, “timw79_rfid”, “xpress13”, “timw79_rxrfid”);if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error();}
$result = mysqli_query($con,“SELECT AssignedMachine FROM TagAssignments WHERE TagID=$tag1”);

while($row = mysqli_fetch_array($result)) { echo $row[‘AssignedMachine’] . " " . $row[‘TagID’]; echo “
”;}[/PHP]

I have confirmed that $tag1 is reporting correctly. I checked spelling and capitalization. I’m stumped on this one. I am getting the error:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home3/timw79/public_html/rxrfid/piupload/addmachinenames.php on line 76

Where have I gone wrong?

I have attached the PHP MyAdmin page for this table in case it is needed.


Because TagID is not an integer value, you need ‘single’ quotes around the $tag1 in the query string. But that is not truly the correct way either cause you’re not sanitizing the $tag1 at all before putting it in the query, so you’re wide open to sql injection. If you’re still learning mysqli at this time, I would highly suggest switching to PDO now, it’s (in my opinion) much better and I found it much easier than mysqli to use also.

Now I have to state I usually work with PDO (Second that nomination ;D) and I don’t know about your query if it is right or wrong. However, someone else can help you there.

Here an untested script that should get you there:

[php]$query = “SELECT AssignedMachine FROM TagAssignments WHERE TagID=’$tag1’”;

if ($result = mysqli_query($con, $query)) {

/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
	echo $row['AssignedMachine'] . " " . $row['TagID'] . "<br>\n";
}

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

}[/php]

That did the trick! Thank you for the help.

Sponsor our Newsletter | Privacy Policy | Terms of Service