CMS made simple: using mysql_select_db($dbname); messes up everything - php

hi im fairly new to cms made simple and ive stumbled upon a problem thats beyond me, my coad is
<?php
$dbh = 'localhost';
$dbu = 'root';
$dbp = '';
$connect = mysql_connect($dbh, $dbu, $dbp) or die ('Error connecting to mysql');
$yatzi = 'myposts';
mysql_select_db($yatzi);
echo "hello";
?>
and im using this through a user defined tag to import a php file, the proble is that everytime i load this page an error pops up saying:
string(61) "Smarty error: unable to read resource: "globalcontent:footer"" string(61) "Smarty error: unable to read resource: "globalcontent:footer""
and everything gets messedup, i seriously have no idead what is going on, can anybody please help me,, thnks...

If this connection is to the same db server the issue could be that youre overwriting the connection resource and thus your CMS cant pull anything form the db.
This would be because by default PHP will detect that you already have a connection open and return that one if they share the same parameters. You can override this behavior by forcing a new connection:
$connect = mysql_connect($dbh, $dbu, $dbp, true);
Then when using this server you need to make sure you always specify which link to use:
mysql_select_db($yatzi, $connect);
mysql_query($query, $connect);
// etc...
Ohter possible issues might be that you have the code in the worng place (like directly in a Smarty template file without the special php escape tags surrounding it), or that the problem isnt related to your code at all and something is up with your CMS installation or customization.

<?php
$dbh = 'localhost';
$dbu = 'root';
$dbp = '';
$yatzi = 'myposts';
$connect = mysqli_connect($dbh, $dbu, $dbp,$yatzi) or die ('Error connecting to mysql');
echo "hello";
?>

Related

Why does my mysqli_query return false?

I realise there are a lot of questions like this here, but none of them seem to help me. The problem is as follows:
I have a website with an SQL Database, and now I am developing an application that reads the data. I've read that the best way to do that is a web service, so I'm in the process of writing one now. First, I'm trying to get all the data to display on a webpage, for testing purposes. I have the following code now:
<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";
$db = "mydatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $db, 3306);
// Check connection
if (!$conn){echo "Not connected";} else {echo "connected";}
$query = "SHOW TABLES FROM mydata";
$result = mysqli_query($query) or die(" but not executed" . mysqli_error());
?>
When I save this and go to mywebsite/test.php, it displays the following:
connected but not executed. That means that it connects right, but the mysqli_query() returns false. However, mysqli_error is empty.
This is the entire test.php file. Am I missing something?
"Am I missing something?"
Yes, db connection arguments to both mysqli_query() and mysqli_error().
RTM's
http://php.net/manual/en/mysqli.query.php
http://php.net/manual/en/mysqli.error.php
It's all in there.
Btw, this is a community wiki. No rep should come of this.
If that still doesn't work, then you may have to remove the port , 3306.
Your $query appears to say
SHOW TABLES FROM mydata
However, your database is actually called mydatabase, as demonstrated on the $db line.

Connecting to mysql database within WordPress server

We are trying to create a login screen for a WordPress website. I think the way to connect to the database is good. The code also seems to be good, we have a layout where someone types the username and password. Those are stored in variables and then it should connect to a database.
Before the following lines of code the ?>
TEST prints out TEST. However when you try to login the error 500 pops up, and no print of TEST. The error code 500 is very wide unfortunately.
We are working outside the code of WordPress in a different folder. WordPress has 3 folders on the server named wp-admin, wp-content and wp-includes. We just created a folder next to it and are trying to build it there. I'd like to find out the options why it is not working, some internet research here brought me to the wp-config. But that didn't work out for us yet.
$connection = mysql_connect("IP", "username", "password");
?>
<HTML><BODY>TEST</BODY></HTML>
<?php
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// Selecting Database
$db = mysql_select_db("db_name", $connection)
or die("no connection to database");
I can add the code of the login screen as well if its necessary, just comment if that is needed.
**** I used old functionalities of PHP and that is why it is not connecting. For WordPress do not use mysql_connect but mysqli_connect.
The best way to load only load the core functionality of WordPress is to use the wp-load.php.
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-load.php';
include_once $path . '/wp-config.php';
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Test the connection:
if (mysqli_connect_errno()){
// Connection Error
exit("Couldn't connect to the database: ".mysqli_connect_error());
}

Cannot connect to MYSQL in MAMP using PHP

