...

View Full Version : connection error (mysqli)



fogofogo
06-12-2006, 11:44 AM
Hello All,

I have a script that connects to a database and displays a few images and text. It worked fine when running on my machine at home, but I brought the code and database to another machine, and got a few errors. I'm pretty sure its to do with how my php.ini file is set up but I can seem to figure it out.

Heres the error message I get


Warning: mysqli::mysqli() [function.mysqli]: (/1045): in C:\wamp\www\work\greg\db_fns.php on line 5

Warning: mysqli::query() [function.query]: Couldn't fetch mysqli in C:\wamp\www\work\greg\index.php on line 9

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\wamp\www\work\greg\index.php on line 13

and heres the code for db_fns.php


<?php

function db_connect()
{
$handle = new mysqli('localhost', 'content', 'password', 'content');
if (!$handle)
{
return false;
}
return $handle;
}

function get_writer_record($username)
{
$handle = db_connect();
$query = "select * from writers where username = '$username'";
$result = $handle->query($query);
return($result->fetch_assoc());
}

function get_story_record($story)
{
$handle = db_connect();
$query = "select * from stories where id = '$story'";
$result = $handle->query($query);
return($result->fetch_assoc());
}

function query_select($name, $query, $default='')
{
$handle = db_connect();

$result = $handle->query($query);

if (!$result)
{
return('');
}

$select = "<select name='$name'>";
$select .= '<option value=""';
if($default == '') $select .= ' selected ';
$select .= '>-- Choose --</option>';

for ($i=0; $i < $result->num_rows; $i++)
{
$option = $result->fetch_array();
$select .= "<option value='{$option[0]}'";
if ($option[0] == $default)
{
$select .= ' selected';
}
$select .= ">[{$option[0]}] {$option[1]}</option>";
}
$select .= "</select>\n";

return($select);
}

?>

and here is the code for index.php


<?php
include_once('db_fns.php');
//include_once('header.php');

$handle = db_connect();

$pages_sql = 'select * from pages order by code';
$pages_result = $handle->query($pages_sql);

echo '<table border="0" width="400">';

while ($pages = $pages_result->fetch_assoc())
{
$story_sql = "select * from stories
where page = '{$pages['code']}'
and published is not null
order by published desc";

$story_result = $handle->query($story_sql);

if ($story_result->num_rows)
{
$story = $story_result->fetch_assoc();
echo "<tr>
<td>
<h2>{$pages['description']}</h2>
<p>{$story['headline']}</p>
<p align='right' class='morelink'>
<a href='page.php?page={$pages['code']}'>
Read more {$pages['code']} ...
</a>
</p>
</td>
<td width='100'>";
if ($story['picture'])
{
echo '<img src="resize_image.php?image=';
echo urlencode($story[picture]);
echo '&max_width=80&max_height=60" />';
}
echo '</td></tr>';
}
}
echo '</table>';

//include_once('footer.php');
?>

I've searched around but can't seem to figure it out. Any thoughts?

Thanks all

dswimboy
07-08-2006, 08:15 PM
mysqli appears to require MySQL v4.1 or above. does your new machine have that?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum