Jump to content

mysqli_query problem

Recommended Posts

As the query works fine in phpMyAdmin or MySQL workbench, I am assuming at present it is a php problem. I have the following code being used to uniquely identify a person from a form entry,



if (mysqli_connect_errno($condb))


echo 'Failed to connect to MySQL: ' . mysqli_connect_error();


else {echo 'connected';} //****debug 1


$firstname = preg_replace('/[^a-zA-Zs]/', '', $_POST['idfirstname'] );

$lastname = preg_replace('/[^a-zA-Zs]/', '', $_POST['idlastname'] );

$streetno = preg_replace('/[^0-9s]/', '', $_POST['idhouseno'] );

$telephone = preg_replace('/[^0-9s]/', '', $_POST['idtelephone'] );


// echo '<br>'.$firstname.', '.$lastname.', '.$streetno.', '.$telephone; //****debug 2


$query="SELECT .....;";

echo '<br><b>Query for user id:</b> <br>'.$query; //****debug 3

$result = mysqli_query($condb,$query);

$count = mysqli_num_rows($result);

echo '<br><b> row count:</b>'.$count.' - result: '.$result. '!'; //****debug 4

// --------------------------------------------------------------------------------------------------------------

// Check for unique data

/* if($count == 1) {

// unique ID successfull

while($row = mysqli_fetch_array($resultA))

{ ......


Code was working fine this morning and I was refining the code further on and adding other extraction options in new pages based on having a unique identification. However it has suddenly stopped working. Other parts of the site, not relying on this identification are working fine. Debug point 1 comes up fine as 'connected', the data at debug 2 is fine and if I cut and paste the query output in the browser from debug point 3 into phpMyAdmin or mySQL workbench, no problems, I get the results expected. However debug point 4 does not materialise. If I comment out the line $result = mysqli_query($condb,$query); debug 4 comes up, but obviously not with any results. I'm using Aptana workbench and that is not flagging any syntax errors. I have tried rewriting the line and cut and pasted a similar mysqli_query line from other pages where it is working in case a funny character had got into the code, but to no effect. As I have lots of other similar code instances still working fine, this is causing some frustration. Any ideas on what I am hoping is an obvious error that I just can't see, very much appreciated.





Link to post
Share on other sites

Tried adding mysqli_error() and could not get any errors reported. I did add some code to simulate a successful query and the other three queries in the code worked fine. So I tried pasting in one of the $result = mysqli_query($dbAcon,$query) statements that worked for the other queries with no progress. The query causing the problem did have a join in it, but as I said, had been working fine. I was going to split the query to take out the join for other reasons, so I thought I'd try that as I was planning to run a demo on Tuesday and it is now working. I then did a quick survey of my other pages and found that the only places where I use joins I have created views in the database first. That's the only difference I can see. Although why it should work and then not work.... So I have a solution, but not a satisfactory reason for the failure.


many thanks for answering.

Link to post
Share on other sites

you should check if query is successfully executing or not. to do so you need to use mysqli_error() with mysql_query() too as mentioned by niche. If your query has external unescaped input, it could break the query.

Link to post
Share on other sites

That's part of the problem - I'm not seeing any error returns at all. I see one echo, with the sqli_error, after the problem query statement and that's it. As far as I can see there is still a lot of valid HTML output that should come out on further echoes but if you view the source code in the browser there is just nothing else after the sqli_error echo, not even the closing Body and HTML tags.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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