I just installed MAMP and have created a MYSQL database. I can access it via PHPMYADMIN.
In my php page I have this, pasted directly from the MAMP webstart page--
$user = 'root';
$password = 'root';
$db = 'local_db';
$host = 'localhost';
$port = 3306;
$link = mysql_connect(
"$host:$port",
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);
The resulting page stops at this point, won't print anything below these instructions.
I've tried changing the port in the MAMP preferences. I also included or die("Could not connect"); after the first line, but still don't get any text after the link data in the page.
I checked online, and others with the problem at least see the die text. I don't get that.
I haven't changed any passwords or data other than mess with the port number.
Any help would be appreciated!
Please give the following a try, I have developed and tested it locally, functionality within has been documented to help you understand what is going on in every step.
/**
*
* Modern method of connecting to a MySQL database and keeping it simple.
*
* If you would like to learn more about PDO,
* please visit http://php.net/manual/en/book.pdo.php
*
*/
//Set up database connection constants, so they cannot be changed.
define('DBHOST','127.0.0.1'); //Change this to the ip address of your database
define('DBNAME','test'); // Change this to the database name you are trying to connect to.
define('DBUSER','databaseuser'); // Insure this user is not the root user!!!!
define('DBPASS','databasepassword'); // Insure this is not the root password!!!!
//Let's try to connect to the database first.
try {
//Initiate a new PDO object called $MYDB and pass it the proper information to make
//the connection
$MYDB = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME."", DBUSER, DBPASS);
//If we are successful show it :D for the test page, if this is for production you should not show this.
echo "Database connection was successful.";
//If this does not worth catch the exception thrown by PDO so we can use it.
} catch(PDOException $e) {
//Show that there was an issue connecting to the database. Do not be specific because,
//user's do not need to know the specific error that is causing a problem for security
//reasons.
echo "Oh, sorry there was an issue with your request please try again.";
//Since we had an issue connecting to the database we should log it, so we can review it.
error_log("Database Error" . $e->getMessage());
}
//Since this is 100% php code we do not need to add a closing php tag
//Visit http://php.net/manual/en/language.basic-syntax.phptags.php for more information.
If you have any issues with this please attempt to break it up into smaller pieces while reviewing the PDO documentation.

if statement doesnt need action

I'm making a user login system with php. Obviously to do that I need to have databases and I do using PHP MyAdmin. The connection in my php to mysql didn't work at first so I looked it up online and found this code:
<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db = 'a_database';
if(!mysql_connect($mysql_host, $mysql_user, $mysql_pass)||!mysql_select_db($mysql_db)){
die(mysql_error());
}
?>
And this would be enough to connect to SQL. So here is my question: how can it be that you don't have to actually connect to the database in order for it to work? Because to my knowledge of php all that is doing is checking if that works, it isn't actually connecting. So is this code correct? Or do you need to add
mysql_connect($mysql_host, $mysql_user, $mysql_pass)||!mysql_select_db($mysql_db))
to this code to make it work? Thanks in advance.
(I'm using XAMPP btw)
mysql_connect tries to connect to to database. If it's false (connection failed) - your script dies. Otherwise - connection established succesfully and we need to check second condition
mysql_select_db selects db. It is false - your script dies too, if not - db selected successfully.

Can't get my PHP document to connect to my xampp server

I am very new to php, and am sorry if this question turns out to be too vague, but if there is any other information that you need from me let me know.
Anyway, basically what my problem is I have some simple php code that should call die() if it can't connect to the xampp server I have set up, however, even if I put in invalid info for the server or user it prints Successful. I really don't know where to go with this, so if anyone has any suggestions that would be great.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
if ( !mysql_connect($dbhost, $dbuser, $dbpass) )
{
die(mysql_error());
}
else
echo 'Succesful';
?>
Ok for some reason it was just not working with the root user, so I created a new user who required a password and apparently that connected like it was supposed to. Thanks everyone for the answers.
Your code is correct pistolpete333. For your root user issue, that your not able to connect with root user you can check below file for your phpmyadmin configuration
C:\wamp\apps\phpmyadmin3.5.1\config.inc.php.
In above file you can check your host, username and password of phpmyadmin.
I have found that when trying to connect php to an xampp server there are usually only a few things that could cause the issue you are having.
1. mySQL service is not running
2. no specific database is selected to try to access
3. user does not exist in phpmyadmin.
When you pick a user to connect php to mySQL with you should always make sure that a password is set, that is usually the thing people miss. You can use root if you create another instance of it in the user list and require a password, or you can add a password to the root user already in the list, but the best option is to create a new custom user that only has access to the database you want to access with your php and make sure it has a password required. (make sure any user you use can connect with localhost; root can by default and I think most newly created users can as well).
Below is the php I use for websites I design that need php to access a DB.
<?php
// This configures the variables for database access
$dbhost = 'localhost';
$dbuser = '????';
$dbpass = '????';
$dbname = '????';
// This will connect to the server and then the correct database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
This is working because you have specified valid username, host and password to mysql. If you specify wrong password, you get mysql error. This code works and connects to the mysql server though you have not selected any database.
Try this code instead
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link); //foo is your database name
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>

Categories