SQL query err_empty_response - php

I have a problem when executing a simple piece of code:
$con=mysqli_connect($host,$user,$password,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM exercises");
if (mysql_num_rows($result) > 0) {
}
The variables on the first line are imported from another file. Everything works until I try to do anything with the $result variable. I can get the result from the database without an error, but as soon as I start doing something with it in an if statement or a loop (to fetch the results), my page will simply not load, and I get an err_empty_response. I tested it in multiple browsers. Also, some other of these issues on stackoverflow did not seem to have the soultion to the problem.
All help is appreciated.

Related

PHP/MYSQLI: mysqli_query fails in PHP

First off, I'm a total noob in mysql(i) language and know a little more than the basics of PHP.
Note: I do not manage or own / have access to the server on which the webpage currently is hosted. I can however access the phpMyAdmin page.
That said, I've got a webpage on which I am trying out some stuff. Right now I'm trying to make a log-in page linked to a database.
Now, behold the code:
$mysql_host = "localhost";
$mysql_user = "my_user";
$mysql_database = "my_database";
$mysql_password = "my_password";
$table = "my_tablename";
// Create connection
$con=mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// sending query
$result = mysqli_query("SELECT * FROM my_tablename");
if (!$result) {
echo "Query to show fields from table failed! :-(";
}
Now, here comes the actual problem. As soon as I launch the page it will give me my "Query to show fields from table failed!" error message. But when I enter the same query on the phpMyAdmin 'SQL' tab, I get the wanted results. How come the webpage gives me an error, while the phpMyAdmin gives me the results, and, how do I solve it?
Use correct syntax:
$result = mysqli_query($con, "SELECT * FROM my_tablename");
You forgot to link current mysqli connection. First parameter is link - which mysqli connection you want to use (good for multiple conns) and then the second is your query.

PHP file_get_contents() insert web page to MySQL database not working

I have a wierd problem with my script that is suppose to insert the web page's source code into the database.
This script works fine when I'm fetching tiny html page on same server where this code is but when I try to fetch some other page over the internet it just won't work and it doesn't give any errors.
My own thought was that MySQL query runs before the web page is assigned to the $content variable? Is there any way to go around this?
// Set web page to fetch
$url = "http://www.webpage.com";
// Assign web page souce to variable
$content = utf8_decode(htmlspecialchars(file_get_contents($url)));
// Standard MySQL connection
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Insert web page source to database
mysqli_query($con, "INSERT INTO table_name (content) VALUES ('$content')");
//Close connection
mysqli_close($con);
The REAL problem with your code is the absence of any error checking. So now you are assuming your code works and when it doesn't you wonder why. If you did, it would have saved you and others a lot of time.
if (!mysqli_query($con,"INSERT INTO table_name (content) VALUES ('$content')"))
{
echo("An error occured: " . mysqli_error($con));
}
//Insert web page source to database
$content = mysqli_real_escape_string($con,$content);
mysqli_query($con, "INSERT INTO table_name (content) VALUES ('$content')");

Php / Html - database disconnect

I have a working php code which simply connects database lists the data and closes it.
I embedded this working php code into my html but it does not parse the rest of the html. It is simple blank after listing data from database and php close tag " ?> ". I tried to get rid of " $db->disconnect(); " in php and now it works.
Do you have any idea why?
Do i really need to use $db->disconnect(); every time?
Thanks
beginning of the html code
<ul>
<?php
$db = new mysqli('localhost', 'root', 'root', 'troy');
if (mysqli_connect_errno()) {
printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
exit;
}
$query = "SELECT * FROM platters";
$result = $db->query($query);
while($row = mysqli_fetch_array($result)) {
echo "<li>" . $row['name'] . "</li>";
}
$db->disconnect();
?>
</ul>
rest of the html code
$db->disconnect(); is not listed under the documentation, is this a custom function that you have created?
Instead of disconnecting the entire database connection, you should just perform:
$result->close();
this will free up the current query, and enable you to perform further connections throughout that PHP Script.
Only close the connection to the database entirely if you are done with your SQL Connection for that script
Edit
After re-reading your question, $db->disconnect(); is not a function for MySQli. See the entire documentation here:
http://php.net/manual/en/book.mysqli.php
You should use:
$db->close(); which closes the current connection to the database (if used on your database variable)
http://www.php.net/manual/en/mysqli.close.php
Run it on the command line
php test.php
and see the error message
PHP Fatal error: Call to undefined method mysqli::disconnect() in test.php on line 28
This is the reason, why it outputs everything from the database, but omits everything after that line.
When you remove the $db->disconnect(); statement, the error is gone and the PHP script can work it's way until the end.

Mysql fails in php but works in phpmyadmin

I've made this a lot of times but now I can't :(
The insert allways return false but if I execute the same SQL script (taked from the output) it inserts in the database without any problem. I'm connected to the database because some values are fetched from another table.
This is my code:
$query = "INSERT INTO normotensiones(fecha,macropera,pozo,equipo_pmx,equipo_compania,paciente,sexo,edad,id_compania,otra_compania,puesto,ta,tum,ove,coordinador)
VALUES('$fecha','$macropera','$pozo','$equipo_pmx','$equipo_compania','$paciente','$sexo',$edad,$id_compania,'$otra_compania','$puesto','$ta','$tum','$ove','$coordinador')";
if (mysql_query($query,$connection)){
//OK
} else {
$errno = mysql_errno();
$error = mysql_error();
mysql_close($connection);
die("<br />$errno - $error<br /><br />$query");
exit;
}
The output is:
0 -
INSERT INTO normotensiones(fecha,macropera,pozo,equipo_pmx, equipo_compania,paciente,sexo,edad,id_compania, otra_compania,puesto,ta,tum,ove,coordinador)
VALUES('20111001','P. ALEMAN 1739','P. ALEMAN 1715','726', 'WDI 838','SERGIO AYALA','M',33,21, '','','110/70','ROBERTO ELIEL CAMARILLO','VICTOR HUGO RAMIREZ','LIC. PABLO GARCES')
Looks like there are no error, but allways execute the code in the else part of the if instruction. Any idea? Thanks in advance.
I think the issue might be you are missing the mysql_select_db line after the connection.
After the connection with the database is established you need to select a DB. Please make sure you have selected the Database that your desired table resides in.
And you can even use the following snippets to get some useful informated through mysql_errors.
$connection = mysql_connect('localhost', 'root', 'password');
if (!$connection) {
die('<br>Could not connect: ' . mysql_error());
}
if (!mysql_select_db('db_name')) {
die('Could not select database: ' . mysql_error());
}
And try you insert query after these lines of code. All the best.
I agree with the others concerning the column types. INT is one of the only data types that do not require single quotes.
There are two blank strings. There is a possibility that the variables are not defined, and therefore giving you a PHP exception (not even in the MySql yet) but that requires stricter-than-normal exception settings. I would personally look into the $connection variable. Before the SQL query statement, put this and send us the cleaned results:
echo '<pre>'.var_dump($connection, true).'</pre>';
Additionally, on your mysql_connect function call, put
OR die('No connection')
afterwords. Do the same thing with the mysql_select_db function, changing it to 'No DB Select' obviously.
Ultimately, we will need more information. But changing to mysqli is very desirable.
Oh! And make sure the permissions for the user you are connecting as are not changed. Sometimes I find people who connect to PhpMyAdmin using one user account but a different account in their PHP code. This is problematic, and will lead to problems eventually, as you forget the different accounts, at times.

Could anyone with MySQLi experience help me with this short SELECT code?

I have researched my question but there just isn't much help out there for MySQLI functions. I'm also new here so, please bear with me.
I have a test database named "website" and it contains a table called "users" which has data for all of the members of my website. In a separate file I have the php code that connects to my localhost and selects the database "website." I'm using the newest functions of MySQL so instead of the connection string containing mysql_connect it contains mysqli_connect. The registration process works great and I can find no error in any of the other code that uses the mysqli functions.
My include file (connect.php)
<?php
$link = mysqli_connect("localhost", "useracc", "useracc1", "website");
?>
I set up a test script for the login section.
<?php
include('connect.php');
$user = $_POST['user'];
$query = "SELECT birthday FROM users";
$result = mysqli_query($link, $query) or die ("RESULT ERROR");
echo "$user";
echo "$result";
?>
There is a password box on the previous page but for the test script I left that out and so far, only the $user is shown on the page. I get no error even when I replace "RESULT ERROR" with mysqli_error($link). I've tried interchanging ' for " in every part of the code that uses quotes but that didn't work. I've tried rewording the $query line but that also had no effect. I'm really new to MySQL and php so if you guys could tell me where I'm going wrong I'd really appreciate it.
Thanks for reading my question. Have a great day.
Add the following lines to the connect.php after you connect line
if (mysqli_connect_errno()) {
printf("Can't connect Errorcode: %s\n", mysqli_connect_error());
exit;
}
this will ensure the connection is made without any errors and return them if there are ...

Categories