Unhelping error message - php

What the flippedy hell is wrong with that error, and where does it come from ?
I'm in charge of renovating a website and the page was covered in errors because of the use of mysql_connect, so I switched to mysqli_connect and got this error in the process.
EDIT: Here's the code of "mysql.php":
<?php
$db = mysqli_connect("host","username","password","database");
#session_start();
?>
(The session start is here because hte file is called at the start of every page. The informations in "..." has obviously been modified)

The error lies in the mysqli_connect. Question answered.
However to trouble shoot your problem(what you are acutally asking) try the following steps:
if you make a blank php page and connect to the database, does it also give errors?
Is the database server up and running?
Is the database corrupted?
is there a backup database?
Is mysqli even installed on that server?
What's the PHP version? Does it support mysqli?
Did all mysql_real_escape_string get transformed to the mysqli variant?

Related

Not able to perform operations on my database when the site is online

I'am doing a dynamic site in php. The contents to my 'project' page can be added/edited/deleted from the backend. I'm able to do the same from my computer (localhost). When I upload the site online, I'm not able to perform these operations. I have given all user privileges from the cpanel. Still its not working. I'm able to login to my backend and even view the contents that's been loaded dynamically. But i'm not able to add/delete/edit (only when its online). This is my connection code:
<?php
$connection =mysql_connect("localhost","user_name","password");
$database=mysql_select_db("db_name");
if(isset($_SESSION["message1"]))
{
echo "<script language='javascript'> alert('{$_SESSION['message1']}'); </script>";
}
?>
Can Somebody help me solve this? Is it something related to my host name?
I cannot add comments and that's why I answer your question !
First check in your cPanel what PHP version you are using, if it's 7.x, you will not be able use mysql_* api, you may use mysqli_* or even better PDO, however, I think you can change your PHP version in cPanel to lower version, also if you can load contents with same connection using mysql_*, this might not be the problem !
Can you post your add/delete/edit queries here ?
Your host should be probably ok, however you can test the connection using try {} catch {}
One thing that I can think of would be different table name or column name between your local and online database, this things might happen often when working on two different environments

How to connect MySQL to PHP?

So I'm brand spanking new to mysql and php.
I'm set up with Mysql workbench and I'm practicing building a site using Notepad++ and just run it through Chrome. All I want to do is create a sign up page, which I'm assuming I use a .php page on the site, where it would be a username and password. That's it. I can't seem to find any tutorials on how to connect mysql to the .php page, or how to create a sign in page. Any help would be appreciated!
Welcome to PHP!
Typically a connection is established on a PHP page with something along the lines of this:
$conn = mysqli_connect("localhost","[username]","[password]","[databasename]") or die("Error " . mysqli_error($conn));
The "or die" will produce an error if there's a problem establishing a connection. Also, this uses the newer "mysqli_" method for connection; make sure when you call this connection in future that you use mysqli_ methods (there are still traditional "mysql_" methods available, but are depreciated).
Hope this helps!
M
here you go you, here you can find a way to properly connect to the database as well as all the data you need to get set up with your signup form
http://mrbool.com/how-to-create-a-sign-up-form-registration-with-php-and-mysql/28675

Connecting html to mysql with php - brand new to php

I have a few general questions I would like to have a better understanding on.
For a project I have been spending my free time on, I am attempting to display a ladder-based rankings table for a weekly-based chess tournament on a website. To do this, I have created a MYSQL database to hold each player's wins/losses/tournament wins. I understand that in order to display this data onto a html-based website, I need to use php to connect the database and the site.
I also have a very shallow understanding of php as a server-side language, and not a client-side one. Basically to me, this means that php can only be understood by the online server hosting the site, and not by the web browser (such as Google Chrome) itself. Consequently, this means that I can't simply run a .php file by opening it in Notepad++ and choosing "Run in Chrome". That will not work - I can only test php files by first saving them into the website directory and viewing them online. Is this the correct way of thinking about this?
My second question is more straightforward - and it involves the steps required to connect my website and the MYSQL server. Here is my first attempt:
<?php
//Connect to database
$db = mysql_connect("a2412233_ss","a2412233_ss", 'My_Password');
if (! $db){
die("Database connection failed: " . mysql_error());
}
else
echo("Success!");
?>
<html>
<head>
<Title> MySQL Table Connection </Title>
</head>
<body>
</body>
</html>
Two problems with this that I would love to get cleared up:
1) When I click the link to this file from my index.html on the website, the php file downloads rather than opening up on the browser window like a normal webpage should. So clearly something is wrong there.
2) Secondly, I am uncomfortable with the fact that written this way, anyone can view my database info/password by viewing this page source. So there has to be a better way of doing this where that php isn't visible by viewing the source.
Thank you all for the clarification and kick in the right direction! Slowly getting this html/MYSQL/php stuff figured out.
In regards to your first question, Yes that is correct, as it is server side and not client side, you can only view your php page if the code is running through a host.
I would recommend looking into getting XAMPP, which is a piece of software which allows you to use a local host to more quickly test out your code, this also comes with a free MySQL database (for local use only, but still serves a good purpose for testing). But anyway, you can look up on youtube tutorials on how to get XAMPP set up, it's not that complicated, and will save you tons of time when testing out your code, as it's then just as simple as editing your code, hitting the save button and then you can view the changes straight away in your browser.
Also I'd like to point out that you should rename your index.html to index.php, otherwise your browser won't/may not recognise your php code.
In regards to your second question, mysql_connect is generally an older way of connecting to the database, and a lot of people may advice you to avoid it, but I suppose it will do no harm at all to connect to a database this way, yet again, for this question you should be able to find many tutorials on youtube to help you set up your database and connect to it using a php script.
Oh and one last thing, to get rid of your doubts, as PHP is server side, it means that if someone was to 'view source code', they will not be able to see any of your PHP code, all they will see if the HTML and CSS code (or any other client side code), and therefore your username and passwords for your database are entirely safe.
EDIT: I'd also recommend looking at this PHP manual to pick up some of the basics of the language: http://php.net/manual/en/index.php

