get success/failure response from mysqli_query

mysql, php

Solution

You can use `mysqli_error()` to see if an error occurred

if (mysqli_error($runcheck ))
{
   // an error eoccurred
}

In your particular example you're better of checking if the row exists before doing the insert. Your example is close but would better using `mysqli_num_rows()`:

$check = "SELECT * FROM jos_activeagents WHERE AGENTUID = '".$agt."'";
$runcheck = mysqli_query($link,$check);
if (mysqli_num_rows($runcheck) > 0)
{
    // username in use
}

Problem

I'm gathering info from a user and then adding them to a table. ``` $insert = "INSERT INTO jos_activeagents (RINGPHONE, AGENTUID, FNAME, LNAME) VALUES ('(618) 717-2054','".$result['AGTBRDIDMM']."','".$result['AGTFNAME']."','".$result['AGTLNAME']."')"; $set = mysqli_query($link,$insert); ``` `AGENTUID` is a unique key. If a user tries to submit with a duplicate unique key, I get an error (of course). Now, how would I go about knowing if and when an error occurred and then putting a response back to the page? I know of `mysqli_get_warnings()`, but the PHP manual doesn't show any examples. I have also tried looking for the `AGENTUID` in the table first: ``` $check = "SELECT * FROM jos_activeagents WHERE AGENTUID = '".$agt."'"; $runcheck = mysqli_query($link,$check); $rescheck = mysqli_fetch_assoc($runcheck); if($rescheck != null){ echo 'This Agent ID is already enrolled.' } ``` But this seems sloppy. Is there a better way do this?

Original source

Related problems