Web Hosting Talk







View Full Version : PHP / MySQL not grabbing content.


BostonGuru
05-04-2008, 05:37 PM
I have very basic code to get a cell from a MySQL DB



$mysqli = new mysqli($host,$user,$password,$database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($stmt = $mysqli->prepare("SELECT pageContent FROM articles WHERE articleID=?")) {
$articleID = 20;
$stmt->bind_param("i", $articleID);
$articleID = 20;
$stmt->execute();
$stmt->bind_result($articleContent);

while ($stmt->fetch()) {
printf("%s \n", $articleContent);
}


$stmt->close();
}
else
{
echo 'error';
}

$mysqli->close();

So I have a article title column which is of type 'text' and a content column of type 'longtext'. When I run this code with the SQL query to grab the title column it works fine, but when I run this code with the SQL query to grab the content column, it returns an empty string.

The content cells have html markup in them. Could that be causing a problem? If not, what could the problem be? Thanks.

azizny
05-04-2008, 06:53 PM
Maybe this:

$stmt->bind_param("i", $articleID);

Should be:

$stmt->bind_param("?", $articleID);

Peace,

Burhan
05-05-2008, 09:01 AM
How do you know its not returning anything?

nsvr
05-05-2008, 01:25 PM
Your type for your column is too big i guess, and probably causes a memory overflow.

LONGTEXT is 2^32 bytes, which is about 4GB.

http://dev.mysql.com/doc/refman/5.1/en/string-type-overview.html#string-type-overview

Always choose the smallest types possible for a column. I think you don't need to store 4GB in a text field right?

You can probably change it to MEDIUMTEXT, which is 2^24, and around 16MB. You can store a lot text in that.

ALTER TABLE `articles` CHANGE `pageContent` `pageContent` MEDIUMTEXT


Maybe this:

$stmt->bind_param("i", $articleID);

Should be:

$stmt->bind_param("?", $articleID);

Peace,

No "i" is fine as it binds articleID as an Integer. With mysqli you can only choose from i (integer), d (double), s (string) or b (blob). The question mark in the query is correct.

BostonGuru
05-07-2008, 03:05 PM
Hi guys,

Thanks for the responses. I am in the middle of finals (fun!) which is why I am just getting back to this thread now.

I tried writing the same code using mysql instead of mysqli, in function form rather than oop form, and it worked fine (go figure...). Although the code doesn't look as clean, and I can't use prepared statements, it gets the job done.