After recent shared hosting updates to Php, MySQL and Apache my site is not pulling data from MySQL (1064 syntax error)

Newbie here. My hosting company recently upgraded their server(s) and that's where my website problems began:
FYI:
MySQL upgraded from 5.1.70 to version 5.5.32
Apache upgraded from 2.2.25 to version 2.4.6
PHP upgraded from 5.3.27 to version 5.4.22
I have a shared hosting account
For the last 2 days I've been searching Google, online manuals and developer/tech forums for possible answers to my "issues" and I'm having no luck finding good answers that help me fix my code to make my website display properly. Very frustrating. Obviously I need to learn more.
BKGD: I hired a developer to build a CMS for my website a couple of years ago and have since been trying to learn so I have a VERY basic understanding of Php, MySQL and HTML... but obviously not enough to fix these issues. Can you/anyone please help me get closer to fixing this nightmare.
I know my original website code worked fine a week ago - prior to the hosting updates.
The Index page and other "main Sections" currently display just fine for some reason. But if you click on any of the links to "articles", "news" and other "pages", those pages are not connecting properly >> not pulling table data from the associated MySQL database. Those pages showed a couple of different error messages.... initially displayed ONLY a "1064 syntax error" message OR "no page found" error (no "header", "footer", "page title", or any other info). I've since tweaked the code (I.e., I "hid" some of it with "//") and have it displaying the header and footer but nothing from MySQL database.
The original "php.ini" file needed extra code >> "register_globals=on" << to work properly after the second-to-last Php & MySQL update. That is now "deprecated" and "removed".
This newbie thinks that the following code is ONE EXAMPLE that isn't working properly (that I believe needs to be edited/updated to allow it to connect to MySQL). I think if I figure this one out I can apply similar tweaks to the other "news" and "article" pages:
⁣
//-- START: SAMPLE CODE ----
include "init.php";
$res = mysql_query("SELECT * FROM {$prefix}pages WHERE pag_name='$page_name'") or report();
if (mysql_num_rows($res)==1)
cdie("no page found");
else
$row=fetch($res);
$title = "$row[pag_title]";
$desc = "$row[pag_description]";
//------- END: SAMPLE CODE --
I realize I might be way off on this being the issue (E.g., the problem might be in the functions.php file) but that's where I'm at right now. Clueless. Ugh.
Any thoughts or suggestions? Need more info (just ask and I'll reply as soon as I get the email).
If your code relies on register_globals, you are going to run into undefined variable problems.
For example, if the $page_name variable comes from a query string like index.php?page_name=test, you could access it before like $page_name but now you must access it like $_GET['page_name'].
You need to check all your code to see if your variables are defined (a good IDE can help with that).
As a sidenote, you also need to double-check to see if you don't have any sql injection problems, injecting variables directly in a query is normally not a good sign. You should really switch to PDO or mysqli and prepared statements but at the very least use mysql_real_escape_string on your variables before you use them in your queries.

MySQL server has gone away (PHP)

So, I'm getting the following error:
Error Number: 2006
MySQL server has gone away
I can all but guarantee this is because the script takes a bazillion minutes to run, and then more queries are run in the middle of the script. Considering this is an admin-only, huge file-writing script, this is to be expected. (and is perfectly acceptable) I think the MySQL connection is getting closed because PHP / MySQL / something isn't holding it open. However, I can't for the life of me figure out how to stop this timeout from occurring!
I have the following at the top of my PHP script, but it doesn't appear to be helping.
ini_set('default_socket_timeout', -1);
ini_set('max_execution_time', -1);
ini_set('memory_limit', -1);
ini_set('mysql.connect_timeout', -1);
Any idea how I can fix this problem? Thanks!
I think a viable option would be to mysql_connect everytime before you call mysql_query. This ensures that there will be a live connection before every query.
But a much better approach is to first check for the connection status, and only then reconnect if necessary. mysql_ping is useful for checking the connection status, so you can use this to check if re-connection is required. Example code present in #galador's answer.
Thanks #diolemo and #galador.

Categories