Jump to content

Updating to MySQLi


phppup

Recommended Posts

I am updating some old code to the IMPROVED version of PHP and running into a problem in this section after connecting successfully to my database:

// retrieve the record that was selected
 $record_id = (isset($_POST['record_id'])) ? $_POST['record_id'] : '';

//check for $record_id emptiness
if(!empty($record_id)){

// Retrieve ALL the data from the "example" table
$result = mysqli_query($conn, "SELECT * FROM $table WHERE id = $record_id ")
or die(mysqli_error());

// store the record of the "example" table into $row
$row = mysqli_fetch_array( $result, MYSQLI_BOTH );

} 

Not sure what I'm missing or what needs to be re-organized.

Please advise.

Link to comment
Share on other sites

I have many BLANK lines for easier reading in my coding.

My first ERROR MESSAGE read:

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/..... on line 368

So, I commented out line 368

$result = mysqli_query($conn, "SELECT * FROM $table WHERE id = $record_id ");
//or die(mysqli_error());

This took me to the following:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/.... on line 378

$row = mysqli_fetch_array( $result, MYSQLI_BOTH );

which is required to fill fields with data from my table.

I guess I need to fix the fetch and the mysqli_error for a fully acceptable coding result.

Link to comment
Share on other sites

Now that I'm playing with ERROR messages, I see that

or die(mysqli_error())

provides the line number that corresponds to the coding issue, but differs from

or die(mysqli_error($conn))

which specifically gave me a reason for the unsuccessful effort.

Is there a simple way to display BOTH pieces of information, or does an IF statement need to be created?

Edited by phppup
Link to comment
Share on other sites

10 minutes ago, phppup said:

Is there a simple way to display BOTH pieces of information

The former had a php error so gave you a php error message. The latter gives you the mysql error message.

If you see the first then you won't see the second (as you found out). However the latter should tell the line number in your code where the mysql error occured.

A couple of pieces of advice.

 1 ) If you are moving off mysql_ functions, move to PDO and not mysqli_ functions.

 2 ) If you insist on sticking with mysqli, call

 mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

before your mysqli connection code. That will tell mysqli to automatically report all errors without your having to use "or die()" every time.

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.