Jump to content

Catchable fatal error: Object of class mysqli could not be converted to string


lauren_etherington

Recommended Posts

Hello Forumites!!

I would like to ask a bit of advice please.

 

I am currently writing a peice of code where my user can add content to their database and retreive it from the database when they view it live on the website.

I want them to be able to edit their content and this my friends is wherein the problem lies.

 

This is my Edit code:

<?php
function editnews($i)
{
	$conn = mysqli_connect("Connection Stringy Stuff");
	
	$enquery = "SELECT * FROM news WHERE newsid=$i";
	
	$enresult = mysqli_query($conn, $enquery) or trigger_error("Query Failed! SQL: $conn - Error: ".mysqli_error(), E_USER_ERROR);
	
	while ($enrow = mysqli_fetch_array($enresult)) {
		
		echo "
				<form enctype='multipart/form-data' action='newsedit.php' method='post'>
				<table>
				
					<tr>
						<td>News Ref:</td>
						<td>" . $i . "<input type='hidden' name='newsid' value='" . $i . "' /><input type='hidden' name='oldim' value='" . $enrow['newsimage'] . "' /></td>
					</tr>
				
					<tr>
						<td>Title:</td>
						<td><input type='text' name='title' value='" . $enrow['newstitle'] . "' size='100' /></td>
					</tr>

						<tr>
						<td>Author:</td>
						<td><input type='text' name='author' value='" . $enrow['newsauthor'] . "' size='100' /></td>
					</tr>

					<tr>
						<td>Status:</td>
						<td><select name='stat'><option value='enabled' ";
		if ($enrow['newstatus'] == "enabled") {
			echo "selected='selected' ";
		}
		echo ">Enabled</option><option value='disabled' ";
		if ($enrow['newsstatus'] == "disabled") {
			echo "selected='selected' ";
		}
		echo "}>Disabled</option></select>
					</td>
					</tr>

					<tr>
						<td>Snippettext:</td>
						<td><textarea name='snip' rows='6' cols='80'>" . $enrow['newssnippet'] . "</textarea></td>
					</tr>

					<tr>
						<td>News story:</td>
						<td><textarea name='stry' rows='20' cols='80'>" . $enrow['newsarticle'] . "</textarea></td>
					</tr>


					<tr>
						<td>Current image:</td>
						<td>" . $enrow['newsimage'] . " - <a href='news/" . $enrow['newsimage'] . "' target='_blank'>View Image</a></td>
					</tr>

					<tr>
						<td>Change Image:</td>
						<td><input type='file' name='file'></td>
					</tr>

					<tr>
						<td colspan='2'><input type='submit' name='submit' value='Edit' /></td>
					</tr>
				</table>
				</form>
			";
	}
}
?>

This code displays on the 'Edit' page using the following above the DOC type:

<?php
include('functions/newseditform.php');
$i = $_GET['i'];
?>

AND the following in the HTML:

<?php
editnews($i)
?>

