Discuss Mysqli_query bafflement in the PHP Development forum on Dev Shed. Mysqli_query bafflement PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Posts: 31
Time spent in forums: 1 Day 5 h 52 m
Reputation Power: 2
Mysqli_query bafflement
I have been testing on a local server and thought to myself that I would test it out on the web server. It works perfectly locally, but not remotely. I have done Google searches, search this forum and change my code around what seems like a billion times. What it's doing it is deleting instead of updating the text content portion of the Table (it doesn't delete the row) on the server, so I think my query is working fine, but not the info isn't being sent? Here's a small portion of my code:
$query = "UPDATE pages SET content='$content' WHERE id=1";
$query = mysqli_query($db, $query) or die (mysqli_error($db));
}
I wrote tighter code, but when I start getting this problem I just trying to get it to work. I have taken out the mysql_prep() function out of the equation and get the same results. My best guess at this moment is either something isn't compatible with the PHP versions between the local and remote server, MySQL database is wrong on the remote server (Though gone through that with a fine tooth comb), or the most logical choice is I not doing something right. Any help would be greatly appreciated, even if it's just pointing me in the right direction or area will be fine. If more code is need I can post that also.
Posts: 2,046
Time spent in forums: 1 Month 3 Weeks 4 h 54 m 33 sec
Reputation Power: 812
Hi,
the first thing you should do is get rid of all this stuff you somehow carried over from the old MySQL library.
MySQLi has prepared statements, so no more fumbling with query strings and variables. At least you escape the values. Though I have a feeling you're not doing it right, because I don't see you passing the database object to your mysql_prep() function (which is necessary to determine the character encoding).
Also, please throw away this "or die(mysqli_error())" stuff. I don't know who invented that, but it's a really, really terrible way of handling errors -- except for script kiddies trying to "hack" your website. They'll get great help while optimizing their attacks on your database
After you've done that, what is the content of $content?
Posts: 31
Time spent in forums: 1 Day 5 h 52 m
Reputation Power: 2
One more thing that is baffling me is how to update the table, I understand the logic of getting information from database. It's actually simple, but change the results is a different story. I know I have to use something like the following - "UPDATE pages SET content=$content WHERE menu_name=?". When I go to the PHP Manual they give an example how to insert to a table, but not update. So I thought I go to MySQL Reference manual on the syntax on how to do it, but that just seems to confuse me more. I am doing a local test script to see if I can get it working before I rewrite my final code. I do appreciate the help, even if I can just be pointed in the right direction like before. I have been doing Google Searches, but I think I being to vague or not wording the search correctly. Thanks once again for the help.
$menu = "Green Cay";
function display_content($menu) {
global $link;
/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "SELECT content FROM pages WHERE menu_name=?")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $menu);
/* execute query */
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $content);
/* fetch value */
mysqli_stmt_fetch($stmt);
//printf("Content is : %s \n", $content);
/* close statement */
mysqli_stmt_close($stmt);
return $content;
}
}
$content = display_content($menu);
$content .= " this is a test";
Posts: 2,046
Time spent in forums: 1 Month 3 Weeks 4 h 54 m 33 sec
Reputation Power: 812
First of all, good work rewriting the code to prepared statements.
Doing an UPDATE is actually even simpler, because it's over as soon as you've executed the prepared statement. Note that you need a query parameter for each value you want to insert, so in your case you have two: one for $content, one for $menu.
You might wanna use the object-oriented API of the MySQLi extension, because it's somewhat less cumbersome:
PHP Code:
<?php
$database = new mysqli('localhost', 'USER', 'PW', 'DB');
Posts: 31
Time spent in forums: 1 Day 5 h 52 m
Reputation Power: 2
Just like to say Thank You for the help, it diffidently steered me in the right direction. I Know have a lot more to learn in PHP, but I think now I'm steered going in the right direction. My next adventure will be learning PDO, but for right now going in the mysqli direction is better that the old mysql.