The problem I have however is that the code at the top, does not actually display anything on the html page :(

I have debugged the code in my IDE and received these error messages:

Notice: Undefined index: i in pathway on Line 3 (This is the bit at the top of the 'Edit' page)

AND

Catchable fatal error: Object of class mysqli could not be converted to string in 'pathway' on line 8 this being this line of the edit code:

$enresult = mysqli_query($conn, $enquery) or trigger_error("Query Failed! SQL: $conn - Error: ".mysqli_error(), E_USER_ERROR);

Before I added the 'trigger_error' bit, it was displaying the "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given" error.

 

 

If anyone can help point out where I went wrong I would be very appreciative.

 

Thanks!!!

Link to comment
Share on other sites

Did you look at the mysqli_query documentation page at all? http://us1.php.net/mysqli_query

 

Look at the example for the procedural style you're using.

 

You should have something like this:

 

if ($enresult = mysqli_query($conn, $enquery)) {
    // query ok, fetch using $enresult.

} else {
    printf("Errormessage: %s\n", mysqli_error($conn));
}
Clearly your problem above is also that in testing you are not apparently passing a url parameter ... site?i=23 or some key value that will make the query valid.
Link to comment
Share on other sites

Well okay, i don't really see how it's possible that your form would output but not that. Unless maybe your html markup elsewhere is causing it to be visually hidden.. look in your view-source and ctrl+f for it... or, how about echoing it to your js console or doing an alert instead..

 

$enquery = "SELECT * FROM news WHERE newsid=$i";
echo "<script>console.log('{$enquery}');alert('{$enguery}');</script>";
The overall goal here is to find out what $i actually contains.
Link to comment
Share on other sites

Well $i should be the id field of the row so it should find the id and then pull the info from that row.

 

I use this code layout for quite a bit of work and I normally don't have any issues, The way it is now has worked in the past when I have used it in other projects (with the obvious bits changed)

however despite countless comparisons and completely copying and pasting code, I just cannot fathom it.

 

There must be something that I'm not seeing :S

Link to comment
Share on other sites

the original error message is because you are putting the $conn variable into a string context -

 

trigger_error("Query Failed! SQL: $conn - Error: ".mysqli_error(), E_USER_ERROR);

 

i suspect you intended to put the variable holding the sql query string at that point. you do however need to use $conn in the msyqli_error($conn) statement.

 

next, the query is failing because the $i variable is either empty or is a string, it is being used in the query as though it is a number, resulting in invalid sql syntax. you would need to both determine exactly what is in $i and you should be validating it so that you don't even attempt to run the query unless you know $i contains an expected type of data.

 

lastly, in looking at your function definition. you should NOT form a database connection inside a function to run one query. your application should form the database connection and pass it into any function/class that in dependent on a database connection.

Link to comment
Share on other sites

The thing is I have used this code before in a similar context. It pretty much my go to code.

If you look at this one there aren't really any major differences that I can see yet this one works and actually displays the form populated by the database information for the user to edit.

function editnews($i)
{
    $conn = mysqli_connect("Connection string where the same functions work");
    $enquery = "SELECT * FROM newsitem WHERE niid=$i";
    $enresult = mysqli_query($conn, $enquery);
    while ($enrow = mysqli_fetch_array($enresult)) {
       
        echo"
			<form enctype='multipart/form-data' action='functions/newsedit.php' method='post'>
			<table>
				<tr>
					<td>News Ref:</td>
					<td>" . $i . "<input type='hidden' name='nid' value='" . $i . "' /><input type='hidden' name='oldim' value='" . $enrow['newsimage'] . "' /></td>
				</tr>
				<tr>
					<td>Author:</td>
					<td><input type='text' name='auth' value='" . $enrow['newsauthor'] . "' size='100' /></td>
				</tr>
				<tr>
					<td>Title:</td>
					<td><input type='text' name='tit' value='" . $enrow['newstitle'] . "' size='100' /></td>
				</tr>
				<tr>
					<tr>
					<td>Snippettext:</td>
					<td><textarea name='snip' rows='6' cols='80'>" . $enrow['newssnippet'] . "</textarea></td>
				</tr>
				<tr>
					<td>News story:</td>
					<td><textarea name='cont' rows='20' cols='80'>" . $enrow['newscontent'] . "</textarea></td>
				</tr>
				<tr>
					<td>Keywords:</td>
					<td><input type='text' name='kw' value='" . $enrow['keywords'] . "' size='100' /></td>
				</tr>
				<tr>
					<td>Current image:</td>
					<td>" . $enrow['newsimage'] . " - <a href='news/" . $enrow['newsimage'] . "' target='_blank'>View Image</a></td>
				</tr>
								
				<tr>
					<td colspan='2'><input type='submit' name='submit' value='Edit' /></td>
				</tr>
			</table>
			</form>
		";
    }
}

I am really at a loss as to why the code in this post does as it is told but when I have modified it to fit this purpose, in the first post, has just decided that it doesn't want to play along, when I have followed the same "template" as it where.....

Baffling my brain!!

Link to comment
Share on other sites

it doesn't matter how many times you may have used any specific code. what matters is the code is currently failing and until you troubleshoot it and determine why it is failing, it won't get fixed.

 

you need to find out why the query is failing by using mysqli_error, correctly, with the $conn variable as a parameter and without the $conn variable as part of a string.

Link to comment
Share on other sites

Okay so this is what the code looks like now.

function editnews($i)

{
 
	$i = $_GET['newsid']; //newsid is the name of the unique id field. 
	$conn = mysqli_connect("connection string")
		or die("could not connect");
	$enquery = "SELECT * FROM news WHERE newsid=$i";
	$enresult = mysqli_query($enquery);
	
	
	if ($enrow = mysqli_fetch_array($enresult)) {

	echo "
				<form enctype='multipart/form-data' action='newsedit.php' method='post'>
				<table>

					<tr>
					<td>News Ref:</td>
					<td>" . $i . "<input type='hidden' name='newsid' value='" . $i . "' /><input type='hidden' name='oldim' value='" . $enrow['newsimage'] . "' /></td>
				</tr>


					<tr>
						<td>Title:</td>
						<td><input type='text' name='title' value='" . $enrow['newstitle'] . "' size='100' /></td>
					</tr>

						<tr>
						<td>Author:</td>
						<td><input type='text' name='author' value='" . $enrow['newsauthor'] . "' size='100' /></td>
					</tr>

					<tr>
						<td>Status:</td>
						<td><select name='stat'><option value='enabled' ";
		if ($enrow['newstatus'] == "enabled") {
			echo "selected='selected' ";
		}
		echo ">Enabled</option><option value='disabled' ";
		if ($enrow['newsstatus'] == "disabled") {
			echo "selected='selected' ";
		}
		echo "}>Disabled</option></select>
					</td>
					</tr>

					<tr>
						<td>Snippettext:</td>
						<td><textarea name='snip' rows='6' cols='80'>" . $enrow['newssnippet'] . "</textarea></td>
					</tr>

					<tr>
						<td>News story:</td>
						<td><textarea name='stry' rows='20' cols='80'>" . $enrow['newsarticle'] . "</textarea></td>
					</tr>


					<tr>
						<td>Current image:</td>
						<td>" . $enrow['newsimage'] . " - <a href='news/" . $enrow['newsimage'] . "' target='_blank'>View Image</a></td>
					</tr>

					<tr>
						<td>Change Image:</td>
						<td><input type='file' name='file'></td>
					</tr>

					<tr>
						<td colspan='2'><input type='submit' name='submit' value='Edit' /></td>
					</tr>
				</table>
				</form>
			";
	}

else {
	printf("Errormessage: %s\n", mysqli_error($conn));
}
	
	}

However I'm still not getting anything, not even an error.

I have ran the debugger on my IDE and got nothing, I have uploaded the file to my test url and there is nothing. I have taken $conn out of the variable and put it back in, I have put it in the wrong way round.

I have changed the query to say that newsid = 1 so no matter what article I click on to edit it should pull the info from the row where the id is 1.

 

I have also added

ini_set('display_errors',1);
error_reporting(E_ALL);
	

to the top of the page with no luck.

Link to comment
Share on other sites

currently, your mysqli_query() statement is missing the $conn parameter, which would be producing a php error like - Warning: mysqli_query() expects at least 2 parameters, 1 given in your file...

 

your previously posted code correctly used mysqli_query(). you seem to be just randomly changing code without any reason. programming like that rarely leads to a solution and makes it very difficult to help when the problem is a randomly moving target.

Link to comment
Share on other sites

Okay so having followed the advice on here the code now looks like this:

function editnews($i)

{
		
	ini_set('display_errors',1);
	error_reporting(E_ALL);

	$i = $_GET['newsid'];
	
	$conn = mysqli_connect("connection string");
	
	$enquery = "SELECT * FROM news WHERE newsid=$i";
	$enresult = mysqli_query($conn, $enquery);


	if (!mysqli_query($conn, "SELECT * FROM news WHERE newsid=$i"))
	{
		echo("Error description: " . mysqli_error($conn));
	}


	while ($enrow = mysqli_fetch_array($enresult)) {

	echo "
				<form enctype='multipart/form-data' action='newsedit.php' method='post'>
				<table>

					<tr>
					<td>News Ref:</td>
					<td>" . $i . "<input type='hidden' name='newsid' value='" . $i . "' /><input type='hidden' name='oldim' value='" . $enrow['newsimage'] . "' /></td>
				</tr>


					<tr>
						<td>Title:</td>
						<td><input type='text' name='title' value='" . $enrow['newstitle'] . "' size='100' /></td>
					</tr>

						<tr>
						<td>Author:</td>
						<td><input type='text' name='author' value='" . $enrow['newsauthor'] . "' size='100' /></td>
					</tr>

					<tr>
						<td>Status:</td>
						<td><select name='stat'><option value='enabled' ";
		if ($enrow['newstatus'] == "enabled") {
			echo "selected='selected' ";
		}
		echo ">Enabled</option><option value='disabled' ";
		if ($enrow['newsstatus'] == "disabled") {
			echo "selected='selected' ";
		}
		echo "}>Disabled</option></select>
					</td>
					</tr>

					<tr>
						<td>Snippettext:</td>
						<td><textarea name='snip' rows='6' cols='80'>" . $enrow['newssnippet'] . "</textarea></td>
					</tr>

					<tr>
						<td>News story:</td>
						<td><textarea name='stry' rows='20' cols='80'>" . $enrow['newsarticle'] . "</textarea></td>
					</tr>


					<tr>
						<td>Current image:</td>
						<td>" . $enrow['newsimage'] . " - <a href='news/" . $enrow['newsimage'] . "' target='_blank'>View Image</a></td>
					</tr>

					<tr>
						<td>Change Image:</td>
						<td><input type='file' name='file'></td>
					</tr>

					<tr>
						<td colspan='2'><input type='submit' name='submit' value='Edit' /></td>
					</tr>
				</table>
				</form>
			";
	}

	}

?>

And i now have errors :D which read:

Notice: Undefined index: newsid in pathway on line 9

Error description: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1.

 

Now I can't quite understand the second error as line one literally contains <?php

Although the first error is also confusing me as well because I am declaring $i as get 'newsid' which is the name of the id field in the news table.....

Link to comment
Share on other sites

Good News!

I've managed to get rid of the 'undefined index' error by changing this in the 'Edit News' page from this:

<?php
session_start();
include('functions/newseditform.php');
$i = $_GET['i'];
?>

To this:

<?php
session_start();
include('functions/newseditform.php');
$i = $_GET['newsid'];
?>

However if anyone can please help with the second error I would be very appreciative!

Link to comment
Share on other sites

Okay So I have managed to fix the two errors that I was originally having problems with.

I now have this error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in.....

 

I have updated my code and it now looks like this:

function editnews($i)

{
	ini_set('display_errors',1);
	error_reporting(E_ALL);

	$conn = mysqli_connect("super duper connection to my DB");
	
	$enquery = "SELECT * FROM news WHERE newsid=$i";
	$enresult = mysqli_query($conn, $enquery) or trigger_error($enquery . ' - has encountered an error at:<br />');

	if ($enresult) {
		
	while ($enrow = mysqli_fetch_array($enresult)) {

	echo "
				<form enctype='multipart/form-data' action='newsedit.php' method='post'>
				<table>

					<tr>
					<td>News Ref:</td>
					<td>" . $i . "<input type='hidden' name='newsid' value='" . $i . "' /><input type='hidden' name='oldim' value='" . $enrow['newsimage'] . "' /></td>
				</tr>


					<tr>
						<td>Title:</td>
						<td><input type='text' name='title' value='" . $enrow['newstitle'] . "' size='100' /></td>
					</tr>

						<tr>
						<td>Author:</td>
						<td><input type='text' name='author' value='" . $enrow['newsauthor'] . "' size='100' /></td>
					</tr>

					<tr>
						<td>Status:</td>
						<td><select name='stat'><option value='enabled' ";
		if ($enrow['newstatus'] == "enabled") {
			echo "selected='selected' ";
		}
		echo ">Enabled</option><option value='disabled' ";
		if ($enrow['newsstatus'] == "disabled") {
			echo "selected='selected' ";
		}
		echo "}>Disabled</option></select>
					</td>
					</tr>

					<tr>
						<td>Snippettext:</td>
						<td><textarea name='snip' rows='6' cols='80'>" . $enrow['newssnippet'] . "</textarea></td>
					</tr>

					<tr>
						<td>News story:</td>
						<td><textarea name='stry' rows='20' cols='80'>" . $enrow['newsarticle'] . "</textarea></td>
					</tr>


					<tr>
						<td>Current image:</td>
						<td>" . $enrow['newsimage'] . " - <a href='news/" . $enrow['newsimage'] . "' target='_blank'>View Image</a></td>
					</tr>

					<tr>
						<td>Change Image:</td>
						<td><input type='file' name='file'></td>
					</tr>

					<tr>
						<td colspan='2'><input type='submit' name='submit' value='Edit' /></td>
					</tr>
				</table>
				</form>
			";
				}

		}
			else {
			echo "Oh noes, an error!";
		}
}

The 'trigger_error' code has quite helpfully pointed out a notice. More specifically this notice:

Notice: SELECT * FROM news WHERE newsid= - has encountered an error at: ......
in /home/sites/agile-cms.co.uk/public_html/admin/functions/newseditform.php on line 11

 

I have done a google search on it to no avail.

 

I have changed the query to read:

$enquery = "SELECT * FROM news WHERE newsid=1";

SO now when I click 'Edit' no matter what row it will always display the results for the row with id=1 which works quite happily so it must be something to do with the $i but I can't see what.

 

Can someone please point me in the right direction with this?

Link to comment
Share on other sites

After all this time I'm still getting the impression that $i doesn't contain what you expect. So once again I tell you to echo out $enquery to see what your query string actually shows. You said you did that and it didn't echo anything out, which is kind of impossible, unless it was visually hidden because of the rest of the html layout on your page. I then asked you to ctrl+f for it in viewsource or output a js console log or alert and... near as I can tell, you just ignored me.

 

Look, this is really simple. If it works if you hardcode a number, but doesn't work when you use $i then obviously $i doesn't contain what you expect. Either it's not getting a value, or it's getting a bad value, or maybe it's getting an id that just isn't in your table.

 

Look at the trigger error message, where you echo out $enquery.

 

  

Notice: SELECT * FROM news WHERE newsid= - has encountered an error at: ......

That red stuff right there. That's your query string your sending to the db. Notice how there is no number after newsid? So, $i is not getting a value. $i is defined as an argument passed to your editnews() function. You said way back in your original post:

 

This code displays on the 'Edit' page using the following above the DOC type:

<?php
include('functions/newseditform.php');
$i = $_GET['i'];
?>
AND the following in the HTML:

<?php
editnews($i)
?>

 

So, assuming this code is really there and in the correct order/scope, are you including an "i" parameter on your page when you view it? e.g. mypage.php?i=123. I.. I feel like this too is something that was mentioned already.
Link to comment
Share on other sites

Yes I have included an "i" parameter. When you click on 'Edit' the link in the php is

<a href='editnews.php?i=" . $gnrow['newsid'] . "'>Edit</a>

So when I click on 'Edit' it should find the newsid and pull that information out but I can't fathom out why which is why I came to the forum. The error message as shown in the post you have linked to didn't actually show me anything on the screen, just a blank page.

 

 

However, since my last post - the query is now working to some extent.

the URL is finding 'News Id' and populating with the correct id number after the .php?i=

 

So it is performing the query just displaying a white screen.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.